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

2024-01-21

我运行的是 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++ to g++ -std=c++11

  4. 转到 TestStdThread -> 属性 -> C/C++ 构建 -> 设置 -> GCC C++ 编译器 -> 包含,添加/usr/include to the 包含路径 (-I),并添加pthread.h to the 包含文件 (-include)

  5. 转到 TestStdThread -> 属性 -> C/C++ Build -> 设置 -> GCC C++ Linker -> Libraries,添加pthread to the 库 (-l),并添加/usr/lib/x86_64-linux-gnu to the 库搜索路径 (-L)

  6. TestStdThread -> 构建项目

  7. 单击“运行”

There were no build errors. Eclipse told me that the project had errors and asked if I wanted to run it anyway, and when I said yes, the output was, correctly: You have 4 cores.. However, Eclipse still underlined the std::thread::hardware_concurrency part in red, and reported it (on hover) as "Function 'hardware_concurrency' could not be resolved," and std::thread didn't show up when typing std:: Ctrl+Space.

这是我用来查找我的位置的 bash 命令pthread文件位于/usr (/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

Go to 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 to the CDT GCC Builtin Compiler Settings specs.

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

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Eclipse 内容辅助无法识别 std::thread,但可以正确编译 的相关文章

随机推荐