Issue
I have a project structured like below -
app
|--src
| |---module
| | |---file1.cpp
| | |---file1.hpp
| |---file2.cpp
| |---file2.hpp
| |---main.cpp
|---CMakeLists.txt
CMakeLists.txt
cmake_minimum_required(VERSION 3.15)
add_executable(testApp)
target_include_directories( testApp INTERFACE src)
target_sources(
src/file2.cpp
src/file2.cpp
src/module/file1.hpp
src/module/file1.cpp
src/main.cpp
)
file1.cpp
#include"../file2.hpp"
//....some code.....
Question: In file1.cpp, I am including #include"../file2.hpp"
giving a relative path to file2.hpp. How do I avoid this ??
I am including this path to CMakeLists.txt
target_include_directories( testApp INTERFACE src)
But when I do #include "file2.hpp"
I get a file not found
error. How do I include the src
folder to my project so that I can use the headers file directly ??
Solution
target_include_directories — CMake 3.27.0-rc4 Documentation
The
INTERFACE
,PUBLIC
andPRIVATE
keywords are required to specify the scope of the following arguments.PRIVATE
andPUBLIC
items will populate theINCLUDE_DIRECTORIES
property of<target>
.PUBLIC
andINTERFACE
items will populate theINTERFACE_INCLUDE_DIRECTORIES
property of<target>
. The following arguments specify include directories.
INTERFACE_INCLUDE_DIRECTORIES — CMake 3.27.0-rc4 Documentation
When target dependencies are specified using
target_link_libraries()
, CMake will read this property from all target dependencies to determine the build properties of the consumer.
So basically INTERAFACE
means that only target which links to given target will have this include directories. Current target build will not be impacted.
Just change this to PUBLIC
:
target_include_directories( testApp PUBLIC src)
Answered By - Marek R Answer Checked By - Marie Seifert (WPSolving Admin)