Johnny Verbeek
Johnny Verbeek

Reputation: 143

cmake: Include directories root?

I'm trying to set up cmake for a project I'm working on, but I have a problem which I can't resolve currently. My project has the following folder structure:

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

The root CMakeLists.txt file:

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's CMakeLists.txt:

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

Now, I want to achieve the following, in the main.cpp I want to write

#include "SDL2/include/SDL2.h"

But currently I have to write

#include "SDL2.h"

Since there will be files with the same name later on, I need to distinguish them in their folders. So the easiest would be to add the "ThirdParty" folder as a root so I can use #include relative to that then, but doing

include_directories(${THIRDPARTY_PATH})

does not work. Any ideas? Thank you!

Upvotes: 2

Views: 5458

Answers (1)

Johnny Verbeek
Johnny Verbeek

Reputation: 143

With help of k.v. I was able to sort this out. I needed to add the following in the CMakeLists.txt in the Source directory:

target_include_directories(MotorEngine PUBLIC ${THIRDPARTY_PATH})

Upvotes: 1

Related Questions