C++ CLion 不解析来自外部库的标头

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28449556/
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 20:59:24  来源:igfitidea点击:

CLion doesn't resolve headers from external library

c++header-filesclionheader-only

提问by gianluca

Some time ago I started a big header library in C++1x using XCode. The current layout of the library is () something like (partial output from ls -R sponf)

前段时间我使用 XCode 在 C++1x 中启动了一个大型头文件库。库的当前布局是 () 类似于(来自 的部分输出ls -R sponf

sponf/sponf:
ancestors        sponf.h                sponf_utilities.h
categories       sponf_children.h       utilities
children         sponf_macros.h           

sponf/sponf/ancestors:
function.h       meter.h        set.h                    simulation.h

sponf/sponf/categories:
free_space.h     prng.h         random_distribution.h    series.h

sponf/sponf/children:
distributions    histogram.h    random                   simulations
meters           numeric        series                   spaces

sponf/sponf/children/distributions:
arcsine_der.h    exponential.h
box_muller.h     uniform.h

sponf/sponf/children/meters:
accumulator.h    timer.h

#... other subdirs of 'children' ...

sponf/sponf/utilities:
common_math.h    limits.h       string_const.h

#... other directories ...

I wanted to port this project to CLion, which seems a really good IDE (based on the similar AndroidStudio IDE) but I'm getting some troubles.

我想将此项目移植到 CLion,这看起来是一个非常好的 IDE(基于类似的 AndroidStudio IDE),但我遇到了一些麻烦。

Small test program

小测试程序

I tried this small program as a test:

我尝试了这个小程序作为测试:

#include <iostream>
#include <sponf/sponf.h>

using namespace std;

int main() {
    using space = sponf::spaces::euclidean_free_space<double, 3>;
    sponf::simulations::random_walk<space> rw;

    rw.step(1);

    std::cout << rw.position.value << std::endl;

    return 0;
}

The program compiles and runs fine. However, CLion does not recognize the spacesnamespace (declared in one of the children files), nor the simulationsnamespace; they are both marked red and I cannot inspect their content, nor navigate to their definitions by ?-clicking, etc. etc...

该程序编译并运行良好。但是,CLion 无法识别spaces命名空间(在其中一个子文件中声明),也无法识别命名simulations空间;它们都被标记为红色,我无法检查它们的内容,也无法通过?单击等导航到它们的定义......

Relevant parts of the library

图书馆相关部分

Looking in "sponf.h"we find

看着"sponf.h"我们发现

#ifndef sponf_h
#define sponf_h

/* The classes below are exported */
#pragma GCC visibility push(default)

// include some of the standard library files
// ...

#include <Eigen/Eigen>

#include "sponf_macros.h"

#include "sponf_utilities.h"
#include "sponf_children.h"

#pragma GCC visibility pop

#endif

while in "sponf_children.h"(which is located at the top level, next to "sponf.h") we find

而在"sponf_children.h"(位于顶层,旁边"sponf.h")我们发现

#ifndef sponf_locp_sponf_children_h
#define sponf_locp_sponf_children_h

namespace sponf {

// include some of the children
// ...

#include "children/spaces/euclidean_free_space.h"
#include "children/simulations/random_walk.h"

// include remaining children
// ...

}

#endif

Each "child" header will then include its corresponding "ancestor" or "category" header (which defines the superclass of the "child" itself).

然后,每个“子”标头将包含其相应的“祖先”或“类别”标头(定义“子”本身的超类)。

The reaction of CLion

CLion的反应

Despite the autocompletition prediction, which easily finds all the subdirectories and the headers, all the include directives in this last file get marked red and ?-clicking on any of them leads to a popup message

尽管自动完成预测可以轻松找到所有子目录和标题,但最后一个文件中的所有包含指令都被标记为红色,并且?单击它们中的任何一个都会导致弹出消息

Cannot find declaration to go to

找不到要转到的声明

while the right ribbon of the editor signal many errors like

而编辑器的右侧功能区则表示许多错误,例如

',' or ) expected

) expected

Declarator expected

Expecting type

Missing ;

Unexpected symbol

',' 或 ) 预期

) 预期的

预期声明符

期待类型

丢失的 ;

意想不到的符号

which are not the same for each include statement (each generates from 2 to all of these errors).

每个包含语句都不相同(每个包含 2 到所有这些错误)。

On the other hand, CLion is perfectly able to find all Eigenheaders, which have pretty much the same structure!

另一方面,CLion 完全能够找到所有Eigen具有几乎相同结构的标题!

I have put both libs in /opt/local/includeand changed CMakeLists.txtaccordingly

我已经把两个库中/opt/local/include,并改变CMakeLists.txt相应的

cmake_minimum_required(VERSION 2.8.4)
project(sponf)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")

include_directories(/opt/local/include/sponf /opt/local/include/eigen3)

set(SOURCE_FILES main.cpp)
add_executable(sponf ${SOURCE_FILES})

Why can't CLion properly parse the project structure? XCode, after having included /opt/local/include/sponfand /opt/local/include/eigen3in the HEADER_SEARCH_PATHSenv. variable of the project, is able to find any header while compiling the same exact program.

为什么 CLion 不能正确解析项目结构?XCode中,已经包括后/opt/local/include/sponf/opt/local/include/eigen3HEADER_SEARCH_PATHSENV。项目的变量,能够在编译相同的程序时找到任何头文件。

Is there anything else I need to know? Am I doing it wrong or is it that CLion isn't that mature yet and this is just a sorry bug? This is my first approach to the CLion and the CMake toolchain, so any kind of information about it will be greatly appreciated!

还有什么我需要知道的吗?是我做错了还是 CLion 还没有那么成熟,这只是一个遗憾的错误?这是我对 CLion 和 CMake 工具链的第一个方法,因此将不胜感激任何有关它的信息!

Sorry for the very long question, I didn't manage to shrink it further... Thanks in advance guys, see you soon!

很抱歉这个很长的问题,我没有设法进一步缩小它......提前谢谢大家,很快再见!

回答by GPrathap

Here what I did in windows using cigwin64. I wanted to use Eigen library include in my project. Eigen library is places in /usr/include/eigen then edited CMakeLists.txt and add

这是我在 windows 中使用 cigwin64 所做的。我想在我的项目中使用 Eigen 库。Eigen 库位于 /usr/include/eigen 然后编辑 CMakeLists.txt 并添加

  include_directories("/usr/include/eigen") 

into it. Now CLion can find all source files in eigen lib. May be this what you wanted too.

进去。现在 CLion 可以在 eigen lib 中找到所有源文件。可能这也是你想要的。

回答by fire1

Downgrade to Clion 2016.1.4 fixes the problem

降级到 Clion 2016.1.4 修复了问题