[英]How to include another cmake project's header files in my cmake project?
我正在尝试将另一个 CMake 项目包含为我的项目的第三方库。 问题是我不能包含来自所述库的标题。
我尝试使用add_subdirectory
函数添加库,但它似乎不起作用。
这是我的 CMakeLists.txt:
cmake_minimum_required (VERSION 2.6)
project (Cam2ClientCpp)
set(CMAKE_VERBOSE_MAKEFILE ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
include_directories ("${PROJECT_SOURCE_DIR}/restclient-cpp")
add_subdirectory (restclient-cpp)
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)
add_executable (Cam2ClientCpp main.cpp)
target_link_libraries (Cam2ClientCpp restclient-cpp)
另一个项目的 CMakeLists.txt 在这里: https : //github.com/mrtazz/restclient-cpp/blob/master/CMakeLists.txt 。
当我运行 make 时,出现以下错误:
/Library/Developer/CommandLineTools/usr/bin/c++ -I/Users/nhendy/Development/cam2clientcpp/restclient-cpp -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk -std=gnu++11 -o CMakeFiles/Cam2ClientCpp.dir/main.cpp.o -c /Users/nhendy/Development/cam2clientcpp/main.cpp
/Users/nhendy/Development/cam2clientcpp/main.cpp:1:10: fatal error: 'restclient-cpp/connection.h' file not found
#include "restclient-cpp/connection.h"
通常,要使用库,您必须:
add_subdirectory
以便解析和解释此项目的CMakeLists.txt
。 这使 CMake 知道项目/库。PUBLIC
),以及内部包含路径 ( PRIVATE
):target_include_directories(MyLibrary
PUBLIC
include
PRIVATE
src
)
target_link_libraries(MyCoolApp
PRIVATE
MyLibrary
)
这样库导出使用库所需的路径,即不是库的用户知道如何使用它,而只有库本身。
我的建议是为图书馆创建一个 PR,并恳请他们使用这种方法。
如果这是不可能的,您可以设置 include_directories。 我的 CMakeLists.txt 看起来像这样(对你的只有改变的是 include_directories 命令,使用${CMAKE_SOURCE_DIR}
):
cmake_minimum_required (VERSION 2.6)
project (Cam2ClientCpp)
set(CMAKE_VERBOSE_MAKEFILE ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
file(WRITE
${CMAKE_BINARY_DIR}/restclient-cpp/include/restclient-cpp/version.h
"#define RESTCLIENT_VERSION \"0.5.0\"\n"
)
include_directories ("${CMAKE_SOURCE_DIR}/restclient-cpp/include")
include_directories ("${CMAKE_BINARY_DIR}/restclient-cpp/include")
#add_subdirectory (restclient-cpp)
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)
add_executable (Cam2ClientCpp main.cpp)
target_link_libraries (Cam2ClientCpp restclient-cpp)
我使用以下文件结构进行了测试:
./CMakeLists.txt
./restclient-cpp
./restclient-user
./restclient-user/CMakeLists.txt
./restclient-user/main.cpp
cmake_minimum_required(VERSION 3.10)
add_subdirectory(restclient-cpp)
add_subdirectory(restclient-user)
首先需要单独编译第三方库。 然后链接到 CMake 项目中的输出库。 查看target_link_libraries
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.