Cmake basic library linking problem

2019-05-10 08:18发布

问题:

I have a simple problem with the linking of libraries with CMake (I don't know CMake very well). My configuration is the following :

project/src/CMakeLists.txt (with all .cpp and .h files)
project/support/linux/gmp/include/gmp.h
project/support/linux/gmp/include/gmpxx.h
project/support/linux/gmp/include/libgmp.a
project/support/linux/gmp/include/libgmpxx.a

How to include the library gmp in the process of compilation ? (I am lost between FIND_PACKAGE, INCLUDE_DIRECTORIES, TARGET_LINK_LIBRARIES, ADD_LIBRARY ... commands)

Thank you very much.

回答1:

CMake is not so hard to understand.

First Step

Use find_package to locate the libary.

find_package(GMP REQUIRED)

Second step

Use include_directories to include the libary header files.

include_directories(${GOBJECT_INCLUDE_DIR})

Third Step

Use target_link_libraries to link your binary against the libary.

add_executable(ExecutableName Main.cpp)
target_link_libraries(ExecutableName ${GOBJECT_LIBRARIES})


标签: linker cmake