2017-06-05 169 views
1

正常編譯(正常工作):如何將外部庫添加到CMakeLists?

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

[如何添加 「-l」(ELL)編譯器的CMake標誌的可能的複製](https://stackoverflow.com/questions/43136418/how-to-add-l-ell-compiler-flag-in-cmake) – Tsyvarev

+0

我認爲你需要先調用add_executable來創建目標,然後指定庫與target_link_libraries。 – vre

回答

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}) 
+0

CMake錯誤/usr/share/cmake-3.7/Modules/FindPkgConfig.cmake:637(消息): 找不到所需的'pqxx'找到 由sudo apt-get install安裝的庫不是來源。也許,這是缺少findlib.cmake-s文件的問題? – user3455638

+0

你應該使用'sudo apt-get install \t libpqxx-dev'安裝軟件包'libpqxx-dev' – OlivierM

+0

它已經安裝。 – user3455638