c++11 #include gives compile error

2019-07-12 17:48发布

问题:

I get a compile error when trying to create an object file from a compiled source file. I am using the header which came with c++11. I am also using a c++ pattern recognition library with several other includes.

All I did was add #include <thread> to my rbm_test.cc source file.

My compile command:

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

The compile error I get is:

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.

Strangely, when I compile the following code example with

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

then I have no error.

Here Is 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();
}

Is it possible that some of the compile flags are conflicting when I try to compile rbm_test.cc?

回答1:

The -ansi flag conflicts with the -std=c++11 flag. -ansi is equivalent to -std=c++98. Removing the -ansi flag solves the problem.