0

Here is a simplified version of my C++/CMake project's layout:

├── CMakeLists.txt
│
├── project_name/
│   ├── source_1.hpp
│   └── CMakeLists.txt
│
├── tests/
│   ├── test_1.cpp
│   └── CMakeLists.txt

And here are CMakeLists.txt:

  • Root directory:
    add_subdirectory(project_name)
    add_subdirectory(tests)
    
  • project_name directory:
    add_library(main_library INTERFACE)
    target_include_directories(main_library INTERFACE .)
    
  • tests directory:
    add_executable(test_1 test_1.cpp)
    target_link_libraries(test_1 PRIVATE main_library)
    

So in test_1.cpp I want to include source_1.hpp.
Currently, I have to write #include "../project_name/source_1.hpp".

Is there a way to use a path that would be relative to the project's root directory for such includes, i.e. #include "project_name/source_1.hpp?

Lev
  • 146
  • 2
  • 12
  • Not your point, but `"project_name/source_1.hpp"` is not an absolute path because it's still interpreted relative to the current directory. An absolute path starts with `/` (on Linux at least). – john Jul 08 '20 at 08:07
  • Yeah, sorry, I meant an absolute path from project's root directory. – Lev Jul 08 '20 at 08:11
  • 3
    "that would be relative to the project's root directory" - just call command `include_directories` or `target_include_directories` and pass project's root directory to it. Not sure what is a problem. – Tsyvarev Jul 08 '20 at 08:32
  • "I meant an absolute path from project's root directory" - Once again, *absolute* path is the one started with `/`. All other paths are *relative*. So what do you want is "path relative to the project's root directory". (and path you use currently - `#include "../project_name/source_1.hpp"` - is a "path relative to the current directory"). Please, use **correct terminology** when possible. Many things becomes very confusing when use words in the wrong context. – Tsyvarev Jul 08 '20 at 08:37
  • @Tsyvarev fixed the question, thank you. About `target_include_directories` – is it okay to include the whole project when all I need is a `project_name` directory? – Lev Jul 08 '20 at 09:21
  • "is it okay to include the whole project when all I need is a `project_name` directory?" - Generally, it is up to you what is OK or not in your project. But with current files structure you have no other way for make `#include "project_name/source_1.hpp` to work. But would I have the same purpose, I would think about moving `source_1.hpp` header into `project_name/include/project_name/source_1.hpp` and making `project_name/include/` as *include directory*. – Tsyvarev Jul 08 '20 at 10:46
  • 1
    Does this answer your question? [Header files with prefix in CMake project](https://stackoverflow.com/questions/38879819/header-files-with-prefix-in-cmake-project). As commented earlier, you can use `CMAKE_SOURCE_DIR` in the `include_directories()` command to add the project's root directory to the include directories, if you want to **prefix** your `#include` directives. – Kevin Jul 08 '20 at 13:11

0 Answers0