c++ - Eclipse 内容辅助无法识别 std::thread,但可以正确编译

标签 c++ eclipse ubuntu eclipse-cdt stdthread

我正在运行 Ubuntu 14.04。

我重现的步骤:

  1. 创建一个新的 C++ 项目(新建 -> 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 路径 ( -I),并将pthread.h添加到Include files(-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::thread 在键入 std:: Ctrl+Space 时没有出现。

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

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

最佳答案

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

您还可以对所有转到 Window 的项目执行此操作 -> Preferences -> C/C++ -> Build -> Settings -> Discovery 并将 -std=c++11 附加到 CDT GCC Builtin Compiler Settings 规范。

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

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

关于c++ - Eclipse 内容辅助无法识别 std::thread,但可以正确编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24835081/

相关文章:

java - 使用 jUnit 在 Eclipse 中中断异常

python - 运行带参数的 python 脚本

php - 缺少 json 扩展名。请检查您的 PHP 配置

c++ - 为什么要这样声明宏?

C++:如何专门针对左值和右值

c++ - 如何使用mmap和shm_open在多个独立进程之间共享内存

linux - Eclipse 不使用 .bashrc 中设置的路径

c++ - 理解给定的位反转片段

java - Eclipse 在文件夹中找不到我的项目

ubuntu - 如何在ubuntu上编译这段代码?