C++14/17 项目是否可以使用使用 C++11 标准编译的二进制库,或者源代码是否需要重新编译?
Can a C++14/17 project use binary libraries compiled using C++11 standard or does the source code need to be recompiled?
使用 C++11 编译的二进制文件可以在 C++14/17 项目中使用吗?
c++17 项目中的 c++14 二进制库怎么样?
或者是否需要使用与项目相同的标准更新和重新编译源代码?
是否有任何其他方法可以在新的标准项目中包含旧的标准 C++ 库?
标准定义了编译器在源代码中可以理解的特性以及标准库提供的特性。您的问题的答案取决于您的库的 ABI 版本的兼容性如何,这些版本由您的 compiler/toolchain version/implementation 定义。例如,由于 std::string
GCC 4.x 和 5.x 之间的 ABI 变化,他们构建的库将无法相互链接。
C++ 标准与二进制文件格式无关。那只取决于 compilers/linkers 和 OS。因此,如果编译器供应商更改了 ABI(应用程序二进制接口),则不能简单地 link 将这些部分组合在一起。
如您所见,仅与 gcc 相关:
https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_dual_abi.html
gcc 在 gcc5.1 中引入了一个新的 ABI。来自 gcc 的库版本的历史记录以及对 ABI 更改的评论可以在这里找到:
https://gcc.gnu.org/onlinedocs/libstdc++/manual/abi.html
因此,从一个 C++ 版本更改为另一个版本不会更改 ABI,但更改编译器可以。
我注意到你问题中的STL标签。如果您在客户端界面中使用 STL(我不会这样做),库中使用的实现可能与您当前使用的实现不同。
旧版本的虚构 STL(或其他库)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; }
//...
};
使用旧 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 );
}
同一虚构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; }
//...
};
您的应用程序混合了 STL 版本:
// 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?
}
使用 C++11 编译的二进制文件可以在 C++14/17 项目中使用吗? c++17 项目中的 c++14 二进制库怎么样?
或者是否需要使用与项目相同的标准更新和重新编译源代码?
是否有任何其他方法可以在新的标准项目中包含旧的标准 C++ 库?
标准定义了编译器在源代码中可以理解的特性以及标准库提供的特性。您的问题的答案取决于您的库的 ABI 版本的兼容性如何,这些版本由您的 compiler/toolchain version/implementation 定义。例如,由于 std::string
GCC 4.x 和 5.x 之间的 ABI 变化,他们构建的库将无法相互链接。
C++ 标准与二进制文件格式无关。那只取决于 compilers/linkers 和 OS。因此,如果编译器供应商更改了 ABI(应用程序二进制接口),则不能简单地 link 将这些部分组合在一起。
如您所见,仅与 gcc 相关:
https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_dual_abi.html
gcc 在 gcc5.1 中引入了一个新的 ABI。来自 gcc 的库版本的历史记录以及对 ABI 更改的评论可以在这里找到:
https://gcc.gnu.org/onlinedocs/libstdc++/manual/abi.html
因此,从一个 C++ 版本更改为另一个版本不会更改 ABI,但更改编译器可以。
我注意到你问题中的STL标签。如果您在客户端界面中使用 STL(我不会这样做),库中使用的实现可能与您当前使用的实现不同。
旧版本的虚构 STL(或其他库)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; }
//...
};
使用旧 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 );
}
同一虚构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; }
//...
};
您的应用程序混合了 STL 版本:
// 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?
}