0

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.

Community
  • 1
  • 1
squashed.bugaboo
  • 1,338
  • 2
  • 20
  • 36
  • Try with -std=c++0x. It might work as some compilers use this instead of -std=c++11. – Celine NOEL Jan 07 '16 at 23:24
  • I tried that first actually, and it didn't work. But the link I referred to mentioned to use c++11 for GCC > 4.7, and so I did, but even that didn't work. – squashed.bugaboo Jan 07 '16 at 23:25
  • 2
    RHEL6 ships with a much older gcc, are you sure you're actually invoking gcc 4.8.2 and that it's properly installed ? – nos Jan 07 '16 at 23:27
  • Let us see the cmakelists.txt too. – Celine NOEL Jan 07 '16 at 23:28
  • @nos: That's a good point. Yes, I am invoking GCC 4.8.2, that's for sure. The whole application is built with a custom GCC 4.8.2 compiler installed on RH6. I did not do the installation of the compiler. Do you think that might be the problem? – squashed.bugaboo Jan 07 '16 at 23:29
  • @squashed.bugaboo It seems to be. Can you at least verify that you're invoking this custom installed gcc, and not the default gcc v 4.4 ? – nos Jan 07 '16 at 23:30
  • @nos: Yes, see my edited comment. – squashed.bugaboo Jan 07 '16 at 23:31
  • 1
    Your include path is for gcc 4.4 but you using gcc 4.8.2. I highly recommend to use devtools-2 on CentOS 6/RHEL 6. wget http://people.centos.org/tru/devtools-2/devtools-2.repo -O /etc/yum.repos.d/devtools-2.repo && yum install devtoolset-2-gcc devtoolset-2-binutils devtoolset-2-gcc-c++ && scl enable devtoolset-2 bash – Brian Cannard Jan 07 '16 at 23:35
  • 2
    Then you can try to narrow it down. Create a single example.cpp file that uses std::shared_ptr , , compile it by hand by running `g++ -Wall -std=c++11 example.cpp` , run g++ -v to view its version. If that doesn't work, there's a problem with your gcc installation, if it does work, it's a problem with your build system. – nos Jan 07 '16 at 23:36
  • @nos: Thanks, that sounds like a good idea. What did you mean by run g++ -v to view the version though? If ran it with the g++ from the custom compiler, I know I would see 4.8.2, so that remark seemed out of place. Wouldn't just compiling the example program with the custom compiler suffice? – squashed.bugaboo Jan 07 '16 at 23:41
  • Run `g++ -v`, and it prints version information. – Mats Petersson Jan 07 '16 at 23:43
  • @avesus: I've instructed CMake the path to my custom GCC dir and compilers; so I am including the correct headers at the intended include path assuming CMake does what it is supposed to. – squashed.bugaboo Jan 07 '16 at 23:44
  • No, that remark is not out of place. If somone has installed a custom g++, they hopefully did so in a way that would not conflict with the normal gcc installation of RHEL. That means somehow your PATH must be changed for you to invoke g++ and get gcc 4.8.2, and if you somehow are in a shell that don't have this altered PATH, or there's things in your cmake files messing with the PATH, your assumptions could be wrong when compiling the suggested example.cpp. Ofcourse this was before we knew you already set the proper PATH, since all you told us was you ran g++. Stil, always verify assumptions. – nos Jan 07 '16 at 23:45
  • @nos: I added some more notes on how this is done in CMake. But I still don't see the value of running `g++ -v`; if I did just that, then yeah I would see 4.4.7 or whatever comes default with RHEL6; but our environment is set up to use GCC 4.8.2 to build the application (again, see the snippet above). So I don't see what running `g++ -v` would tell me that I don't already know. But yeah, compiling the small test program is helpful. – squashed.bugaboo Jan 07 '16 at 23:51
  • 2
    You're free to skip running it, it was just a suggestion so you are actually sure about what's going on. albeit I cannot see why you would, it'll take a second at most. But please, create a simple example and run g++ by hand so you can verify if it's the compiler or cmake at fault. – nos Jan 07 '16 at 23:52
  • I highly recommend to run shell with `scl enable devtoolset-2 bash`. It sets `PATH` appropriately. – Brian Cannard Jan 08 '16 at 00:09

1 Answers1

0

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.

squashed.bugaboo
  • 1,338
  • 2
  • 20
  • 36