0

这是我的 CMakeList.txt 文件,我想包含 python3.9 目录并希望链接 python3.9 路径下的所有头文件和库。当我使用命令行运行它时,我使用了以下命令

 g++ data.cpp -std=c++11 -I/usr/include/python3.9 -lpython3.9 -I/opt/mongo-cxx-driver/include/mongocxx/v_noabi -I/opt/mongo-cxx-driver/include/libmongoc-1.0   -I/opt/mongo-cxx-driver/include/bsoncxx/v_noabi -I/opt/mongo-cxx-driver/include/libbson-1.0   -L/opt/mongo-cxx-driver/lib64 -lmongocxx -lbsoncxx

使用上述命令可以正常工作,但是当我使用以下 CMakeList.txt 文件进行构建时。但它没有链接我的python库。我做错了什么?请帮忙

 cmake_minimum_required(VERSION 3.2)
 set(CMAKE_CXX_STANDARD 11)
 add_subdirectory(../dlib dlib_build)
 add_executable(testmongo data.cpp)

INCLUDE_DIRECTORIES("/usr/include/python3.9")
find_package(libmongocxx REQUIRED)
find_package(libbsoncxx REQUIRED)
include_directories(${LIBMONGOCXX_INCLUDE_DIR})
include_directories(${LIBBSONCXX_INCLUDE_DIR})
include_directories("/opt/mongo-cxx-driver/include/mongocxx/v_noabi")
include_directories("/opt/mongo-cxx-driver/include//bsoncxx/v_noabi")
include_directories("/opt/mongo-cxx-driver/include/libmongoc-1.0")
include_directories("/opt/mongo-cxx-driver/include/libbson-1.0")
include_directories("/opt/mongo-cxx-driver/lib64")
target_link_libraries(testmongo ${LIBMONGOCXX_LIBRARIES})
target_link_libraries(testmongo ${LIBBSONCXX_LIBRARIES})
target_link_libraries(testmongo dlib::dlib)
target_link_libraries(testmongo ${PYTHON_LIBRARIES})
   

一切都很好,除了python库链接

4

1 回答 1

0

include_directories影响在它之后声明的目标。

add_executable(blabla ...)      # does not use /some/dir
include_directories(/some/dir)  # change!
add_executable(blabla2 ...)     # uses /some/dir!

但根本不要使用include_directories。改为使用target_include_directories

于 2021-04-06T16:59:29.600 回答