【问题标题】:Eclipse content assist doesn't recognize std::thread, but compiles correctlyEclipse 内容辅助无法识别 std::thread,但可以正确编译
【发布时间】:2014-09-10 04:28:41
【问题描述】:

我正在运行 Ubuntu 14.04。

我复制的步骤:

  1. 新建一个 C++ 项目(New -> C++ -> Hello World 项目),我称之为TestStdThread

  2. 将主文件中的代码改成这样:

    #include <thread>
    #include <iostream>
    
    int main() {
        std::cout << "You have " << std::thread::hardware_concurrency() << " cores." << std::endl;
        return 0;
    }
    
  3. 转到 TestStdThread -> Properties -> C/C++ Build -> Settings -> GCC C++ Compiler,并将 Command 选项从 g++ 更改为 g++ -std=c++11

  4. 转到TestStdThread -> Properties -> C/C++ Build -> Settings -> GCC C++ Compiler -> Includes,将/usr/include添加到Include paths (-I),并将pthread.h 添加到包含文件(-include)

  5. 转到 TestStdThread -> Properties -> C/C++ Build -> Settings -> GCC C++ Linker -> Libraries,将 pthread 添加到 Libraries (-l),然后将/usr/lib/x86_64-linux-gnu 添加到库搜索路径 (-L)

  6. TestStdThread -> 构建项目

  7. 点击“运行”

没有构建错误。 Eclipse 告诉我该项目有错误并询问我是否要运行它,当我说是时,输出正确:You have 4 cores.。但是,Eclipse 仍然用红色强调了std::thread::hardware_concurrency 部分,并将其报告为(悬停时)“无法解析函数'hardware_concurrency'”,并且在键入std:: Ctrl 时std::thread 没有出现+空格

这是我用来查找我的 pthread 文件在 /usr 中的位置的 bash 命令(/usr/share 被省略,因为它包含很多我不需要的 doc 文件):

llama@llama-Satellite-E55-A:/usr$ find -name "*pthread*" -not -path "./share/*"
./include/pthread.h
./include/x86_64-linux-gnu/bits/pthreadtypes.h
./lib/x86_64-linux-gnu/pkgconfig/pthread-stubs.pc
./lib/x86_64-linux-gnu/libpthread.so
./lib/x86_64-linux-gnu/libpthread_nonshared.a
./lib/x86_64-linux-gnu/libgpgme-pthread.so.11.11.0
./lib/x86_64-linux-gnu/libgpgme-pthread.so.11
./lib/x86_64-linux-gnu/libpthread.a
./lib/perl/5.18.2/bits/pthreadtypes.ph
./lib/debug/lib/x86_64-linux-gnu/libpthread-2.19.so

【问题讨论】:

  • #include &lt;thread&gt; 也许?
  • @vsoftco 哎呀,这是一个复制/粘贴错误。当然,我的原始代码中确实有这个;)
  • 尝试重新索引项目。
  • @vsoftco 试过了;还是不行。
  • 在我这边工作 (OS X gcc 4.9),没有红色标记,但是我在 Project Properties/C++ Build/Settings/GCC C++ Compiler/Miscellaneous (Other flags) 下添加了 -std=c++11 标志),但我想这应该没什么区别。

标签: c++ eclipse ubuntu eclipse-cdt stdthread


【解决方案1】:

转到Project -> Properties -> C/C++ General -> Preprocessor include paths, etc -> Providers -> CDT GCC Builtin Compiler Settings 并将-std=c++11 附加到编译器规范中。

您也可以对所有转到 Window -> Preferences -> C/C++ -> Build -> Settings -> Discovery 的项目执行此操作,并将 -std=c++11 附加到 @987654335 @规格。

确保之后重新索引您的项目。

这些说明适用于 Eclipse Luna (4.4.0),对于以前的版本,路径类似。

【讨论】:

  • 重新索引是否意味着重建?
猜你喜欢
  • 2013-07-05
  • 2011-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-11
  • 1970-01-01
  • 2012-05-29
相关资源
最近更新 更多