How to make CLion understand includes in header-only template library?


I have a project, which is a header-only template library.

The folder structure is as follows:


The header files in the folder includes/ contain the template definitions. The implementation of the templates happens in .cpp files, which are located in the src folder. Finally, a header file includes the cpp file with the implementation. So this is not standard way that the source file includes the header file, but the other way around.

The folder examples contains cpp files, which use the library. They just have to include MyMainHeader.h from the includes folder.

The simplified CMakeLists.txt basically looks as follows:

cmake_minimum_required(VERSION 3.3)
add_executable(MyExecutable ${CMAKE_CURRENT_SOURCE_DIR}/examples/MyExample.cpp)
target_include_directories(MyExecutable PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)

This compiles fine, but the problem is that CLion does not understand that the library cpp files are actually included in the header files.
Therefore, I get lots of errors because namespaces, types etc., which are defined in the header files are not recognized in the source files. CLion suggests to include the headers in the source files, but that is not what I want.

I already tried to add the files explicitly to the executable, to create a separate custom target, and to mark the folders as project folders. Unfortunately, that does not work.

Therefore the question: Is there a way to make a source file aware of the types in its header file (where it is included), without including the header file in the source file?

Thank you!

1 comment
Comment actions Permalink

Hi Fabian.

For now CLion in general can't process a file in context of its inclusion. We have an issue in our tracker: Feel free to comment or upvote. Is it possible for you to add the header file on top of the corresponding .cpp as a workaround? It will be eliminated by preprocessor anyways.


Please sign in to leave a comment.