C++ CLion 索引器无法解析项目目录中的某些包含

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36189910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:35:30  来源:igfitidea点击:

CLion indexer does not resolve some includes in the project directory

c++clion

提问by CPayne

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

我有一个具有以下结构的 CLion C++ 项目:

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

The first line of my_src.cpp is

my_src.cpp 的第一行是

#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.

我使用需要这种包含格式的外部构建系统。问题是如果我在包含的标头中定义的源文件中使用一个函数,如果我尝试将鼠标悬停在包含的内容上,CLion 会说“找不到 my_own.hpp”。

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

我尝试将包含目录标记为包含项目源或标题,但这并没有解决它。有任何想法吗?

回答by marco.m

You need to create a CMakeLists.txtfor 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.

你需要CMakeLists.txt为 CLion创建一个快乐。声明所有源文件就足够了,您不必将 scon(或任何其他构建系统)转换为 cmake。

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

你甚至不必手工编写 CMakeLists.txt,你可以让 CLion 来做:

  • File | New CMake Project from Sources...(since CLion 2019.2)
  • File | Import project ... |(older CLion)
  • File | New CMake Project from Sources...(自 CLion 2019.2 起)
  • File | Import project ... |(较旧的 CLion)

and then point at the directory containing your project.

然后指向包含您的项目的目录。

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

现在编辑生成的CMakeLists.txt并添加一个 cmake 命令来告诉 CLion 在哪里可以找到包含(实际上是告诉编译器,CLion 将重用该信息)。

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 as #include "my_includes/my_own.hpp",您需要告诉 cmake 包含目录的基本目录my_includes

include_directories(.)

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

其中点表示与包含CMakeLists.txt.

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

我测试了一个重现您的布局的项目,然后my_src.cpp我可以导航到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.

然后要构建,您仍然必须在控制台中使用 scons。还可以添加一个 cmake 命令,add_custom_target()该命令将调用您的 scons(或您的 make,或其他),以便您还可以从 CLion 导航到构建错误。

回答by nastasiak2512

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

这应该是一个基于 CMake 的项目,可以在 CLion 中正确打开。如果您是 CMake 新手,请查看 CMake 基础教程:https: //www.jetbrains.com/help/clion/2016.1/quick-cmake-tutorial.html