0

I've been assigned to completely run a project using CMake.

Basically, the project has over 20 modules, and for each module i created a CMake file such as:

# Module: CFS
file(
    GLOB_RECURSE
    files
    *.c
    *.cpp
)

include_directories("${PROJECT_SOURCE_DIR}/include/PEM/cfs")
include_directories("${PROJECT_SOURCE_DIR}/include/PEM/kernel2")
SET(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)
add_library(cfs ${files})

kernel2 is another module and has its own CMakeFile.

Now the problem is that a third module: m3 requires headers from cfs (which also require headers from kernel2)

So i basically go with:

# Module: m3
file( ... )
include_directories("${PROJECT_SOURCE_DIR}/include/PEM/cfs")
add_library(m3 ${files})
target_link_library(m3 cfs)

Unfortunately this is not enough, kernel2 included files won't be found when i compile unless I add:

include_directories("${PROJECT_SOURCE_DIR}/include/PEM/kernel2")

Am I doing it wrong? Perhaps I should also add include files using add_library directive?

1 Answer 1

1

If you have #include directives in cfs's headers, then you should use

include_directories("${PROJECT_SOURCE_DIR}/include/PEM/kernel2")

It's not the problem of CMake, but how C/C++ compiler works.

For example, you have following header in cfs:

#include "kernel2/someclass.h"

class SomeCfsClass
{
private:
    SomeKernelClass kernelObject;
}

Now if you wish to instantiate SomeCfsClass in your m3 module, the compiler should know it's size. But knowing it's size is not possible without knowning SomeKernelClass definition from kernel2/someclass.h.

This situation can be resolved by storing not the object, but pointer to it inside SomeCfsClass:

class SomeKernelClass; // forward declare SomeKernelClass

class SomeCfsClass
{
private:
    SomeKernelClass * kernelObject;
}

But of course, there are cases, when it's simply impossible to avoid including.

As an alternative, i can suggest to use relative paths in #include directives, but this solution is somewhat hackish.

Sign up to request clarification or add additional context in comments.

2 Comments

Either i explained it wrong or got it wrong. But since cfs compiles and outputs a library file, isn't there any way to tell m3 to use that library without including the kernel2 headers?
If cfs' headers includes kernel2 headers - no. If you wish, i can expand answer with explanation why is it so.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.