为什么gcc链接器看不到我的析构函数?

时间:2013-03-25 02:48:54

标签: c++ gcc linker

我正在尝试按照https://code.google.com/p/wkhtmltopdf/wiki/compilation的说明在Linux机器上编译wkhtmltopdf-qt。

现在我被链接器拒绝链接所困扰。它打印出几个错误,但引起我注意的是:

/home/ubuntu/wkhtmltopdf-qt/lib/libwebcore.a(AccessibilityRenderObject.o): In function `WebCore::AccessibilityRenderObject::~AccessibilityRenderObject()':
AccessibilityRenderObject.cpp:(.text._ZN7WebCore25AccessibilityRenderObjectD0Ev+0x5): undefined reference to `WebCore::AccessibilityRenderObject::~AccessibilityRenderObject()'

至少可以说这很奇怪。首先,因为链接器声称析构函数尝试引用自己和下一个 - 因为它没有这样做!疯狂的事情... 无论如何,一切似乎都被定义了。请观察:

ubuntu@ip-10-245-78-162:~$ nm --defined-only -C -A /home/ubuntu/wkhtmltopdf-qt/lib/libwebcore.a | grep ~AccessibilityRenderObject
/home/ubuntu/wkhtmltopdf-qt/lib/libwebcore.a:AccessibilityRenderObject.o:0000000000000000 T WebCore::AccessibilityRenderObject::~AccessibilityRenderObject()
/home/ubuntu/wkhtmltopdf-qt/lib/libwebcore.a:AccessibilityRenderObject.o:0000000000000000 T WebCore::AccessibilityRenderObject::~AccessibilityRenderObject()
/home/ubuntu/wkhtmltopdf-qt/lib/libwebcore.a:AccessibilityRenderObject.o:0000000000000000 T WebCore::AccessibilityRenderObject::~AccessibilityRenderObject()
ubuntu@ip-10-245-78-162:~/wkhtmltopdf-qt/src/3rdparty/webkit/Source/WebCore/accessibility$

接下来,在AccessibilityRenderObject.cpp文件中:

#include <wtf/unicode/CharacterNames.h>

using namespace std;

namespace WebCore {

using namespace HTMLNames;

AccessibilityRenderObject::AccessibilityRenderObject(RenderObject* renderer)
    : AccessibilityObject()
    , m_renderer(renderer)
    , m_ariaRole(UnknownRole)
    , m_childrenDirty(false)
    , m_roleForMSAA(UnknownRole)
{
    m_role = determineAccessibilityRole();

#ifndef NDEBUG
    m_renderer->setHasAXObject(true);
#endif
}

AccessibilityRenderObject::~AccessibilityRenderObject()
{
    ASSERT(isDetached());
}

因此,析构函数定义得很好。但我走得更远 - 我再次编译了AccessibilityRenderObject.cpp,这次停在预处理器上。当然,析构函数就在那里:

class AccessibilityRenderObject : public AccessibilityObject {
protected:
    AccessibilityRenderObject(RenderObject*);
public:
    static PassRefPtr<AccessibilityRenderObject> create(RenderObject*);
    virtual ~AccessibilityRenderObject();
.
.
.
AccessibilityRenderObject::~AccessibilityRenderObject()
{
    ((void)0);
}

用于编译该文件(以及许多其他文件)的编译行是:

g++ -c -m64 -pipe -Wall -Wextra -Wreturn-type -fno-strict-aliasing -Wcast-align -Wchar-subscripts -Wformat-security -Wreturn-type -Wno-unused-parameter -Wno-sign-compare -Wno-switch -Wno-switch-enum -Wundef -Wmissing-noreturn -Winit-self -Wno-c++0x-compat -ffunction-sections -fdata-sections -O2 -fPIC -fvisibility=hidden -fvisibility-inlines-hidden -D_REENTRANT -DNDEBUG -DBUILDING_QT__=1 -DNDEBUG -DQT_ASCII_CAST_WARNINGS -DENABLE_XSLT=0 -DENABLE_WEB_TIMING=0 -DENABLE_JAVASCRIPT_DEBUGGER=1 -DENABLE_DATABASE=1 -DENABLE_EVENTSOURCE=1 -DENABLE_OFFLINE_WEB_APPLICATIONS=1 -DENABLE_DOM_STORAGE=1 -DENABLE_ICONDATABASE=1 -DENABLE_CHANNEL_MESSAGING=1 -DENABLE_DIRECTORY_UPLOAD=0 -DENABLE_FILE_SYSTEM=0 -DENABLE_QUOTA=0 -DENABLE_SQLITE=1 -DENABLE_DASHBOARD_SUPPORT=0 -DENABLE_FILTERS=1 -DENABLE_XPATH=1 -DENABLE_WCSS=0 -DENABLE_SHARED_WORKERS=1 -DENABLE_WORKERS=1 -DENABLE_XHTMLMP=0 -DENABLE_DETAILS=1 -DENABLE_METER_TAG=1 -DENABLE_PROGRESS_TAG=1 -DENABLE_BLOB=1 -DENABLE_NOTIFICATIONS=1 -DENABLE_INPUT_SPEECH=0 -DENABLE_INSPECTOR=1 -DENABLE_3D_RENDERING=1 -DENABLE_WEB_AUDIO=0 -DENABLE_WEBGL=0 -DENABLE_MEDIA_STATISTICS=0 -DENABLE_VIDEO_TRACK=0 -DENABLE_TOUCH_ICON_LOADING=0 -DENABLE_ANIMATION_API=0 -DENABLE_SVG=1 -DENABLE_SVG_FONTS=1 -DENABLE_SVG_FOREIGN_OBJECT=1 -DENABLE_SVG_ANIMATION=1 -DENABLE_SVG_AS_IMAGE=1 -DENABLE_SVG_USE=1 -DENABLE_DATALIST=1 -DENABLE_TILED_BACKING_STORE=1 -DENABLE_NETSCAPE_PLUGIN_API=1 -DENABLE_WEB_SOCKETS=1 -DWTF_USE_QT_BEARER=1 -DENABLE_TOUCH_EVENTS=1 -DENABLE_VIDEO=0 -DENABLE_VIDEO=0 -DSQLITE_CORE -DSQLITE_OMIT_LOAD_EXTENSION -DSQLITE_OMIT_COMPLETE -DXP_UNIX -DENABLE_NETSCAPE_PLUGIN_METADATA_CACHE=1 -DBUILDING_JavaScriptCore -DBUILDING_WTF -DBUILDING_WEBKIT -DQT_MAKEDLL -D_LARGEFILE64_SOURCE -D_LARGEFILE_SOURCE -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_NETWORK_LIB -DQT_CORE_LIB -I../../../../../mkspecs/linux-g++-64 -I. -I../../../../../include/QtCore -I../../../../../include/QtNetwork -I../../../../../include/QtGui -I../../../../../include -I../JavaScriptCore -I../../Source -I../ThirdParty -I../JavaScriptCore/assembler -I../JavaScriptCore/bytecode -I../JavaScriptCore/bytecompiler -I../JavaScriptCore/heap -I../JavaScriptCore/dfg -I../JavaScriptCore/debugger -I../JavaScriptCore/interpreter -I../JavaScriptCore/jit -I../JavaScriptCore/parser -I../JavaScriptCore/profiler -I../JavaScriptCore/runtime -I../JavaScriptCore/wtf -I../JavaScriptCore/wtf/gobject -I/home/ubuntu/wkhtmltopdf-qt/src/3rdparty/webkit/Source/JavaScriptCore/wtf/symbian -I../JavaScriptCore/wtf/unicode -I../JavaScriptCore/yarr -I../JavaScriptCore/API -I../JavaScriptCore/ForwardingHeaders -I../JavaScriptCore/generated -Ibridge/qt -Ipage/qt -Iplatform/graphics/qt -Iplatform/network/qt -Iplatform/qt -I../WebKit/qt/Api -I../WebKit/qt/WebCoreSupport -I. -Iaccessibility -Ibindings -Ibindings/generic -Ibridge -Icss -Idom -Idom/default -Iediting -Ifileapi -Ihistory -Ihtml -Ihtml/canvas -Ihtml/parser -Ihtml/shadow -Iinspector -Iloader -Iloader/appcache -Iloader/archive -Iloader/cache -Iloader/icon -Imathml -Inotifications -Ipage -Ipage/animation -Iplatform -Iplatform/animation -Iplatform/audio -Iplatform/graphics -Iplatform/graphics/filters -Iplatform/graphics/filters/arm -Iplatform/graphics/texmap -Iplatform/graphics/transforms -Iplatform/image-decoders -Iplatform/leveldb -Iplatform/mock -Iplatform/network -Iplatform/sql -Iplatform/text -Iplatform/text/transcoder -Iplugins -Irendering -Irendering/mathml -Irendering/style -Irendering/svg -Istorage -Isvg -Isvg/animation -Isvg/graphics -Isvg/graphics/filters -Isvg/properties -Itesting -Iwebaudio -Iwebsockets -I/home/ubuntu/wkhtmltopdf-qt/src/3rdparty/webkit/Source/WebCore/wml -Iworkers -Ixml -Ibridge/jsc -Ibindings/js -I/home/ubuntu/wkhtmltopdf-qt/src/3rdparty/webkit/Source/WebCore/bindings/js/specialization -Ibridge/c -Itesting/js -Igenerated -I../../Source -I../../include -I../include/QtWebKit -I../include -I../../../sqlite -I/usr/X11R6/include -I.moc/release-static -o .obj/release-static/AccessibilityRenderObject.o accessibility/AccessibilityRenderObject.cpp

最后,失败的链接器命令行是:

g++ -Wl,-rpath-link,/home/ubuntu/wkhtmltopdf-qt/lib -m64 -Wl,-O1 -Wl,-rpath,/home/ubuntu/wkhtmltopdf-qt/src/3rdparty/webkit/Source/lib -Wl,-rpa
th,/home/ubuntu/wkqt/lib -Wl,-rpath,/home/ubuntu/wkqt/lib -o tst_qwebframe .obj/release-static/tst_qwebframe.o .obj/release-static/qrc_tst_qweb
frame.o    -L/home/ubuntu/wkhtmltopdf-qt/lib -L/usr/X11R6/lib64 -L/home/ubuntu/wkhtmltopdf-qt/lib -lQtWebKit -L../../WebCore/release -L../../Ja
vaScriptCore/release -L/usr/X11R6/lib64 -Wl,--start-group -lwebcore -lQtWebKit -Wl,--end-group -ljscore -lQtTest -lQtGui -lXrender -lXext -lX11
 -lQtNetwork -lQtCore -lm -ldl -lrt -lpthread -lXrender -lXext -lX11 -lm

引用上述析构函数的所有链接器错误都是:

/home/ubuntu/wkhtmltopdf-qt/lib/libwebcore.a(AccessibilityRenderObject.o): In function `WebCore::AccessibilityRenderObject::~AccessibilityRenderObject()':
AccessibilityRenderObject.cpp:(.text._ZN7WebCore25AccessibilityRenderObjectD2Ev+0x3): undefined reference to `vtable for WebCore::AccessibilityRenderObject'
/home/ubuntu/wkhtmltopdf-qt/lib/libwebcore.a(AccessibilityRenderObject.o): In function `WebCore::AccessibilityRenderObject::~AccessibilityRenderObject()':
AccessibilityRenderObject.cpp:(.text._ZN7WebCore25AccessibilityRenderObjectD0Ev+0x5): undefined reference to `WebCore::AccessibilityRenderObject::~AccessibilityRenderObject()'

有许多可疑的链接器错误,例如:

/home/ubuntu/wkhtmltopdf-qt/lib/libwebcore.a(AccessibilityMenuList.o):(.data.rel.ro._ZTIN7WebCore21AccessibilityMenuListE[typeinfo for WebCore:
:AccessibilityMenuList]+0x10): undefined reference to `typeinfo for WebCore::AccessibilityRenderObject'

但据我所知,所有虚拟函数都已正确声明和定义,所有源树中的makefile都没有提到 rtti

任何人都可以向我解释发生了什么事吗?

1 个答案:

答案 0 :(得分:4)

编译命令包括选项:-ffunction-sections -fdata-sections(记录here)。 -ffunction-sections将每个函数放在对象文件的自己的部分中,而不是简单地将它们全部放在一个.text部分中。同样,-fdata-sections适用于全局/静态数据。

这个想法是,通过指定--gc-sectionsgarbage collection of unused input sections),链接器可以在将它们合并到二进制文件的最后.text部分之前丢弃“不可达”部分。简而言之,减少代码大小而不为每个函数使用单独的编译单元。

这就是预感。我不希望它在没有--gc-sections选项的情况下工作。显然没有任何好处,因为没有任何东西被丢弃。为什么会失败?

当实现析构函数时,gcc,clang等使用的C ++ ABI通常会生成3个析构函数变体,结尾为:D0EvD1EvD2Ev,以及作为该类的vtabletypeinfo数据。我的猜测是这些单独的部分没有正确合并。无论这是一个错误,还是链接器(或链接规范)的限制,我都不知道。


在构建共享库时,这也没用,因为无法事先知道将使用哪些符号。使用-fPIC调用编译器,这应该没问题,因为对象只是被存档。但是,-fvisibility选项可能会使问题复杂化。