2013-03-26 200 views
1

尝试从编译源文件创建目标文件时出现编译错误。我正在使用C++ 11附带的头文件。我还使用了一个C++模式识别库与其他几个包括。C++ 11 #include <thread>给出编译错误

我所做的只是将#include <thread>添加到我的rbm_test.cc源文件中。

我的编译命令:

g++ -std=c++11 -O3 -DQUIET -fPIC -pthread -ansi -pedantic -DARCH_INTEL -Wall -W -Wchar-subscripts -Wpointer-arith -Wcast-qual -Wwrite-strings -Wconversion -Wno-old-style-cast -Wctor-dtor-privacy -Wnon-virtual-dtor -I../src -I../.. -DPATREC -D_UNIX_ -o rbm_test.o -c ../src/rbm_test.cc

编译错误我得到的是:

error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.

奇怪的是,当我编译

g++ -std=c++11 -pthread -c main.cpp -o main.o

下面的代码示例那我也没错误。

这里是main.cpp

#include <iostream> 
#include <thread> 

void f1() 
{ 
    std::cout << "Thread executing\n"; 
} 

int main() 
{ 
    std::thread t1(f1); 
    std::thread t2(f1); 
    t1.join(); 
    t2.join(); 
} 

有没有可能是某些编译标志,当我尝试编译rbm_test.cc是冲突的?

+0

好吧,就像我发布的问题,我找到了解决方案。 -ansi标志与-std = C++ 11标志冲突。 -ansi相当于-std = C++ 98。移除-ansi标志解决了这个问题。 – mcvz 2013-03-26 08:10:43

+1

是的,那就是我正在建议的。您可以将其发布为答案并选择它。 – juanchopanza 2013-03-26 08:11:23

+0

我可能错过了它,但包含编译器版本是个好主意。 – 2013-03-26 08:12:23

回答

7

- ansi标志与-std=c++11标志冲突。 -ansi相当于-std=c++98。删除-ansi标志可解决该问题。