I’m using g++ 4.4 to compile a shared library on linux. I would like to use some C++11 features if I can in the library, but I cannot update the version of the compiler or require any special compiler switches for users of my library.
I have two questions and I’m having trouble finding a definitive answer.
-
If I compile a shared library with -std=c++0x or -std=g++0x, am I guaranteed that a program that uses my library doesn’t need those switches (provided I have no c++0x features in the header files)? It seems to work, but I don’t want to be signing up for subtle problems down the road.
-
The standard library for C++11 in g++ 4.4 is quite incomplete. Since much of the standard library is header-only and gnu’s header files are generally full of version ifdefs, I would think that there may be a way to use a more recent version of at least the header files in libstdc++. I can’t use a different .so for it, though. I’m sure I can kludge this together, but is it possible to do something like this correctly?
Thanks.
C++11 support was still experimental in GCC 4.x releases (it is no longer experimental from GCC 5 onwards). Although we tried to keep things working, the answer is no, you are not generally guaranteed that will work in all cases. There are a number of ABI changes caused by using
-std=c++0xthat could cause problems for programs that mix C++03 code and C++11 code, see http://gcc.gnu.org/wiki/Cxx11AbiCompatibility for more details. If your library doesn’t export any of the symbols described on that page then you should be fine.No, there is absolutely no chance whatsoever that will work. The headers from later versions use features not supported by 4.4, and even if you could use them you’d need to use the newer
libstdc++.so. Just no.The headers are not full of version
#ifdefs, almost the only ones you’ll find are checks for__GXX_EXPERIMENTAL_CXX0X__which is defined by G++ when you use-std=c++0xbut that doesn’t mean your 4.4 version supports lambdas, non-static data member initializers, proper rvalue reference semantics, default/deleted functions etc. that later headers make liberal use of. You must use libstdc++ headers with the same version of GCC they came with.In short, if you want proper C++11 support you need to use a newer compiler.
If you can’t use a newer compiler you can’t get proper C++11 support.