简体   繁体   English

如何在我的 cmake 项目中包含另一个 cmake 项目的头文件?

[英]How to include another cmake project's header files in my cmake project?

I'm trying to include another CMake project as a third party library for my project.我正在尝试将另一个 CMake 项目包含为我的项目的第三方库。 The problem is that I can't include the headers from the said library.问题是我不能包含来自所述库的标题。

I tried adding the library using add_subdirectory function but it doesn't seem to work.我尝试使用add_subdirectory函数添加库,但它似乎不起作用。

This is my CMakeLists.txt:这是我的 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)

The other project's CMakeLists.txt is here: https://github.com/mrtazz/restclient-cpp/blob/master/CMakeLists.txt .另一个项目的 CMakeLists.txt 在这里: https : //github.com/mrtazz/restclient-cpp/blob/master/CMakeLists.txt

When I run make, I get the following error:当我运行 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"

Usually, to use a library, you have to:通常,要使用库,您必须:

  • Call add_subdirectory so that the CMakeLists.txt of this project is parsed and interpreted.调用add_subdirectory以便解析和解释此项目的CMakeLists.txt This makes the project/library known to CMake.这使 CMake 知道项目/库。
  • The library project defines the include paths the user of the library needs to know ( PUBLIC ), as well as the internal include paths ( PRIVATE ):库项目定义了库的用户需要知道的包含路径 ( PUBLIC ),以及内部包含路径 ( PRIVATE ):
target_include_directories(MyLibrary
  PUBLIC
    include
  PRIVATE
    src
)
  • The user of the library only needs to reference the library:库的用户只需要引用库:
target_link_libraries(MyCoolApp
  PRIVATE
    MyLibrary
)

This way the library exports the paths required to use the library, ie not the user of the library knows how to use it, but only the library itself.这样库导出使用库所需的路径,即不是库的用户知道如何使用它,而只有库本身。

My suggestion is to create a PR to the library and kindly ask them to use this approach.我的建议是为图书馆创建一个 PR,并恳请他们使用这种方法。

In case this is not possible, you can set the include_directories.如果这是不可能的,您可以设置 include_directories。 My CMakeLists.txt looks like this (only change to yours is the include_directories command, using ${CMAKE_SOURCE_DIR} ):我的 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)

I tested with following file structure:我使用以下文件结构进行了测试:

./CMakeLists.txt
./restclient-cpp
./restclient-user
./restclient-user/CMakeLists.txt
./restclient-user/main.cpp
  • restclient-cpp contains the cloned repo of the client library restclient-cpp 包含客户端库的克隆 repo
  • restclient-user the executable code which uses the library restclient-user 使用库的可执行代码
  • ./CMakeLists.txt only contains: ./CMakeLists.txt 仅包含:
cmake_minimum_required(VERSION 3.10)
add_subdirectory(restclient-cpp)
add_subdirectory(restclient-user)

First, you need to compile the third party library alone.首先需要单独编译第三方库。 Then link against the output library in your CMake project.然后链接到 CMake 项目中的输出库。 See target_link_libraries查看target_link_libraries

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM