Header files outside root won't show up in External Sources

Answered

I'm working on a project that is structured this way:

MyProject/
CMakeLists.txt
Source/
SourceFile1.cpp
SourceFile2.h
...
MyStaticLibrary/
LibrarySourceFile1.h
  LibrarySourceFile2.cpp
  LibrarySourceFile2.h
...

"MyProject" is the main project, and "MyStaticLibrary" is a collection of .h/.cpp files that I include in the sources of "MyProject" and other projects. Some of the sources of "MyStaticLibrary" are header-only (just .h file). I don't want to move "MyStaticLibrary" into a subfolder of "MyProject".

To add the sources of "MyStaticLibrary" into "MyProject", I added the following to my CMakeLists.txt:

As you can see, "External Sources" shows only the .cpp file, and the .h files (array_helpers.h, dr_wav.h, piano.h, string_hammer.h) are absent. If I access these header files from CLion, I get the message "This file does not belong to any project target; code insight features might not work correctly". I'm able to compile the project, nevertheless this is very annoying. Another IDE (QtCreator) is able to show these header files as source, using the same exact CMakeLists.txt.

 

Any suggestions?

 

EDIT: following this guide, I've tried adding the include_directories() command. This has the effect of adding the library directory in "External Libraries", but the header files are still missing:

7 comments
Comment actions Permalink

Hello!

Do I understand correctly that OpenPianoCore itself is a CMake project (with it's own CMakeLists.txt) too?

0
Comment actions Permalink

Hello Anna! Yes, OpenPianoCore can be also built as a standalone - its main.cpp contains some tests that I use to benchmark the library. So the CMakeLists.txt contained in OpenPianoCore is not meant for building OpenPianoCore as a library. It is completely unrelated to the other project, OpenPiano.

 

EDIT #1: Just to let you know, I tried removing the CMakeLists.txt contained in OpenPianoCore and reloading the project. It made no difference.

 

EDIT #2: Following the workaround showed here, I modified the misc.xml by adding two lines into related to OpenPianoCore (one in sourceRoots and a second one in excludeRoots). Still, I don't think this is a good solution.

<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
  <component name="CMakeWorkspace" IGNORE_OUTSIDE_FILES="true" PROJECT_DIR="$PROJECT_DIR$" />
  <component name="CidrRootsConfiguration">
    <sourceRoots>
      <file path="$PROJECT_DIR$/../OpenPianoCore" />
      <file path="$PROJECT_DIR$/Source" />
    </sourceRoots>
    <libraryRoots>
      <file path="$PROJECT_DIR$/JUCE" />
    </libraryRoots>
    <excludeRoots>
      <file path="$PROJECT_DIR$/../OpenPianoCore/Builds" />
      <file path="$PROJECT_DIR$/Builds" />
    </excludeRoots>
  </component>
</project>

 

EDIT #3: There seems to be an open issue about this. I'm a bit dazzled as to why we need to apply workarounds to something that seems quite ordinary to me (libraries that aren't necessarily in sub-directories). I also think that changing the project root is out of the question.

Apart from the open issues on youtrack, I still don't understand why "External Sources" would show only .cpp files and ignore header files. This is particularly annoying in the case of header-only libraries...

0
Comment actions Permalink

Anna Falevskaya so there's no solution for this?

0
Comment actions Permalink

Indeed, the External Sources node shows only source files, not headers. That's by design. If you don't like this behavior, feel free to create an issue in the tracker https://youtrack.jetbrains.com/issues/CPP. 

This has the effect of adding the library directory in "External Libraries", but the header files are still missing

As far as I can see, in your screenshot the headers are correctly shown under External Libraries > Header Search Paths > OpenPianoCore. 

0
Comment actions Permalink

Anna Falevskaya thanks for your response. Who would like this behavior? I don't really understand this choice. If I, for example, write a header-only C++ class or library, why couldn't that be considered as a source? This makes no sense. Yes, I will open an issue in the tracker.

No,  the headers are not correctly shown - CLion doesn't recognize them as part of the project, even thought they are included in CMake. If I open them, I get the message "This file does not belong to any project target; code insight features might not work correctly".

Until this is resolved, I can't really use CLion for my projects.

0
Comment actions Permalink

This is how the same project looks like in QtCreator:

 

0
Comment actions Permalink

If I open them, I get the message "This file does not belong to any project target; code insight features might not work correctly".

We've changed this behavior in CLion 2022.1 EAP, this warning is not shown in cases like yours. Feel free to try it https://www.jetbrains.com/clion/nextversion/.

0

Please sign in to leave a comment.