Library and include path


I am creating a library, and the associated test.

Here is the files structure :

── MyLibrary
   ├── CMakeLists.txt
   ├── include
   │   └── x.h
   ├── src
   │   ├── CMakeLists.txt
   │   ├── x.cpp
   │   ├── y.h
   │   └── y.cpp
   └── test
       ├── CMakeLists.txt
       ├── test.h
       └── test.cpp

and where is what I do in the CMakeLists.txt inside the tesy directory :

source_group(TREE . PREFIX Sources)


The problem is that in the test, if I want to include a.h, I just have to do :

#include <a.h>

I’d like to be able to do :

#include <MyLibrary/a.h>

I read several posts on StackOverflow telling to do :

── MyLibrary
   ├── CMakeLists.txt
   ├── include
   │   └── MyLibrary
   │       └── x.h

It’s not very clean, and when I am including a.h in the library, I have also to add MyLibrary in the path, where I am already in the library.

Would it be possible to alias the include directory so that





PS : I am not sure of the PUBLIC access in the commands. Let me know if I should change anything about this point.

The path in the #include is interpreted by the compiler. It searches in the filesystem for that path.
There’s nothing CMake or any other build system can do about that.

I’ve learned to use that pattern (include/MyLibrary) and now it seems pretty natural to me.
I set up the library target to use include and include/MyLibrary (typically the first is public/interface, and the second is private), and just roll with that.
It looked weird at first, but after all it seems to be the cleanest solution.

1 Like

OK, so I made up my mind to follow this “canonical way”. :slight_smile: