如何排除故障:未定义的对 `non-virtual thunk to ...` 的引用

boa*_*der 5 c++ g++

我想弄清楚如何进一步解决这个问题。如果有意义的话,我还想知道如何安装较新版本的 ld。所有涉及的包管理器都告诉我我是最新的。

该代码在 ubuntu 12.04 和 12.10 上使用 g++ (4.7.2) 编译、链接和运行,但无法在 FC17 上编译并出现此错误。

ArchiveServiceLib/debug-posix/libArchiveLib.a(NamedIflTiffCache.o):(.rodata._ZTV26UnlockingGenericFileHandle[_ZTV26UnlockingGenericFileHandle]+0x58): undefined reference to `IHawk::EncryptedHandle::OnNewSecretKey(IHawk::IHPGP::SecretKey&)'
ArchiveServiceLib/debug-posix/libArchiveLib.a(NamedIflTiffCache.o):(.rodata._ZTV26UnlockingGenericFileHandle[_ZTV26UnlockingGenericFileHandle]+0x8c): undefined reference to `non-virtual thunk to IHawk::EncryptedHandle::OnNewSecretKey(IHawk::IHPGP::SecretKey&)'
Run Code Online (Sandbox Code Playgroud)

ld的版本:

12.04 only reports          2.22   (no indication other than 2.22)
12.10 reports               2.22.90.20120924
fedora17 reports            2.22.52.0.1-10.fc17 20120131
Run Code Online (Sandbox Code Playgroud)

g++ 的版本:

Ubuntu 12.04    (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
Ubuntu 12.10    (Ubuntu/Linaro 4.7.2-2ubuntu1) 4.7.2
FC 17           (GCC) 4.7.2 20120921 (Red Hat 4.7.2-2)
Run Code Online (Sandbox Code Playgroud)

包含此方法的所有类的声明说:

../Include/IHawkLib/IHPGP.h:            virtual bool OnNewSecretKey( SecretKey &skey ) = 0;
../Include/IHawkLib/PgpPkidParser.h:            virtual bool OnNewSecretKey( SecretKey &skey )                  {return true;}
../Include/IHawkLib/EncryptedFileHandle.h:              virtual bool OnNewSecretKey( SecretKey &skey );
Run Code Online (Sandbox Code Playgroud)

但这就像 g++ 忘记将虚拟部分传递给 ld 以便它可以在链接/ld 时间解析它。这似乎发生在 2002 年和 2009 年,也许还有其他几次,但在这种情况下,后来的版本似乎已经解决了这个问题。这一次,它似乎是特定于平台的,鉴于它令人不安的代码,这毫无意义。

构建错误来自的用法是:

    std::auto_ptr<IHawk::EncryptedFileHandle> apTmgHandle (GetFileHandle(filename, true, false, pKeyServer));
    if (apTmgHandle.get()){
Run Code Online (Sandbox Code Playgroud)

类派生如下:

class EncryptedFileHandle : public EncryptedHandle {
....
    // Does not mention OnNewSecretKey
....
}

class EncryptedHandle : public IHawk::GenericHandle, protected IHawk::IHPGP {
....
    virtual bool OnNewSecretKey( SecretKey &skey );  // Has a concrete implementation
....
}

class IHPGP {
....
    virtual bool OnNewSecretKey( SecretKey &skey ) = 0;
....
}

class GenericHandle {
....
    // Has no clue about OnNewSecretKey
....
}
Run Code Online (Sandbox Code Playgroud)

链接器命令在所有平台上都是这样的,我们使用 scons 并且到目前为止已经能够与平台无关......(对不起,长线,只是不想冒险弄乱它的拼写错误:

g++ -o debug-posix/ArchiveService -g debug-posix/StdAfx.o debug-posix/ArchiveService_PosixMain.o debug-posix/WebCommands.o -L/usr/local/lib -L/usr/lib/mysql -L/home/mjones/C++/ifl/src/IHDB/debug-posix -L/home/mjones/C++/ifl/src/IHDB -L/home/mjones/C++/ifl/src/XMLib/debug-posix -L/home/mjones/C++/ifl/src/XMLib -L/home/mjones/C++/ifl/src/IHawkLib/debug-posix -L/home/mjones/C++/ifl/src/IHawkLib -L/home/mjones/C++/ifl/src/ImageCore/debug-posix -L/home/mjones/C++/ifl/src/ImageCore -L/home/mjones/C++/ifl/libraries/z39.50/ZExtensions/debug-posix -L/home/mjones/C++/ifl/libraries/z39.50/ZExtensions -L/home/mjones/C++/ifl/src/ServerGuts/debug-posix -L/home/mjones/C++/ifl/src/ServerGuts -L/home/mjones/C++/ifl/libraries/CRUSHER/debug-posix -L/home/mjones/C++/ifl/libraries/CRUSHER -L/home/mjones/C++/ifl/libraries/jsoncpp/debug-posix -L/home/mjones/C++/ifl/libraries/jsoncpp ArchiveServiceLib/debug-posix/libArchiveLib.a -lIHDB -lXMLib -lIHawk -lImageCore -lZExtensions -lServerGuts -lCrusher -ljson -lboost_filesystem-mt -lboost_thread-mt -lboost_regex-mt -lz -lMagickWand -lcrypto++ -lcppunit -llog4cplus -lyaz -lpodofo -lmysqlclient -lxerces-c -ljpeg -lpng -ltiff
Run Code Online (Sandbox Code Playgroud)

Die*_*ühl 6

解决问题的第一步是定位到IHawk::EncryptedHandle定义的地方。这可以nm在目标文件上使用,例如:

nm -po *.o | c++filt | grep IHawk::EncryptedHandle | grep -v ' U ' | less
Run Code Online (Sandbox Code Playgroud)

如果定义来自库,您可以添加相应的库或在适当的目录中使用*.a*.so。一旦找到符号并位于库中(由于未定义的引用来自库,因此丢失的符号很可能也在库中),您需要确保在之后指定具有丢失符号的库引用它的那个。我已经有一段时间没有看到它发生了,但是如果符号来自同一个库,您可能需要ranlib在该库上运行。