2017-02-26 175 views
3

所以我想包含一個全局頭文件在不同的文件夾中。下面是CMakeList.txt的代碼。在我的.cpp中,當我包含來自本地包含文件夾的某些內容時,它可以工作,但不包含位於不同文件夾中的內容。CMake - 包含項目以外的目錄

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}) 

回答

3

不要使用相對路徑,而是使用CMAKE_CURRENT_SOURCE_DIR變量是這樣的:

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

除此之外,它可能是一個好主意,使用宏來找到全局頭你尋找。

+0

謝謝我被困在這個幾個小時,看到你的解決方案後,我意識到我引用了一個錯誤的文件夾。如果我在include_directories中爲相同的文件夾使用相對路徑,它將起作用。這被認爲是不好的做法?爲什麼? – SomebodyOnEarth