我正在运行 Ubuntu 14.04。
我重现的步骤:
创建一个新的 C++ 项目(新建 -> C++ -> Hello World 项目),我将其命名为
TestStdThread
将主文件中的代码更改为:
#include <thread> #include <iostream> int main() { std::cout << "You have " << std::thread::hardware_concurrency() << " cores." << std::endl; return 0; }
转到 TestStdThread -> Properties -> C/C++ Build -> Settings -> GCC C++ Compiler,并将 Command 选项从
g++
更改为g++ -std=c++11
转到 TestStdThread -> Properties -> C/C++ Build -> Settings -> GCC C++ Compiler -> Includes,将
/usr/include
添加到 Include 路径 ( -I),并将pthread.h
添加到Include files(-include)转到 TestStdThread -> Properties -> C/C++ Build -> Settings -> GCC C++ Linker -> Libraries,将
pthread
添加到 Libraries (-l),并将/usr/lib/x86_64-linux-gnu
添加到库搜索路径(-L)TestStdThread -> 构建项目
点击“运行”
没有构建错误。 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/