如何在CLion的CMake文件中正确链接GDAL共享库?
Fixing GDAL Library Linking in CLion's CMakeLists.txt
I see the issue with your current CMake setup — you’re manually attempting to create a GDAL library target instead of using CMake’s built-in functionality to locate and link the pre-installed GDAL shared library. Let’s rewrite your CMakeLists.txt to match the working terminal command you already have:
cmake_minimum_required(VERSION 3.9) project(MyMapsParser) # Set C++ standard to match your terminal command (-std=c++11) set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD_REQUIRED ON) # Let CMake locate the installed GDAL library find_package(GDAL REQUIRED) # Define your executable with all source files add_executable(test main.cpp MapParser.cpp) # Link GDAL's libraries to your executable, and include its header directories target_link_libraries(test PRIVATE ${GDAL_LIBRARIES}) target_include_directories(test PRIVATE ${GDAL_INCLUDE_DIRS})
Key Fixes & Explanations:
find_package(GDAL REQUIRED): This tells CMake to search for the installed GDAL library on your system. It automatically populates variables like${GDAL_LIBRARIES}(path to the shared library) and${GDAL_INCLUDE_DIRS}(path to GDAL's header files) — no need to manually specify paths.- Removed custom
gdallibrary target: Your original code tried to create a library fromMapParser.h, which was incorrect. GDAL is a pre-built system library, so we don’t need to redefine it. add_executable(test ...): Matches your terminal command’s output binary name (test) and includes all necessary source files (main.cppandMapParser.cpp; header files don’t need to be listed here unless you’re using specialized CMake features likeAUTOMOC, which you aren’t in this case).target_link_libraries(...): Links your executable to the GDAL shared library, equivalent to the-lgdalflag in your terminal command.target_include_directories(...): Ensures your code can find GDAL’s header files, which is handled implicitly byg++when using-lgdal, but needs explicit setup in CMake.
Once you update your CMakeLists.txt with this code, CLion should properly configure the project and link the GDAL library just like your terminal command does.
内容的提问来源于stack exchange,提问作者Ali Arsalan
相关产品推荐
相关产品推荐

