Threads in Eclipse AND c++11

2020-07-18 11:40发布

问题:

My goal has been to create multi-threading programs, and I can not even get a simple thread to execute ON ECLIPSE CDT. my Tools: ECLIPSE 3.8.1 CDT Ubuntu 13.10

I have noticed very similar issues regarding mine. I have tried those other solutions but I could not get them to work for me.

When I type the code in Eclipse CDT, Eclipse does not 'resolve' the symbols 'thread', however, It can find the header file 'thread'. 'Mutex' also does not resolve. Furthermore, after building, I run the program, eclipse returns :

"terminate called after throwing an instance of 'std::system_error' what(): Enable multithreading to use std::thread: Operation not permitted"

Some additional notes:

I can compile and execute the code in the terminal using: 'clang++ c.cpp -pthread -std=c++11' but... 'g++ c.cpp -pthread -std=c++11' compiles and produces the same error as quoted above. So it looks like it's a compiler issue. I did start to write the code in a new project within Eclipse CDT with the clang++ compiler and now that gives the same non-resolved 'thread' and produces the error as quoted above. So now I think I have some wrong settings, paths or flags set in Eclipse.

include <iostream>
include <thread>

using namespace std;
void p1(){
    cout<<"process 1 is processing"<<endl;
}

int main() {
    thread t1(&p1);

    cout<<"Hello from main()"<<endl;
    t1.join();
    return 0;
}

回答1:

I have been struggling with the very same issue and I finally resolved it. Here is what I did:

1) Add -std=c++11 for c++ build. To do that right-click your project, select properties and then: C/C++ Build -> Settings -> GCC C++ Compiler -> Miscellaneous(last option)

In other flags append -std=c++11. My Other flags now looks like: -c -fmessage-length=0 -std=c++11 but yours may be a bit different.

2) Add some linker options. In the same view(C/C++ Build -> Settings) as above select the GCC C++ Linker option and from there go to Miscellaneous(second to last option). Add the following Linker flags(the field was empty for me): -Wl,--no-as-needed -pthread. Hit apply.

3) Add a macro. Again from the project properties menu(project->right click->properties). Navigate to C/C++ General -> Paths and symbols -> Symbols. Select GNU C++. Add a symbol with the name __GXX_EXPERIMENTAL_CXX0X__ and no value. Again hit apply.

4) Navigate to C/C++ General -> Preprocessor Include paths.. Select the providers tab. In this tab leave only the following two options checked: CDT GCC Built-in Compiler Settings and CDT Managed Build Setting Entries. Select CDT GCC Built-in Compiler Settings uncheck the checkbox Share setting entries between projects(global provider) and now the text box labeled Command to get compiler specs should be enabled. In this text box append the good old -std=c++11. The text now looks like this for me ${COMMAND} -E -P -v -dD ${INPUTS} -std=c++11. Hit apply one last time.

5) Rebuild the index for the project. To do that right click the project->Index->Rebuild

Following these steps I was able to compile a c++11 multithreaded program, execute it and also Eclipse CDT did not report any errors and was helpful with the autocompletion. Unfortunately this setting has to be done separately for Release and Debug(or at least I have not found a way to share it). Hope this helps.