C ++ - Eclipse - 组织包含

时间:2016-06-01 19:24:03

标签: c++ eclipse-cdt

我已在ExecuterExecuter.hpp中定义了一个分类Executer.cpp

Executer.hpp中有代码:

static std::unique_ptr<Executer> mInstance;
static std::once_flag mOnceFlag;

Executer.cpp中有代码:

std::unique_ptr<Executer> Executer::mInstance;
std::once_flag Executer::mOnceFlag; // without this apparently
    // useless line of code, the program using this shared lib
    // claims: undefined reference to `Executer::mOnceFlag'

之后我尝试让eclipse组织我的导入。

我得到的是:

//------------------------------- Executer.hpp
#include <memory>
#include <stdexcept>
#include <string>

namespace std {
struct once_flag;
} /* namespace std */
//------------------------------- Executer.cpp
#include "Executer.hpp"    
#include <mutex>

我期望(并且编译正确):

//------------------------------- Executer.hpp
#include <memory>
#include <mutex>
#include <stdexcept>
#include <string>
//------------------------------- Executer.cpp    
#include "Executer.hpp"    
  1. 为什么Eclipse会以这种方式表现?
  2. 我能否以这样的方式配置Eclipse,以便以我的方式组织包含(我已经看到很多选项,但我的方法最糟糕)

1 个答案:

答案 0 :(得分:0)

  

为什么Eclipse会以这种方式运行?

静态数据成员的类型不需要在数据成员声明时完成。也就是说,声明将使用刚刚前向声明的类型进行编译,而不是已定义。

“组织包含”的默认设置尝试最小化其他标头中包含的标头数量,以加快编译时间,因此当类型的前向声明足够时,它更喜欢创建一个而不是包括标题而不是定义类型。

  

我是否可以通过这样的方式配置Eclipse,以便我能够组织包含(我已经看到了很多选项,但是我的方法最糟糕)

取消选中Forward declare classes, structs and unions中的Preferences | C/C++ | Code Style | Organize Includes可以为您提供所需的行为。