Library and include path

Hello,

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 :

target_sources(${LIB_PROJECT_NAME}
               PUBLIC
                   test.h
                   test.cpp
			   PUBLIC
				   ../include/x.h)
				   
source_group(TREE . PREFIX Sources)

target_include_directories(${TEST_PROJECT_NAME}
                           PUBLIC
						       ${CMAKE_CURRENT_SOURCE_DIR}
							   ${LIB_PROJECT_NAME}/include/)

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

${LIB_PROJECT_NAME}/include/

becomes

MyLibrary/

?

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:
Thanks.