对静态库中找到的成员函数的未定义引用

Undefined reference to member function found in static library

本文关键字:成员 函数 未定义 引用 静态      更新时间:2023-10-16

我知道你在想什么 - 这已经被回答了一百万次。我希望我能在标题中加入一些东西,以清楚地表明我已经知道库的顺序很重要。

我在 Centos 6 64 上使用 g++。我有一个简单的测试程序:

#include <ptlib.h>
int main()
{
  PTimer indirectTimer1_;
  indirectTimer1_.SetNotifier(0);
  return 0;
}

我用这个命令编译并链接它:

g++ -I./ptlib/include/ mm.cpp ptlib/lib_linux_x86_64/libpt_s.a -lpthread -lrt

我得到这个作为答案:

/tmp/cc53itXb.o: In function `main':
mm.cpp:(.text+0x52): undefined reference to `PTimer::SetNotifier(PNotifierTemplate<int> const&)'
collect2: ld returned 1 exit status

但我知道 PTimer::SetNotifier 在 .a 文件中:

nm -AC ptlib/lib_linux_x86_64/libpt_s.a | grep SetNotifier
ptlib/lib_linux_x86_64/libpt_s.a:osutil.o:0000000000003dd8 T PTimer::SetNotifier(PNotifierTemplate<long> const&)

更令人困惑的是,当我在 Centos 5、32 位上编译库并运行相同的测试时,它的链接很好。

我尝试使用'-Lptlib/lib_linux_x86_64 -lpt_s',我试过使用-wl,--start-group/-wl,--end-group args无济于事。如果我将'ptlib/src/ptlib/unix/osutil.cxx'添加到g++行,它可以很好地编译和链接。不幸的是,这只是我们主程序中未引用函数的一个示例。不是全部,只是像这样一些看似随机的方法没有定义(在 Centos 5,32 位上链接也很好(。

如您所见,我已经尝试了很多东西,但仍然没有弄清楚。我需要别的东西来尝试!或者有人指出我做错了一件非常容易的事情。

undefined reference to `PTimer::SetNotifier(PNotifierTemplate<int> const&)'
                        PTimer::SetNotifier(PNotifierTemplate<long> const&)

请注意intlong