jamespick
jamespick

Reputation: 1978

C++ compiler not finding #include <thread>

Here is the top of my source file:

#include <iostream>
#include <thread>
#include <fstream>

...

thread help(startHelp);

Where the thread is inside the function handleRequestsFromServer and startHelp is a void function.

When compiling this with g++ on Mac OS X 10.8.4, I get this error:

$ g++ diskutilityhelper.cpp -o run.out
diskutilityhelper.cpp:5:18: error: thread: No such file or directory
diskutilityhelper.cpp: In function ‘void handleRequestsFromServer()’:
diskutilityhelper.cpp:140: error: ‘thread’ was not declared in this scope
diskutilityhelper.cpp:140: error: expected `;' before ‘bomb’

I don't understand this error at all. Could anyone please help?

Upvotes: 1

Views: 3787

Answers (2)

rubenvb
rubenvb

Reputation: 76785

You probably want to use Clang instead of GCC.

clang++ -std=c++11 -stdlib=libc++ diskutilityhelper.cpp -o run.out

All the options for GCC can be used with Clang, some are ignored. The above links to libc++, which is the preferred C++ standard library for Mac OS X with Clang (and a lot more complete than libstdc++ (even when considering the newest GCC).

As for the reason why this happens: my magic fortune telling ball tells me the g++ you are calling is an ancient GCC 4.2.1 Apple thingie, with that GCC's libstdc++, which has little to no C++11 support. Apple switched to Clang and it is now much preferred.

Upvotes: 6

Nikos C.
Nikos C.

Reputation: 51920

The version of GCC shipped with XCode is very old. It doesn't support C++11.

You should compile your code using clang++ instead from the latest XCode version. That supports C++11 just fine (AFAIK). GCC is included in XCode mostly for compatibility purposes. The recommended compiler on OS X these days is Clang.

Upvotes: 3

Related Questions