0

I have a project with following structure:

MyProject/
├─ CMakeLists.txt (A)
├─ foo.h
├─ ThirdParty/
│  ├─ CMakeLists.txt (B)
│  ├─ bar.h
│  ├─ ThirdPartySourceOne/
│  │  ├─ CMakeLists.txt (C)
│  │  ├─ boo.h
│  ├─ ThirdPartySourceTwo/
│  │  ├─ CMakeLists.txt (D)
│  │  ├─ far.h

CMakeLists A "includes" CMakeLists B by

add_subdirectory(ThirdParty)

CMakeLists B "includes" CMakeLists C and D

add_subdirectory(ThirdPartySourceOne)
add_subdirectory(ThirdPartySourceTwo)

far.h includes boo.h

When I build it like this, the whole project builds fine (including the third party source).

But when I add #include ThirdParty/ThirdPartySourceTwo/far.h to foo.h I get an error from the far.h along the lines of:

 fatal error: ThirdPartySourceOne/boo.h: No such file or directory
   25 | #include <ThirdPartySourceOne/boo.h>
      |          ^~~~~~~~~~~~~~~~

So how do I include the Third Party code correctly? Thank you in advance.

  • I tried to search for a similar problem on Google
  • I tried include(ThirdParty/CMakeLists.txt) which caused the (untouched) thirdparty Library's CMakeLists(B) not to find the subdirectories anymore:
CMakeLists.txt:19: error: add_subdirectory given source "ThirdPartySourceOne" which is not an existing directory. CMakeLists.txt:17 (include)
  • Trial and error with Paths in CMakeLists(B) that just broke more further down the road
  • " I get an error from the `far.h` along the lines of ..." - Plese, post **exact** and **complete** error message. In the current form it is unclear which tool emits the error (CMake, compiler or some other program) and why. Also, showing only `add_subdirectory` lines is not sufficient to understand your code. For make the projects to successfully compiled, you should somehow specify an include directories. We need to see that exact specifications. – Tsyvarev Dec 26 '22 at 13:30
  • The Error comes from the compiler. I have edited the post for the error message. Also, I literally didnt add nothing more to alreasy existing code than the one `add_subdirectory()` in `CMakeLists.txt (A)` Do I need to specify all the includepaths there as well? I was hoping `CMakeLists.txt(B)` would do that since it compiles fine – Bl4ckmedia Dec 26 '22 at 13:41
  • You say "`far.h` includes `boo.h`". This is not precise enough. You should show the exact `#include` line how it does that. – j6t Dec 26 '22 at 14:00
  • I just added `include_directories(ThirdParty)` to `CMakeLists.txt (A)`and solves some issues. The original project is a little more nested so I will add more and more folders by trial and error until it works. When there is a better way to do this (this seems Janky), feel free too enlighten me – Bl4ckmedia Dec 26 '22 at 14:16
  • Instead of `include_directories` it is better to use `target_include_directories`, which "binds" include directories for a specific *target* and for every target which links with it. That way hierarchical projects are easy to maintain. See [duplicate question](https://stackoverflow.com/questions/13703647/how-to-properly-add-include-directories-with-cmake) for more info. – Tsyvarev Dec 26 '22 at 14:18

0 Answers0