Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a C++17 library against a C++11 application

Is it possible to consume a library built using C++17 against a C++11 application if all the public facing headers and APIs belonging to the C++17 library that the C++11 application consumes follows C++11 syntax. The internal implementation of the C++17 library does have C++17 specific features.

Does it matter if it's a statically linked vs dynamically linked?

like image 774
rstr1112 Avatar asked Jul 24 '20 18:07

rstr1112


2 Answers

Is it possible to consume a library built using C++17 against a C++11 application if all the public facing headers and APIs belonging to the C++17 library that the C++11 application consumes follows C++11 syntax.

In general, that is a recipe for disaster. In a subset of cases it might work if you are lucky (for instance, if you do not share standard library objects that changed ABI, if you do not trigger any ABI difference in your usage of your API, etc.).

What you want to do instead is compile all your code using the exact same compiler, including compiler version and compiler flags. Even then, you should read your compiler's documentation for further possible issues regarding static/dynamic linking of dependencies and system dependencies.

The internal implementation of the C++17 library does have C++17 specific features.

That is not a problem on its own (in fact, many C++ libraries give C interfaces), but you need to respect whatever linking restrictions/issues your compiler/platform documents.

Does it matter if it's a statically linked vs dynamically linked?

Same. For most platforms, it should not change anything with respect the question of mixing C++11 and C++17, but you still have to take care of the usual issues.

like image 135
Acorn Avatar answered Oct 06 '22 00:10

Acorn


C++ does not guarantee ABI stability/compatibility. So you always have to compile and link all parts of your application with the exact same compiler and usually with the same compiler options. Some compilers sometimes give binary compatibility guarantees, but don't count on it. Changing language standard versions between objects is often fine though, but in some cases there are ABI (or API) breaks between language versions.

The C++ language gives no guarantees unless you compile everything the same way with the same tools. Basically; compile everything from scratch every time with the same tools and same settings or no guarantees (this includes your code, the code of the standard library as well as the code of any third party libraries you may be using).

like image 29
Jesper Juhl Avatar answered Oct 06 '22 00:10

Jesper Juhl