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

C++ 在静态库中找到对成员函数的未定义引用,c++,linux,linker,g++,C++,Linux,Linker,G++,我知道你在想什么——这个问题已经回答了一百万次了。我希望我能在标题中写点东西,清楚地表明我已经知道图书馆的顺序是重要的 我在Centos 6 64上使用g++。我有一个简单的测试程序: #include <ptlib.h> int main() { PTimer indirectTimer1_; indirectTimer1_.SetNotifier(0); return 0; } 我得到的回答是: /tmp/cc53itXb.o: In function `mai

我知道你在想什么——这个问题已经回答了一百万次了。我希望我能在标题中写点东西,清楚地表明我已经知道图书馆的顺序是重要的

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

#include <ptlib.h>

int main()
{
  PTimer indirectTimer1_;
  indirectTimer1_.SetNotifier(0);

  return 0;
}
我得到的回答是:

/tmp/cc53itXb.o: In function `main':
mm.cpp:(.text+0x52): undefined reference to `PTimer::SetNotifier(PNotifierTemplate<int> const&)'
collect2: ld returned 1 exit status
/tmp/cc53itXb.o:在函数“main”中:
mm.cpp:(.text+0x52):对'PTimer::SetNotifier(PNotifierTemplate const&')的未定义引用
collect2:ld返回了1个退出状态
但我知道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&)
nm-AC ptlib/lib_linux_x86_64/libpt_s.a | grep SetNotifier
ptlib/lib_linux_x86_64/libpt_s.a:osutil.o:000000000000 3dd8 T PTimer::SetNotifier(PNotifierTemplate const&)
更让人困惑的是,当我在Centos 5 32位上编译库并运行相同的测试时,它链接得很好

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

正如你所看到的,我尝试了很多东西,但仍然没有找到答案。我需要别的东西来试试!或者有人指出我做错了非常容易的事情。

对'PTimer::SetNotifier(PNotifierTemplate const&')的未定义引用
undefined reference to `PTimer::SetNotifier(PNotifierTemplate<int> const&)'

                        PTimer::SetNotifier(PNotifierTemplate<long> const&)
PTimer::SetNotifier(PNotifierTemplate常量&)
注意
int
long

对'PTimer::SetNotifier(PNotifierTemplate const&')的未定义引用的区别
PTimer::SetNotifier(PNotifierTemplate常量&)

注意
int
long
之间的区别?

答对了!我忽略了模板参数中的细微差别。这个库是用一组定义构建的,但是我们的程序在包含头文件时没有使用相同的定义。所以INT被定义为INT,另一个定义为long。@Hieronymus:不是我。。。编译器和链接器是检测到问题的!嗯,它检测到了,但它没有告诉我如何修复:)宾果!我忽略了模板参数中的细微差别。这个库是用一组定义构建的,但是我们的程序在包含头文件时没有使用相同的定义。所以INT被定义为INT,另一个定义为long。@Hieronymus:不是我。。。编译器和链接器是检测到问题的!嗯,它检测到了,但没有告诉我如何修复:)将
-Wall-g
传递到
g++
哦,我们肯定使用-Wall。没有警告。我想加入-Wextra来获得更多,但我没有时间。我只是在上面的测试中尝试了-Wall和-Wextra,没有任何警告。叹气。把
-Wall-g
传给
g++
哦,我们肯定用-Wall。没有警告。我想加入-Wextra来获得更多,但我没有时间。我只是在上面的测试中尝试了-Wall和-Wextra,没有任何警告。唉。
undefined reference to `PTimer::SetNotifier(PNotifierTemplate<int> const&)'

                        PTimer::SetNotifier(PNotifierTemplate<long> const&)