I am using CMake 2.8.11 and GCC 4.8.2. I was building some C++ code which used std::shared_ptr
which built fine in MS VS 2012 but when I tried the same on RHEL6 using GCC 4.8.2, I promptly ran into the following error:
error: 'shared_ptr' is not a member of 'std'
I found this question with responses that I thought addressed and I promptly added -std=c++11
to my CMAKE_CXX_FLAGS
, but I still keep running into the error. I add the flag in CMake simply using:
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11" CACHE STRING "Add C++ 11 flags")
I set my custom compiler in CMake using:
SET(GCC_DIR "</path/to/custom>/gcc")
SET(CMAKE_CXX_COMPILER "${GCC_DIR}/bin/g++ CACHE FILEPATH "CXX compiler")
SET(CMAKE_C_COMPILER "${GCC_DIR}/bin/gcc CACHE FILEPATH "C compiler")
The include is
#include <memory>
which in turn has
#include <bits/shared_ptr.h>
which defines the shared_ptr
class. So I'm not sure why I keep getting the error (and yes I cleared cached and rebuilt after adding the -std=c++11
compiler option). Any ideas are very much appreciated.
EDIT 1:
I created a simple program (main.cpp) as follows:
#include <memory>
#include <iostream>
int main() {
std::shared_ptr<int> pint = std::make_shared<int>();
std::cout << "Pint points to " << pint.get() << "\n";
return 0;
}
Then I built it using <path/to/custom/>g++ main.cpp -o prog
and promptly ran into the same error (above). Next I did: <path/to/custom/>g++ -std=c++11 main.cpp -o prog
and it compiles & runs OK. For my real application, I added the -std=c++11
flag to linker flags as well (in addition to compiler flags) in my CMake
config system, but I still see the same error. Proceeding to check the CMakeCache
to see if the flags are property registered, but any ideas are appreciated.
EDIT 2:
Suprisingly, I found in CMakeCache
that the -std=c++11
flag is not being added to the CMAKE_CXX_FLAGS
, etc. So this must have to do with the error. I am trying to fix it so that it actually takes this flag. Thanks all.
The answer confirms the hunch in EDIT 2 of my question. Apparently CMake 2.8.x
is not appending to the variable CMAKE_CXX_FLAGS
using the SET
command using the syntax shown in my question (as per the documentation); I tried other variants of the SET
command to append, to no avail.
So finally, instead of appending, I assigned separately for the case when C++11 is to be enabled and when it is to be disabled, as follows:
IF(USE_C++11)
...
ELSE(USE_C++11)
...
ENDIF(USE_C++11)
This worked fine. Thanks to @nos for the idea to make an isolated example.