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

Eclipse content assist doesn't recognize std::thread, but compiles correctly

本文关键字:thread 编译 std 识别 Eclipse      更新时间:2023-10-16

我使用的是Ubuntu 14.04。

复制步骤:

  1. 创建一个新的c++项目(new -> c++ -> Hello World project),我称之为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文件(- Include)

  5. 进入TestStdThread -> Properties -> C/c++ Build -> Settings -> GCC c++ Linker -> Libraries,将pthread添加到Libraries (-l),并将/usr/lib/x86_64-linux-gnu添加到Library搜索路径(-l)

  6. TestStdThread -> Build Project

  7. 点击"运行"

没有构建错误。Eclipse告诉我项目有错误,并询问我是否要运行它,当我说是时,输出是正确的:You have 4 cores.。然而,Eclipse仍然用红色标注std::thread::hardware_concurrency部分,并报告它为"Function 'hardware_concurrency'无法解析",并且当输入std:: Ctrl+Space时,std::thread没有显示。

这是我用来找到我的pthread文件位于/usr (/usr/share省略,因为它包含了很多我不寻找的文档文件)的bash命令:

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 Builtin Compiler Settings,并将-std=c++11添加到编译器规范中。

您也可以为所有项目执行Window -> Preferences -> C/C++ -> Build -> Settings -> Discovery,并将-std=c++11附加到CDT GCC Builtin Compiler Settings规范。

一定要在之后重新索引你的项目。

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