How to make a different target_include_directories for internal and external #includes for a header-only library in CMake?

1.9k views Asked by At

The directory structure for my header-only library is as follows:

proj/
    include/
        proj/
            file1.h
            file2.h
    CMakeLists.txt

Basically I would like to be able to do the following inside file1.h (and therefore anywhere within the project):

#include "file2.h"

But have to do the following in any external project:

#include "proj/file2.h"

Currently, my naive implementation of this is in CMakeLists.txt:

add_library( proj INTERFACE )
target_include_directories( proj INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/include )
target_include_directories( proj INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/include/proj )

To allow both internal and external files do be able to do both. This is not restrictive enough, however, and what I am looking for is something that would work equivalently to this:

add_library( proj INTERFACE )
target_include_directories( proj INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/include )
target_include_directories( proj PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include/proj )

But this obviously results in a CMake error because:

target_include_directories may only set INTERFACE properties on INTERFACE targets

Is it possible to do what I am trying to do? Thank you in advance for your help.

0

There are 0 answers