Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CLion indexer does not resolve some includes in the project directory

Tags:

c++

clion

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?

like image 538
CPayne Avatar asked Mar 23 '16 22:03

CPayne


People also ask

How do I add a header file to CLion?

Create a new C/C++ header In the Project tool window, select the directory in which you want to add new files. Right-click it and select New | C/C++ Header File from the context menu. In the dialog that opens: Specify the name of a file.

How add include path CMake?

First, you use include_directories() to tell CMake to add the directory as -I to the compilation command line. Second, you list the headers in your add_executable() or add_library() call.

Where does CLion store project files?

Project root directory Any project in CLion should be encapsulated within the project directory, referred to as the project root. It contains all the project files and subdirectories, including configuration, data, source, and other files related to the project.


1 Answers

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.

like image 178
marco.m Avatar answered Sep 18 '22 15:09

marco.m