undefined reference to `FilterRegistry::RegisterFilter(std::shared_ptr<Filter>&)'

记一次undefined reference排查

一、背景:

  1. 编译模块A,三部曲通过
  2. 编译模块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:

二、排查

  1. 查看A模块里面的方法:
    nm /usr/lib/liborkbase.so |grep FilterRegistry
  2. 得到的结果如下:
    000000000004ecd0 T _ZN14FilterRegistry14RegisterFilterERN5boost10shared_ptrI6FilterEE
  3. 乍一看没问题,仔细看入参的指针类型不一样!
  4. 模块A里的是boost::shared_ptr
  5. 模块B里的入参是std::shared_ptr

三、解决

  1. 源代码里确实允许使用两种
    #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
  2. 而B模块编译的时候Makefile文件中指定了CPP11
    CXXFLAGS = -std=c++11 -g -O2 -fno-inline-functions -std=c++11 -DSUPPORTS_CPP11 -fPIC
  3. 去掉B模块指定的-DSUPPORTS_CPP11即可。
posted @ 2021-12-07 14:21  一沙世界  阅读(137)  评论(0编辑  收藏  举报