如何将外部库添加到CMakeLists?

时间:2017-06-05 22:16:52

标签: makefile cmake linker g++ libpqxx

正常编译(工作正常):

g++ DBHandler.cpp Functions.cpp Main.cpp -I/usr/local/include -L/usr/local/lib -lconfig++ -lpqxx -lpq -o dbhandler

可以运行:

./dbhandler

CMakeLists.txt(标准):

cmake_minimum_required(VERSION 2.8.9)
project(DBHandler)
include_directories(include)
file(GLOB SOURCES "src/*.cpp")
target_link_libraries(dbhandler config++ pqxx pq)
add_executable(dbhandler ${SOURCES})

如何更改CMakeLists.txt并添加:

-I/usr/local/include -L/usr/local/lib -lconfig++ -lpqxx -lpq

使用cmake编译程序?

1 个答案:

答案 0 :(得分:0)

正确的解决方案是使用CMake包PkgConfig来使用pkg_search_module

您的文件将变为:

cmake_minimum_required(VERSION 2.8.9)
project(DBHandler)

find_package(PkgConfig REQUIRED)

pkg_search_module(CONFIGPP REQUIRED config++)
pkg_search_module(PQ REQUIRED pq)
pkg_search_module(PQXX REQUIRED pqxx)

include_directories(include ${CONFIGPP_INCLUDE_DIRS} ${PQ_INCLUDE_DIRS} ${PQXX_INCLUDE_DIRS})
file(GLOB SOURCES "src/*.cpp")
target_link_libraries(dbhandler ${CONFIGPP_LIBRARIES} ${PQ_LIBRARIES} ${PQXX_LIBRARIES})
add_executable(dbhandler ${SOURCES})