Issue
I have just created an interface library called "foo". Now I would like to include each of the modules of foo like this:
#include "foo/module.h"
Right now, I always have to write the include path as following
#include "foo/include/module.h"
Is it somehow possible to tell Cmake to ignore the "include" within the path? My CMakelists.txt of the Interface lib foo looks like this:
add_library(foo INTERFACE)
target_include_directories(foo INTERFACE
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)
An the main CMakeLists.txt looks like this:
cmake_minimum_required(VERSION 3.22.1)
project(project)
add_executable(${PROJECT_NAME} main.cpp)
add_subdirectory(foo)
target_link_libraries(${PROJECT_NAME} foo)
What am I doing wrong? Is there anyone who can help me with this?
Thank you very much in advance!
Best regards,
Simon
Solution
Is it somehow possible to tell Cmake to ignore the "include" within the path?
That's not CMake's job. CMake is a build system that hands off the work of actually compiling the C++ text to the compiler and linker. CMake provides command line parameters and tracks which files need to be compiled, but it doesn't deal in how exactly the compiler parses a #include
directive. It can affect that through command-line switches, but only to the extent that compilers permit it.
While every compiler allows users to define a set of directories to look in when processing include directives, to my knowledge, none of them will insert said directory text into the middle of the include string.
Most libraries that deliver headers will have them in an include
or inc
directory. When building code that links to this library, the path to that directory will be given as one of the include paths. Within that directory, they will usually have a library_name
directory, and it is within that directory where the actual headers are.
That is, it's supposed to be "include/foo/module.h", which you would include with #include "foo/module.h"
.
Answered By - Nicol Bolas Answer Checked By - David Goodson (WPSolving Volunteer)