I'm looking specifically for a clang answer to this question. If I compile one object with -std=c++11
and another with -std=c++17
can they be safely linked?

- 35,360
- 15
- 114
- 174
-
If both objects are compiled with the same version of the same compiler then maybe. – Indiana Kernick Nov 01 '18 at 02:02
-
Might need a 17 wrapper for the 11 objects. – solarflare Nov 01 '18 at 02:04
-
But if your compiler supports C++17 then why would you use C++11 – Indiana Kernick Nov 01 '18 at 02:04
-
You should be good if you don't try to include a 17 header in a 11 file – Indiana Kernick Nov 01 '18 at 02:05
-
Good question I would think it would work as the C++11 is forwards compatible – Jake Freeman Nov 01 '18 at 02:07
-
If it works in GCC then there's a good chance that it'll work in Clang too – Indiana Kernick Nov 01 '18 at 02:08
-
1Same library linked against? Any restriction on what the C++11 code uses? – Yakk - Adam Nevraumont Nov 01 '18 at 02:12
-
@Yakk-AdamNevraumont are you asking if the two objects are linked against different standard libraries? (sorry I got pulled away like immediately after posting) – Ryan Haining Nov 01 '18 at 16:55
-
@Kerndog73 big project, libraries configured with c++11 and I want to write an executable using some of those libraries for c++17 – Ryan Haining Nov 01 '18 at 16:56
-
@RyanHaining Yes. There is a linux standard library that differs on the ABI of `std::string` based on which version if I remember rightly (technically between C++03 and C++11, but your C++11 code might use the C++03 ABI). You can search for "ABI changes" to find if there are more cases like this and check if it applies to your case. – Yakk - Adam Nevraumont Nov 01 '18 at 17:28
-
@Yakk-AdamNevraumont only one stdlib here – Ryan Haining Nov 01 '18 at 22:00
1 Answers
The answer posted here by Jonathan Wakely is correct for Clang as well.
Easy answer: if you're compiling both of the objects yourself, the -std
option you choose won't affect the final outcome.
Most of a given C++ ABI is decided by the standard library. The rest is assorted runtime support such as exceptions, compiler builtins (which may actually dispatch to the standard library), and things like that. libstdc++
ABI compatibility is an explicit goal of for Clang, so you're ok here.
If you're using the same compiler version, the -std
option will change which symbols get exposed to code when you compile. However, libstdc++'s ABI for stable features is forwards compatible, so you'd also be OK here too.
Harder answer: Your comments suggest you have the source, but if you only have object files and are just doing the final link, there's a good chance it will work unless the version of Clang used to build the std=c++11
object is very old. The g++
and libstdc++
authors went out of their way to version symbols, so the chances of you having something that links, but then fails in a hard to diagnose way is basically zero. (This is referred to in the second to last paragraph in Jonathan's answer).
Again, since Clang aims to keep compatibility with libstdc++
's ABI, you should be fine. There are other details, but the vast majority are addressed in Jonathan's answer, and he's in a better position to explain it than I am.

- 1
- 1

- 583
- 2
- 10