Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake - Include directories outside project

Tags:

c++

cmake

So I want to include a global header file that is in a different folder. The code for the CMakeList.txt is below. In my .cpp when I include something from the local include folder it works, but not for the something that is in a different folder.

cmake_minimum_required(VERSION 2.8)

#Project name
project(Server-Client)

#Add all cpp files as source files
file(GLOB_RECURSE SOURCES "src/*.cpp")

#Build executable 'Server' with all files in SOURCES
add_executable(Server ${SOURCES})

#Include all files in include directory
include_directories("include")

target_include_directories(Server PUBLIC "../../GlobalFiles/include")

find_package(Threads REQUIRED)

#Build executable 'localization' with all files in SOURCES
target_link_libraries(Server ${CMAKE_THREAD_LIBS_INIT})
like image 215
SomebodyOnEarth Avatar asked Feb 26 '17 03:02

SomebodyOnEarth


People also ask

How do you add include directories to CMake?

First, you use include_directories() to tell CMake to add the directory as -I to the compilation command line. Second, you list the headers in your add_executable() or add_library() call.

What is the difference between include_directories and target_include_directories?

include_directories(x/y) affects directory scope. All targets in this CMakeList, as well as those in all subdirectories added after the point of its call, will have the path x/y added to their include path. target_include_directories(t x/y) has target scope—it adds x/y to the include path for target t .

What does Add_subdirectory do in CMake?

Add a subdirectory to the build. Adds a subdirectory to the build. The source_dir specifies the directory in which the source CMakeLists.

What is Cmake_source_dir?

CMAKE_SOURCE_DIR refers to the top-level source directory that contains a CMakeLists. txt , while PROJECT_SOURCE_DIR refers to the source directory of the most recent project() command. They are often the same, but a common workflow when using CMake is to use add_subdirectory to add libraries.


1 Answers

Don't use a relative paths, instead use CMAKE_CURRENT_SOURCE_DIR variable like this:

target_include_directories(Server PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/../../GlobalFiles/include")

Other than that, it might be a good idea to use a Macro to find the global header you are looking for.

like image 136
arved Avatar answered Sep 22 '22 00:09

arved