【问题标题】:Linker can't find functions even if they are in a static lib链接器即使在静态库中也找不到函数
【发布时间】:2013-08-05 03:21:15
【问题描述】:

我有一个可以编译的静态库,看起来(简化)如下:

math_util.h:

void foo(...);

math_util.cpp:

void foo(...) { ... }

A.cpp:

#include "math_util.h"
class A 
{
    bar() {
        foo(...);
    }
}

静态库编译良好。 但是,当我想在实际应用中使用 A 时,我得到:

undefined reference to `foo(...)' 

但是当我用 nm 检查我的静态库时:

math_util.o:
                 U _GLOBAL_OFFSET_TABLE_
000000000000010a T _ZN8fooEPA2_dPdj

这不是原型问题,因为如果我将foo 的代码复制到 A.cpp 中,它可以正常工作。

所以,我不明白,为什么 G++ 找不到该符号,即使它显然存在于静态库中? 对于其他任何功能,我都没有这个问题。

注意:如果我正在运行 Linux,使用 QtCreator 并且我对两个项目都使用 qmake(但静态库是纯 STL C++,不涉及 Qt)。

编辑:查看真实的相关文件:

https://github.com/jcelerier/spectral-subtraction/blob/master/libnoisered/math_util.h

https://github.com/jcelerier/spectral-subtraction/blob/master/libnoisered/math_util.cpp

A.cpp:

https://github.com/jcelerier/spectral-subtraction/blob/master/libnoisered/estimation/simple_estimation.cpp

项目文件:

https://github.com/jcelerier/spectral-subtraction/blob/master/libnoisered/libnoisered.pro

【问题讨论】:

  • 显示更多源代码,并给出实际的编译过程(或Makefile)。
  • 我使用 qmake,它会依次生成 makefile。您只需在 project.pro 文件中指定 SOURCES = (cpp files)HEADERS = (header files) 即可,它会处理它,有点像 CMake

标签: c++ linker g++ ld


【解决方案1】:

您在.h 文件中的签名;

void compute_power(fftw_complex *in, double *powoutput, int size);

.cpp文件中的签名不兼容;

void compute_power(fftw_complex *in, double *powoutput, unsigned int size)

它们的名称被修改为不同的符号以进行链接。

将它们更改为相同,并且事情应该链接。

【讨论】:

  • 啊,我已经仔细检查了大概二十次了……邪恶的小未签名。谢谢!
猜你喜欢
  • 2021-08-17
  • 1970-01-01
  • 2017-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-29
  • 1970-01-01
相关资源
最近更新 更多