CLion indexer does not resolve some includes in the project directory
Asked Answered
H

3

14

I have a CLion C++ project that has the following structure:

    project
       ---->my_includes
       |   ----> my_own.hpp
       ---->source
           ----> my_app
               ----> my_src.cpp

The first line of my_src.cpp is

#include "my_includes/my_own.hpp"

I use an external build system that requires this inclusion format. The problem is if I use a function in my source file defined in the included header, CLion says "Cannot find my_own.hpp" if I try to hover over the inclusion.

I tried marking the include directory as containing Project Source or Headers but this didn't fix it. Any ideas?

Hardpan answered 23/3, 2016 at 22:28 Comment(1)
Post your CMakeLists.txtLefton
J
30

You need to create a CMakeLists.txt for CLion to be happy. It is enough to declare all the source files, you don't have to convert your scons (or any other build system) to cmake.

You don't even have to write the CMakeLists.txt by hand, you can ask CLion to do it:

  • File | New CMake Project from Sources... (since CLion 2019.2)
  • File | Import project ... | (older CLion)

and then point at the directory containing your project.

Now edit the generated CMakeLists.txt and add a cmake command to tell CLion where to find the includes (actually to tell the compiler, and CLion will reuse that information).

Since your source files use the include as #include "my_includes/my_own.hpp", you need to tell cmake the base directory containing directory my_includes:

include_directories(.)

Where the dot means the same directory as the one containing the CMakeLists.txt.

I tested with a project reproducing your layout and from my_src.cpp I can navigate to my_own.hpp.

Then to build you still have to use scons in a console. It is also possible to add a cmake command, add_custom_target() that will call your scons (or your make, or whatever), so that you can also navigate from CLion to the build errors.

Jansson answered 24/3, 2016 at 19:1 Comment(3)
I had a similar issue, include_directories was what I needed. I had to add a few different ones, but it works great now.Moneybags
as of CLion 2019.2, this option is called File | New CMake Project from Sources...Scarlet
Possibly a build is needed before the included directories will be synced and recognized. I was able to navigate to those headers only after the build.Giglio
T
1

And for MakeFile + gcc (g++) projects, you can add the flag -I /Dir/To/Your/Project.

If CLion still shows errors with #include after recompiling the make file, delete the .idea folder and restart CLion.

Tris answered 19/7, 2022 at 16:22 Comment(0)
T
0

This should be a CMake-based project to open correctly in CLion. Check CMake basics tutorial if you are new to CMake: https://www.jetbrains.com/help/clion/2016.1/quick-cmake-tutorial.html

Turtleback answered 24/3, 2016 at 9:24 Comment(3)
So even though I am not using CMake for my build (only SCONS), having a CMake project is the only way CLion will know where to look for certain file?Hardpan
Yes, so if we talk about how CLion uses CMake, let's pick just a couple of samples: 1. When code generation is working, we check the feature availability in the compiler, like the override directive - CLion will suggest you this tick only in case the compiler, with all the options set in your CMake, supports it. That means C++11 at least, and the compiler version should support it of course. 2. Rename refactoring, it checks the real context usages, not the text usages.Turtleback
To be able to do so, CLion needs to know quite a lot about your code. And it depends on many things: source files of the project, headers included and headers search paths, compiler flags, toolchain and platform used, compiler predefined macros, and some more. So CLion analyses your CMake project model to get all this information out of there, and then uses it for parsing, resolving and other features.Turtleback

© 2022 - 2024 — McMap. All rights reserved.