【问题标题】:C-library not linking using gcc/g++C 库不使用 gcc/g++ 进行链接
【发布时间】:2009-07-01 09:14:45
【问题描述】:

我有一个在 gcc 中使用的 c 库。该库具有扩展名 .lib,但始终作为静态库链接。如果我编写一个将库用作 c 代码的程序,那么一切都很好。但是,如果我将文件重命名为 .cpp (在 c/c++ 中执行简单的操作),我会得到未定义的引用。这些是我为测试目的编写的简单小程序,所以没有花哨的东西。我编译使用:

gcc -g -Wall -I <path to custom headers> -o program main.c customlibrary.lib -lm -lpthread

上面的工作就像一个魅力。然而:

g++ -g -Wall -I <path to custom headers> -o program main.cpp customlibrary.lib -lm -lpthread

gcc -g -Wall -I <path to custom headers> -o program main.cpp customlibrary.lib -lm -lpthread -lstdc++

导致对 customlibrary.lib 中任何函数的未定义引用。我尝试创建一个名为 customlibrary.a 的符号链接,但没有成功。

为什么 g++ find 不能识别我的库。不幸的是,我无法访问库的源代码,但是将 c-lib 链接到 c++ 应该不是问题,对吧?

【问题讨论】:

    标签: c g++ static-linking


    【解决方案1】:

    您的库似乎有一个 API,假定它将从 C 调用,而不是 C++。这很重要,因为 C++ 实际上要求从库中导出的符号中包含更多信息,而不仅仅是函数名。这是通过“名称修饰”函数来处理的。

    我假设您的库有一个声明其公共接口的包含文件。为了使其与 C 和 C++ 兼容,您应该安排告诉 C++ 编译器它声明的函数应该假定使用 C 的链接和命名。

    测试这个可能很简单的答案是这样做:

    extern "C" {
    #include "customlibrary.h"
    }
    

    在您的 main.cpp 中,而不是直接包含 customlibrary.h

    要使头文件本身在两种语言中都能正常工作,并正确地将其功能声明为 C 类 C++,请将以下内容放在头文件顶部附近:

    #ifdef __cplusplus
    extern "C" {
    #endif
    

    以及靠近底部的以下内容:

    #ifdef __cplusplus
    }
    #endif
    

    【讨论】:

    • 符号__cplusplus有两个下划线,由相关标准定义。
    【解决方案2】:

    C++ 编译器执行所谓的名称修改——出现在代码中的名称与链接器看到的名称不同。解决这个问题的正常方法是告诉编译器某些函数需要 C 链接:

    // myfile.cpp
    extern "C" int libfun();    // C function in your library
    

    或对整个头文件执行此操作:

    // myfile.cpp
    extern "C" {
      #include "mylibdefs.h"      // defs for your C library functions
    }
    

    【讨论】:

      【解决方案3】:

      你的头文件有没有平时的

      #ifdef __cplusplus
      extern "C" {
      #endif
      
      // ...
      
      #ifdef __cplusplus
      } /* extern "C" */
      #endif
      

      显式地为库函数提供 C 链接。

      .cpp 文件是使用 C++ 链接编译的,即默认情况下名称修改。

      【讨论】:

        猜你喜欢
        • 2012-01-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-09-07
        • 1970-01-01
        • 1970-01-01
        • 2010-11-03
        • 1970-01-01
        相关资源
        最近更新 更多