【问题标题】:Can a C++14/17 project use binary libraries compiled using C++11 standard or does the source code need to be recompiled?C++14/17 项目可以使用使用 C++11 标准编译的二进制库还是需要重新编译源代码?
【发布时间】:2017-10-25 04:41:29
【问题描述】:

可以在 c++14/17 项目中使用使用 C++11 编译的二进制文件吗? c++17项目中的c++14二进制库呢?

或者是否需要使用与项目相同的标准更新和重新编译源代码?

还有其他方法可以将旧标准 C++ 库包含在新标准项目中吗?

【问题讨论】:

  • 标准对此只字未提。这取决于您的编译器工具链。
  • @RaymondChen 你能提供一些关于你在实践中看到的东西的见解吗?
  • 在我从事的项目中,一切都是用完全相同版本的编译器编译的。当编译器升级到 C++14 时,整个项目将使用新的编译器重新编译。这样就不会出现不匹配的情况。
  • @RaymondChen 所以所有库源代码都经过检查/更新,以确保它符合新标准(假设库由于某种原因不向前兼容)?
  • 恕我直言,这个问题相当于不同编译器或不同编译器版本构建的链接库。大多数情况下它有效,但没有任何承诺。

标签: c++ c++11 c++14 standards c++-standard-library


【解决方案1】:

C++ 标准与二进制文件格式无关。这仅取决于编译器/链接器和操作系统。因此,如果编译器供应商更改了 ABI(应用程序二进制接口),您就不能简单地将这些部分链接在一起。

如您在此处阅读的,仅与 gcc 相关:

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

gcc 在 gcc5.1 中引入了一个新的 ABI。从 gcc 和 cmets 到 ABI 更改的库版本历史可以在此处找到:

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

因此,从一个 C++ 版本更改为另一个版本不会更改 ABI,但更改编译器可以。

【讨论】:

    【解决方案2】:

    我注意到您问题中的 STL 标签。如果您在客户端界面中使用 STL(我不会这样做),则库中使用的实现可能与您当前使用的实现不同。

    虚构的 STL(或其他库)类的旧版本:

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

    使用旧编译器/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 类的新版本:

    // 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?
    }
    

    【讨论】:

      【解决方案3】:

      标准定义了源代码中编译器可以理解的功能以及标准库提供的功能。对您的问题的回答取决于您的库的 ABI 版本的兼容性,这些版本由您的编译器/工具链版本/实现定义。例如,由于std::string ABI 在 GCC 4.x 和它们构建的 5.x 库之间的更改将无法相互链接。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-27
        • 1970-01-01
        • 2015-11-05
        • 1970-01-01
        • 2020-02-16
        • 2013-07-19
        相关资源
        最近更新 更多