错误:名称空间'std'中的'mutex'未在gcc 4.6.2中命名类型

rkb*_*rkb 3 c++ c++11 gcc4.6

简单的程序就像这样

#include <iostream>       // std::cout
#include <thread>         // std::thread
#include <mutex>          // std::mutex

std::mutex mtx;           // mutex for critical section

int main ()
{
     return 0;
}
Run Code Online (Sandbox Code Playgroud)

尝试以下编译

$ /usr/local/Cellar/gcc46/4.6.4/bin/g++-4.6 -std=gnu++0x -I/usr/local/Cellar/gcc46/4.6.4/gcc/include/c++ -L/usr/local/Cellar/gcc46/4.6.4/gcc/lib temp_mutex.cpp 
temp_mutex.cpp:6:1: error: 'mutex' in namespace 'std' does not name a type
Run Code Online (Sandbox Code Playgroud)

顺便说一句,我在Mac上编译.

Chr*_*ung 5

它对我有用:

$ g++-4.7 -O2 -Wall -std=c++11 -c mutextest.cc
$ g++-4.6 -O2 -Wall -std=c++0x -c mutextest.cc
$ g++-4.6 -O2 -Wall -std=gnu++0x -c mutextest.cc
$ g++-4.4 -O2 -Wall -std=c++0x -c mutextest.cc
$ g++-4.4 -O2 -Wall -std=gnu++0x -c mutextest.cc
Run Code Online (Sandbox Code Playgroud)

这些都没有生成任何错误消息.

  • How many versions of gcc do you have? :) (3认同)