C++ compiler not finding #include <thread>

这一生的挚爱 提交于 2021-02-10 18:13:58

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/17901797/c-compiler-not-finding-include-thread

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!