简体   繁体   中英

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. 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.

This is my 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 .

When I run make, I get the following error:

/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. This makes the project/library known to 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 ):
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.

In case this is not possible, you can set the include_directories. My CMakeLists.txt looks like this (only change to yours is the include_directories command, using ${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-user the executable code which uses the library
  • ./CMakeLists.txt only contains:
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. See target_link_libraries

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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