Doorknob Doorknob - 9 months ago 44
C++ Question

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

I am running Ubuntu 14.04.

Steps I took to reproduce:

  1. Create a new C++ project (New -> C++ -> Hello World project), which I called

  2. Change the code in the main file to this:

    #include <thread>
    #include <iostream>

    int main() {
    std::cout << "You have " << std::thread::hardware_concurrency() << " cores." << std::endl;
    return 0;

  3. Go to TestStdThread -> Properties -> C/C++ Build -> Settings -> GCC C++ Compiler, and change the Command options from
    g++ -std=c++11

  4. Go to TestStdThread -> Properties -> C/C++ Build -> Settings -> GCC C++ Compiler -> Includes, add
    to the Include paths (-I), and add
    to the Include files (-include)

  5. Go to TestStdThread -> Properties -> C/C++ Build -> Settings -> GCC C++ Linker -> Libraries, add
    to the Libraries (-l), and add
    to the Library search path (-L)

  6. TestStdThread -> Build Project

  7. Click "Run"

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
part in red, and reported it (on hover) as "Function 'hardware_concurrency' could not be resolved," and
didn't show up when typing

This is the bash command I used to find where my
files were located within
omitted as it contains lots of doc files that I'm not looking for):

llama@llama-Satellite-E55-A:/usr$ find -name "*pthread*" -not -path "./share/*"

Answer Source

Go to Project -> Properties -> C/C++ General -> Preprocessor include paths, etc -> Providers -> CDT GCC Builtin Compiler Settings and append -std=c++11 to the compiler specs.

You can also do this for all projects going to Window -> Preferences -> C/C++ -> Build -> Settings -> Discovery and append -std=c++11 to the CDT GCC Builtin Compiler Settings specs.

Make sure to reindex your project afterwards.

These instructions are for Eclipse Luna (4.4.0), for previous versions the paths are similar.