Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » C / C++ IDE (CDT) » Using C++11 and threads - linker can't resolve pthread library
Using C++11 and threads - linker can't resolve pthread library [message #1727282] Mon, 21 March 2016 17:18 Go to next message
David Aldrich is currently offline David AldrichFriend
Messages: 13
Registered: July 2009
Junior Member
Hi

I am running Eclipse Mars on Fedora 23.

I have written a tiny program that uses the C++11 threads class.

#include <iostream>
#include <thread>

using namespace std;

// This function will be called from a thread

void call_from_thread()
{
	cout << "!!!Hello World!!!" << endl;
}

int main()
{
	// Launch a thread
	std::thread t1(call_from_thread);

	// Join the thread with main thread
	t1.join();


	return 0;
}


I've configured the project to use C++11 dialect as described here:
https://wiki.eclipse.org/CDT/User/FAQ#CDT_does_not_recognize_C.2B.2B11_features

I also tried the "-std=c++11" compiler spec.

After re-indexing there are no indexer or compiler errors but at runtime I get a linker error:

16:59:36 **** Build of configuration Debug for project solarion_tutorial ****
make all 
Building file: ../src/solarion_tutorial.cpp
Invoking: GCC C++ Compiler
g++ -std=c++0x -I"-l pthread" -O0 -g3 -Wall -c -fmessage-length=0 -std=c++11 -MMD -MP -MF"src/solarion_tutorial.d" -MT"src/solarion_tutorial.o" -o "src/solarion_tutorial.o" "../src/solarion_tutorial.cpp"
Finished building: ../src/solarion_tutorial.cpp
 
Building target: solarion_tutorial
Invoking: GCC C++ Linker
g++  -o "solarion_tutorial"  ./src/solarion_tutorial.o   
./src/solarion_tutorial.o: In function `std::thread::thread<void (&)()>(void (&)())':
/usr/include/c++/5.3.1/thread:137: undefined reference to `pthread_create'


I've tried adding "-l pthread" as an include path but that didn't fix it. But I guess I shouldn't need to do that if I am using C++11?

Can anyone help please?

David
Re: Using C++11 and threads - linker can't resolve pthread library [message #1727380 is a reply to message #1727282] Tue, 22 March 2016 14:35 Go to previous message
David Aldrich is currently offline David AldrichFriend
Messages: 13
Registered: July 2009
Junior Member
Fixed:

In Project's Properties set:

1) C/C++ build > Settings > GCC C++ Compiler > Dialect > Language standard to 'ISO C++11 (-std=c++0x)'

2) C/C++ build > Settings > GCC C++ Linker > Libraries add 'pthread' to Libraries

[Updated on: Tue, 22 March 2016 14:40]

Report message to a moderator

Previous Topic:friends cant start myprogramm
Next Topic:Hooking into the indexer
Goto Forum:
  


Current Time: Wed Apr 24 15:56:55 GMT 2024

Powered by FUDForum. Page generated in 0.02759 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top