c++cmake

Add prefix for target_include_directories


I have following project structure for stm32 library:

-lib
  -include
    -f0
    -f1
    -f4
CMakeLists.txt

CMakeLists.txt contains add_library command for each MCU family and target_include_directories for each library. target_include_directories adds f0/f1/f4 folder, so user can include headers by #include <timer.h, for example. But it's not good, because headers names can conflict with other project deps.

I can link whole include folder and add global timer.h and other with #ifdef F0 #include "f0/timer.h", but in this case user can see other families folder (for example, he uses F0, but see f1 and f4 folders).

My question: can I write target_include_directories and add fake prefix for files from directory? I want to include f0 folder but force user write #include <lib/timer.h>?

I think about reorganize my library, but it's seems so ugly.


Solution

  • can I write target_include_directories and add fake prefix for files from directory?

    No. The directories <anything> is mapped 1:1 to directory structure. If you want to add a prefix, you have to copy files into a directory. You can copy them in CMake and add that copied directory to include path if you want, but note it makes working on the project confusing.

    Consider refactoring your project and putting platform specific includes in separate directories. Typically:

    lib/
    ├── f0
    │   └── include
    │       └── lib
    ├── f1
    │   └── include
    │       └── lib
    └── f4
        └── include
            └── lib