c++ - Clang 链接器报告 "symbol not found",尽管 'nm -m' 显示该名称存在于被链接的库中

标签 c++ boost linker clang

在 OS X (clang) 上,在链接阶段,我的代码中没有找到符号 ( boost::filesystem::portable_posix_name )。这是一个简短的代码片段,其中包含我在代码中调用的函数:

if (boost::filesystem::is_directory(settings.parent_path())
 && boost::filesystem::portable_posix_name(settings.filename().string()))
{ ... }

(如果我的代码片段中需要更多上下文,请告诉我。)

程序编译和链接时使用的链接器命令行包含以下内容:

-L/Users/administrator/boost_1_54_0/lib -lboost_filesystem

... 并且指示的路径和文件名(libboost_filesystem.alibboost_filesystem.dylib )存在于指示的路径中。

链接器找不到 Boost 函数 boost::filesystem::portable_posix_name(std::string const &) :

Undefined symbols for architecture x86_64: "boost::filesystem::portable_posix_name(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)"

但是,我确实看到这个函数存在于 .a 中和 .dylib有问题的文件:

nm -m ./libboost_filesystem.a揭示 00000000000090f0 (__TEXT,__text) external __ZN5boost10filesystem19portable_posix_nameERKSs ,并运行损坏的函数名称 __ZN5boost10filesystem19portable_posix_nameERKSs通过 demangler 线揭示:

_boost::filesystem::portable_posix_name(std::string const&)

...我相信名称前面的下划线是正确的,因为找到的其他函数也有前面的下划线。

(.dylib 文件也包含相同的损坏字符串)。

我已经确认 .a 的架构和 .dylib文件正确:otool -hv ./libboost_filesystem.a显示' x86_64 ' 贯穿始终(对于 .dylib 也是如此)。

为了确保确实找到并链接了库,我在编译命令行中更改了库的名称,如下所示:

-L/Users/administrator/boost_1_54_0/lib -lboost_filesystemm

(注意文件名末尾的双 mm),只是为了确认返回了“找不到库”错误,它是:ld: library not found for -lboost_filesystemm .我还确认了文件系统中唯一出现的 libboost_filesystem.a.dylib文件是给定 Boost 目录树中的文件(一个在 /lib 目录中,另一个大小相同且创建日期/时间几乎相同的文件在 /bin.v2 子目录中);另外,更改 -L/Users/administrator/boost_1_54_0/lib使其成为垃圾的行会导致 ld: library not found for -lboost_filesystem出现错误。

奇怪的是,当我 grep nm -m boost::filesystem::is_directory 存在的输出, 后一个函数的库的符号表中没有结果出现,即使此函数没有链接器错误。这对我来说似乎非常倒退(出现在库中的函数链接正常,但是确实出现在库中的函数给出了链接器错误?) - 但是这个问题改天再说(也许这个函数实际上会出现 undefined symbol 错误,但由于第一个 undefined symbol 错误而没有显示)。

我的问题是:为什么找不到这个符号,尽管正确的库、正确的体系结构和正确的 external符号,正在链接?


为了回应@Sebastian 的回答,我在这里粘贴了链接器的完整命令行。此命令行由 Qt 创建(这是一个 Qt 项目)。

请注意 -lboost_filesystem 列出所有目标文件后出现参数。

/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -headerpad_max_install_names -Wl,-syslibroot,/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk -stdlib=libc++ -mmacosx-version-min=10.7 -o NewGene.app/Contents/MacOS/NewGene main.o newgenemainwindow.o newgenecreateoutput.o newgeneselectvariables.o newgenevariablesummary.o newgenetabwidget.o newgenevariablegroup.o newgenevariables.o newgenevariablegroupsscrollarea.o newgenevariablestoolboxwrapper.o newgenevariablestoolbox.o newgenevariablesummaryscrollarea.o newgenevariablesummarygroup.o kadcolumnselectionbox.o timerangebox.o newgenewidget.o uisettingsmanager.o uimodelmanager.o uidocumentmanager.o uistatusmanager.o globals.o uimanager.o settingchangeresponseindicator.o settingchangerequestindicator.o settingchangeindicator.o projectsettingchangeindicator.o globalsettingchangeindicator.o settingchangeresponseitem.o settingchangerequestitem.o settingchangeitem.o projectsettingchangeitem.o globalsettingchangeitem.o globalsettingchangerequestindicator.o globalsettingchangeresponseindicator.o globalsettingchangerequestitem.o globalsettingchangeresponseitem.o projectsettingchangerequestindicator.o projectsettingchangeresponseindicator.o projectsettingchangerequestitem.o projectsettingchangeresponseitem.o modelchangeresponse.o modelchangerequest.o modelchangeindicator.o modelchangeresponseitem.o modelchangerequestitem.o modelchangeitem.o uiprojectmanager.o newgenefilenames.o uiloggingmanager.o newgeneapplication.o uiallsettings.o uisetting.o uimessager.o uiinputproject.o uioutputproject.o uiinputmodel.o uioutputmodel.o uiallprojectsettings.o uimodel.o uiproject.o uiinputprojectsettings.o uioutputprojectsettings.o uiallglobalsettings.o uiallglobalsettings_list.o uiinputprojectsettings_list.o uioutputprojectsettings_list.o uithreadmanager.o uitriggermanager.o uimodelsettings.o uiinputmodelsettings.o uioutputmodelsettings.o eventloopthreadmanager.o workqueuemanager.o outputprojectworkqueue_base.o inputprojectworkqueue_base.o inputprojectworkqueue.o outputprojectworkqueue.o inputmodelworkqueue_base.o outputmodelworkqueue_base.o inputmodelworkqueue.o outputmodelworkqueue.o inputprojectsettingsworkqueue_base.o outputprojectsettingsworkqueue_base.o globalsettingsworkqueue_base.o inputmodelsettingsworkqueue_base.o outputmodelsettingsworkqueue_base.o globalsettingsworkqueue.o inputmodelsettingsworkqueue.o outputmodelsettingsworkqueue.o inputprojectsettingsworkqueue.o outputprojectsettingsworkqueue.o uiuidatamanager.o uiuiactionmanager.o uimodelactionmanager.o uiprojectmanagerworkqueue_base.o uiprojectmanagerworkqueue.o uiwidgetdatarefresh.o uimessagersingleshot.o modelworkqueue.o uiaction.o variablegroupsetmemberselectionchange.o kadwidgetsscrollarea.o kadspinbox.o kadcountchange.o generateoutput.o newgenedatetimewidget.o timerangechange.o newgenegenerateoutput.o generaloptions.o qrc_NewGeneResources.o moc_newgenemainwindow.o moc_newgenecreateoutput.o moc_newgeneselectvariables.o moc_newgenevariablesummary.o moc_newgenetabwidget.o moc_newgenevariablegroup.o moc_newgenevariables.o moc_newgenevariablegroupsscrollarea.o moc_newgenevariablestoolboxwrapper.o moc_newgenevariablestoolbox.o moc_newgenevariablesummaryscrollarea.o moc_newgenevariablesummarygroup.o moc_kadcolumnselectionbox.o moc_timerangebox.o moc_uisettingsmanager.o moc_uimodelmanager.o moc_uidocumentmanager.o moc_uistatusmanager.o moc_globalsettingchangerequestindicator.o moc_globalsettingchangeresponseindicator.o moc_globalsettingchangerequestitem.o moc_globalsettingchangeresponseitem.o moc_projectsettingchangerequestindicator.o moc_projectsettingchangeresponseindicator.o moc_projectsettingchangerequestitem.o moc_projectsettingchangeresponseitem.o moc_modelchangeresponse.o moc_modelchangerequest.o moc_modelchangeindicator.o moc_modelchangeresponseitem.o moc_modelchangerequestitem.o moc_modelchangeitem.o moc_uiprojectmanager.o moc_uiloggingmanager.o moc_newgeneapplication.o moc_uimessager.o moc_uiinputproject.o moc_uioutputproject.o moc_uiinputmodel.o moc_uioutputmodel.o moc_uiinputprojectsettings.o moc_uioutputprojectsettings.o moc_uiallglobalsettings.o moc_uithreadmanager.o moc_uitriggermanager.o moc_uiinputmodelsettings.o moc_uioutputmodelsettings.o moc_workqueuemanager.o moc_uiuidatamanager.o moc_uiuiactionmanager.o moc_uimodelactionmanager.o moc_kadwidgetsscrollarea.o moc_kadspinbox.o moc_newgenedatetimewidget.o moc_newgenegenerateoutput.o
-F/Users/administrator/Qt/5.1.1/clang_64/lib -L/Users/administrator/newgene/NewGeneUI/NewGene/../../NewGeneBackEnd/OSX/NewGene/DerivedData/NewGene/Build/Products/Debug -lNewGene -L/Users/administrator/boost_1_54_0/lib -lboost_filesystem -framework QtWidgets -framework QtGui -framework QtCore -framework OpenGL -framework AGL

最佳答案

在链接器命令行中,您必须将库(“-l”)放在引用该符号的目标文件之后。 也许您已经这样做了,但是您遇到了另一个问题。但由于您没有显示整个命令行,我们无法知道。

关于c++ - Clang 链接器报告 "symbol not found",尽管 'nm -m' 显示该名称存在于被链接的库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20599721/

相关文章:

c++ - Boost 字符串处理函数是否曾经持有全局锁?

c++ - 在哪里可以获得有关 Visual Studio 中 C/C++ 链接器的信息?

c++ - "undefined reference to"使用模板时链接器错误

c++ - 如何使用多核而不是多线程进行编程?

c++ - Boost Thread - 创建一个没有 join() 的线程

c++ - boost :msm 中没有默认构造函数的状态机

c++ - boost::program_options 值 vector 与 zero_token

c++ - 为什么 Qt Creator 不能链接到我自己的代码?

c++ - 带有作用域链的 JS_EvaluateScript

c++ - 从弧度到度的转换