undefined reference to `FilterRegistry::RegisterFilter(std::shared_ptr<Filter>&)'
记一次undefined reference排查
一、背景:
- 编译模块A,三部曲通过
- 编译模块B(依赖模块A),报错:
/usr/bin/ld: OrkAudio.o: in function Transcode(CStdStr<char>&):
/home/lings/GitSpace/oreka/orkaudio/OrkAudio.cpp:173:
undefined reference to FilterRegistry::RegisterFilter(std::shared_ptr<Filter>&)
/usr/bin/ld: /home/lings/GitSpace/oreka/orkaudio/OrkAudio.cpp:175:
undefined reference to FilterRegistry::RegisterFilter(std::shared_ptr<Filter>&)
/usr/bin/ld: /home/lings/GitSpace/oreka/orkaudio/OrkAudio.cpp:177:
undefined reference to FilterRegistry::RegisterFilter(std::shared_ptr<Filter>&)
/usr/bin/ld: /home/lings/GitSpace/oreka/orkaudio/OrkAudio.cpp:179:
undefined reference to FilterRegistry::RegisterFilter(std::shared_ptr<Filter>&)
/usr/bin/ld: /home/lings/GitSpace/oreka/orkaudio/OrkAudio.cpp:181:
undefined reference to FilterRegistry::RegisterFilter(std::shared_ptr<Filter>&)
/usr/bin/ld: OrkAudio.o:/home/lings/GitSpace/oreka/orkaudio/OrkAudio.cpp:183:
二、排查
- 查看A模块里面的方法:
nm /usr/lib/liborkbase.so |grep FilterRegistry
- 得到的结果如下:
000000000004ecd0 T _ZN14FilterRegistry14RegisterFilterERN5boost10shared_ptrI6FilterEE
- 乍一看没问题,仔细看入参的指针类型不一样!
- 模块A里的是boost::shared_ptr
- 模块B里的入参是std::shared_ptr
三、解决
- 源代码里确实允许使用两种
#ifdef SUPPORTS_CPP11
#include <memory>
namespace oreka {
using std::shared_ptr;
using std::make_shared;
using std::weak_ptr;
};
#else
#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>
#include <boost/weak_ptr.hpp>
namespace oreka {
using boost::shared_ptr;
using boost::make_shared;
using boost::weak_ptr;
};
#endif
- 而B模块编译的时候Makefile文件中指定了CPP11
CXXFLAGS = -std=c++11 -g -O2 -fno-inline-functions -std=c++11 -DSUPPORTS_CPP11 -fPIC
- 去掉B模块指定的-DSUPPORTS_CPP11即可。
上善若水,水利万物而不争。