I'm using cmake to compile a project with the following structure:

Root

  • LibA
    • inc
    • src
  • LibB
    • inc
    • src
  • main.cpp

I have CMakeLists on each LibX, each src and Root folders.

My project is compiling and working as expected. But I would like to include headers as: include "{LibX}/{header}.h" instead of just "{header}.h" or instead of putting the absolute/relative path to the header.

Since I'm setting all the include directories in CMakeLists, I can simply include a header with its name, but that's not really the best solution.

For example, my main.cpp depends on LibA and LibB, and LibA depends on LibB, which is a math library. It would be good to know from which library I'm including a header every time.

Thank you

有帮助吗?

解决方案

Root/LibA/inc/LibA/MyHeaderFileFromA.h

#include <LibA/MyHeaderFileFromA.h>

there you go

其他提示

Promoting a comment to an answer to make it easier for others to find:

#include needs to match the path. You could create an extra folder under Root, called includes, with soft (or hard) links to LibA/inc (call the link LibA) and LibB/inc (call the link LibB). Then set Root/includes as one of the paths searched for header files.

Root

  • LibA
    • inc
    • src
  • LibB
    • inc
    • src
  • includes
    • LibA (links to Root/LibA/inc)
    • LibB (links to Root/LibB/inc)
  • main.cpp
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top