【问题标题】:CLion doesn't resolve headers from external libraryCLion 不解析来自外部库的标头
【发布时间】:2015-02-11 08:20:42
【问题描述】:

前段时间,我使用 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 ...

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

小测试程序

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

#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;
}

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

库的相关部分

查看"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

"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

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

CLion的反应

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

找不到要前往的声明

虽然编辑器的右侧功能区会显示许多错误,例如

',' 或 ) 预期

) 预期

需要声明符

期望类型

缺少;

意外符号

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

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

我已将两个库放入 /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})

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

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

抱歉,这个问题太长了,我没能把它进一步缩小...在此先感谢各位,很快再见!

【问题讨论】:

  • 请记住,CLion 仍处于 EAP 阶段。可能是软件的当前状态有解决方案,但鉴于"the quality of EAP versions may at times be way below even usual beta standards",这可能只是一个错误。尝试向 Jetbrains 报告?
  • 您应该考虑删除此问题,因为它与非发行版软件中的临时错误有关。
  • @xaxxon 该问题已被标记为版主注意,因为我自己无法删除已回答的问题。感谢您的意见!
  • File->Reload CMake project 为我做了。

标签: c++ header-files clion header-only


【解决方案1】:

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

  include_directories("/usr/include/eigen") 

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

【讨论】:

  • 是的,就是这样。这个问题可能是他在评论中描述的一个亚历克斯。他认为这个问题可能只是与 CLion 的实际开发阶段有关。我下载了最新版本,问题就没有了。
  • 我添加了 include_directories,但它不起作用。我需要将外部头目录添加到项目设置中。
  • 外部头目录是什么意思?
【解决方案2】:

降级到 Clion 2016.1.4 解决了这个问题

【讨论】:

    猜你喜欢
    • 2018-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多