Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

g++ -std=c++0x and compatibility

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.

  1. 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.

  2. 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.

like image 431
user1806566 Avatar asked Nov 14 '12 16:11

user1806566


People also ask

Who Should an Ox marry?

Generally Speaking, Oxen can accommodate with people of Rat, Snake and Rooster zodiac signs according to compatibility rules. A happy marriage can be predicted. On the other hand they should avoid those of Tiger, Dragon, Horse and Sheep zodiac families.

Is an Ox compatible with a Dog?

Both are trustworthy and faithful and will conduct their marital duties responsibly. However, problems could come to the fore due to the Ox's overbearing and rigid attitude. The Dog likes to have freedom of speech and equality and may not tolerate the narrow-minded Ox for very long.

Can a Pig marry an Ox?

The male ox's reluctance and obstinacy can be washed away with the presence of the female pig nearby. The Pig and Ox compatibility is amazing which makes them a great couple, particularly if they're in love!

Who is Pig compatible with?

The most compatible zodiac signs for the Pig: Goat, Tiger, and Rabbit. They are attracted to each other and know how to please one another. They admire each other's merits and are willing to make efforts for the family.


2 Answers

1. 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.

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++0x that 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.

2. 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?

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++0x but 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.

like image 108
Jonathan Wakely Avatar answered Sep 28 '22 09:09

Jonathan Wakely


I wouldn't try this. All it takes is one C++11-defined macro in a header to change the definition of a class or function and your end-user will be violating the one-definition rule. These uses could be extremely subtle I imagine.

And then on a similar note, there's the vector(count, item = T()) constructor that no longer exists in C++11 (it's now two constructors).

In short you'd have to be really really careful about what standard library components you used in your library to avoid violating the one definition rule, and I can't imagine the risk of breakage is worth using those features.

What you can do is use boost and tr1 to fill in the language gaps until you can use a new compiler and/or be allowed to require your end-users to compile with C++11 support.

like image 38
Mark B Avatar answered Sep 28 '22 08:09

Mark B