简体   繁体   中英

Can a C++14/17 project use binary libraries compiled using C++11 standard or does the source code need to be recompiled?

Can a binary which was compiled using C++11 be used in a c++14/17 project? What about a c++14 binary library in a c++17 project?

Or would the source code need to be updated and recompiled using the same standard as the project?

Are there any other ways to include older standard C++ libraries in new standard projects?

The C++ standard has nothing todo with the file format of the binary. That only depends on the compilers/linkers and the OS. So if the compiler vendor changes the ABI ( Application binary interface ), you can not simply link the parts together.

As you can read here, only relevant for gcc:

https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_dual_abi.html

gcc introduces with gcc5.1 a new ABI. A history of versions of libraries from gcc and maybe comments to ABI changes can be found here:

https://gcc.gnu.org/onlinedocs/libstdc++/manual/abi.html

So changing from one C++ version to another did not change the ABI but changing the compiler can.

I have noticed the STL tag in your question. If you use STL in your client interface (which I wouldn't do), the implementation used in library might differ from implementation you are currently using.

An old version of a fictional STL (or other library) class:

// old stl implementation
template < class T >
struct stl_t
{
  T data;
  void set_data( const T d ) { data = d; }
  T get_data() { return data; }
  //...
};

A library developed using the old compiler/STL:

// client interface file (source delivered to clients)
void f( stl_t<int>& ili ); // old stl assumed

// client interface implementation file (binary delivered to clients)
#include <stl_t> // old stl included!
//...
void f( stl_t<int>& ili ) // old stl used
{
  ili.set_data( 42 );
}

The new version of the same fictional STL class:

// new stl implementation
template < class T >
struct stl_t
{
  T* data { 0 }; // was T in previous implementation; now it is T*
  void set_data( const T d ) { *data = d; }
  T get_data() { return *data; }
  //...
};

Your application mixing the STL versions:

// your application
#include <stl_t> // new stl!
#include "library.h" // expects OLD stl but new used

void g()
{
  stl_t<int> a; // NEW stl
  f( a ); // OLD stl expected
  int i = a.get_data();
  // what value is i?
}

Standard defnes which features are understandable for compiler in source code and what is provided by standard library. Answer to your question depends on how compatible are ABI versions of your libraries which is defined by your compiler/toolchain version/implementation. For example, because of std::string ABI change between GCC 4.x and 5.x libraries built by them won't be inter-linkable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM