cmake:包含目录root?

时间:2018-01-28 17:01:11

标签: c++ cmake

我正在尝试为我正在进行的项目设置cmake,但我遇到了一个我目前无法解决的问题。我的项目具有以下文件夹结构:

MotorEngine (root dir)
| CMakeLists.txt
| ThirdParty
|-| SDL2
|-|-| include (contains all header files for SDL2)
|-|-| lib
|-|-|-| x64
|-|-|-|-| SDL2.lib (the library file I need to link with)
| Source
|-| CMakeLists.txt
|-| main.cpp

根CMakeLists.txt文件:

cmake_minimum_required(VERSION 2.6)
project(MotorEngine)

# Set an output directory for our binaries
set(BIN_DIR ${CMAKE_CURRENT_SOURCE_DIR}/Binaries)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/Binaries)
set(THIRDPARTY_PATH ${CMAKE_CURRENT_SOURCE_DIR}/ThirdParty)

# Include SDL2
include_directories(${THIRDPARTY_PATH}/SDL2/include)

# Add the engine + third party subdirectory
add_subdirectory(Source)

The Source&#39的CMakeLists.txt:

project(MotorEngine)
add_executable(MotorEngine main.cpp)
target_link_libraries(MotorEngine ${THIRDPARTY_PATH}/SDL2/lib/x64/SDL2.lib)

现在,我想在main.cpp中实现以下内容,我想写一下

#include "SDL2/include/SDL2.h"

但目前我必须写

#include "SDL2.h"

由于稍后会有相同名称的文件,我需要在文件夹中区分它们。所以最简单的方法是添加" ThirdParty"文件夹作为根,所以我可以使用#include相对于那个,但做

include_directories(${THIRDPARTY_PATH})

不起作用。有任何想法吗?谢谢!

1 个答案:

答案 0 :(得分:0)

k.v.的帮助下,我能够解决这个问题。我需要在源目录中的CMakeLists.txt中添加以下内容:

target_include_directories(MotorEngine PUBLIC ${THIRDPARTY_PATH})