Search code examples
c++cmakelinkergdal

Link GDAL library using CMake instead of g++


I wrote a program that reads a GeoTiff file with GDAL. To compile it, I write:

g++ my_program.cpp -lgdal

However, I want to run this code with Clion. How do I add this -lgdal to the CMake file?


Solution

  • The -l is an indication to the linker to link the gdal library to your executable, so the corresponding CMake command is target_link_libraries(). So your CMake might look something like this:

    add_executable(MyExecutable my_program.cpp)
    target_link_libraries(MyExecutable PRIVATE gdal)