2015-12-10 151 views
0

我不是很熟悉链接,特别是在* nix/Mac上。如何正确链接?

我想手动链接二进制文件来执行一些测试。 我有2套库,我需要链接我的二进制文件:第一个库安装在/ usr/local/lib,它被称为cppunit,第二个安装在〜/ mylib/lib,它被称为libmylib。

所以我抓住链接命令相应地修改它,并试图运行二进制文件。不幸的是我得到了它读取错误消息:

使dyld:库未加载:在/ usr/local/lib目录/的libmylib

不幸的是我相信,这样它读取我不能修改链接命令:

-L/usr/local/lib/cppunit -L~/mylib/lib/mylib 

那么,我该如何修改链接命令,以便链接器能够找到它的位置?

下面是完整的链接命令我想:

g++ -mmacosx-version-min=10.6 -o test_gui test_gui_asserthelper.o test_gui_test.o test_gui_testableframe.o test_gui_rect.o test_gui_size.o test_gui_point.o test_gui_region.o test_gui_bitmap.o test_gui_colour.o test_gui_desktopenv.o test_gui_ellipsization.o test_gui_measuring.o test_gui_affinematrix.o test_gui_boundingbox.o test_gui_config.o test_gui_bitmapcomboboxtest.o test_gui_bitmaptogglebuttontest.o test_gui_bookctrlbasetest.o test_gui_buttontest.o test_gui_checkboxtest.o test_gui_checklistboxtest.o test_gui_choicebooktest.o test_gui_choicetest.o test_gui_comboboxtest.o test_gui_dataviewctrltest.o test_gui_datepickerctrltest.o test_gui_frametest.o test_gui_gaugetest.o test_gui_gridtest.o test_gui_headerctrltest.o test_gui_htmllboxtest.o test_gui_hyperlinkctrltest.o test_gui_itemcontainertest.o test_gui_label.o test_gui_listbasetest.o test_gui_listbooktest.o test_gui_listboxtest.o test_gui_listctrltest.o test_gui_listviewtest.o test_gui_markuptest.o test_gui_notebooktest.o test_gui_ownerdrawncomboboxtest.o test_gui_pickerbasetest.o test_gui_pickertest.o test_gui_radioboxtest.o test_gui_radiobuttontest.o test_gui_rearrangelisttest.o test_gui_richtextctrltest.o test_gui_searchctrltest.o test_gui_simplebooktest.o test_gui_slidertest.o test_gui_spinctrldbltest.o test_gui_spinctrltest.o test_gui_textctrltest.o test_gui_textentrytest.o test_gui_togglebuttontest.o test_gui_toolbooktest.o test_gui_treebooktest.o test_gui_treectrltest.o test_gui_treelistctrltest.o test_gui_virtlistctrltest.o test_gui_webtest.o test_gui_windowtest.o test_gui_dialogtest.o test_gui_clone.o test_gui_evtlooptest.o test_gui_propagation.o test_gui_keyboard.o test_gui_exec.o test_gui_fonttest.o test_gui_image.o test_gui_rawbmp.o test_gui_htmlparser.o test_gui_htmlwindow.o test_gui_accelentry.o test_gui_menu.o test_gui_guifuncs.o test_gui_selstoretest.o test_gui_garbage.o test_gui_safearrayconverttest.o test_gui_settings.o test_gui_socket.o test_gui_boxsizer.o test_gui_wrapsizer.o test_gui_toplevel.o test_gui_valnum.o test_gui_clientsize.o test_gui_setsize.o test_gui_xrctest.o -L/Users/AlenaKorot/wxWidgets3.0/buildMac/lib -ldl -arch i386 -framework IOKit -framework Carbon -framework Cocoa -framework AudioToolbox -framework System -framework OpenGL -lwx_osx_cocoau_webview-3.0 -lwx_osx_cocoau_richtext-3.0 -lwx_osx_cocoau_media-3.0 -framework QTKit -lwx_osx_cocoau_xrc-3.0 -lwx_baseu_xml-3.0 -lexpat -lwx_osx_cocoau_adv-3.0 -lwx_osx_cocoau_html-3.0 -lwx_osx_cocoau_core-3.0 -lwx_baseu_net-3.0 -lwx_baseu-3.0 -lwxtiff-3.0 -lwxjpeg-3.0 -lwxpng-3.0 -framework WebKit -lwxregexu-3.0 -L/usr/local/lib -lcppunit -arch i386 -framework IOKit -framework Carbon -framework Cocoa -framework AudioToolbox -framework System -framework OpenGL -lz -lpthread -liconv -lz -lpthread -liconv 
+0

你能找到任何地方吗? – BobTuckerman

+0

@BobTuckerman,是的,我做到了。显然有一个混合的目录。 – Igor

+0

不错,想补充一点,作为答案? – BobTuckerman

回答

1

本质上来说,问题是,你的库不在默认库路径,G ++。你必须明确地将它添加到g ++的参数列表中以知道它在哪里。

1)添加mylib中编译的源

通常,这是一个.A(编译的源记录)或以.o(编译的源)文件。找出它在'mylib'库中的位置,并将其添加到参数列表中,与其他.o文件列出的方式相同。

g++ <blah blah> -o myfile /home/username/mylib/mylib.o 

当你开始,包括从资料库中的东西,你会得到编译错误,如果头包括不在你的G ++参数列表。

2)添加 'MYLIB' 报头包括

查找在mylib中目录中包括目录。如果您不添加此内容,则只要您尝试从库中获取某些内容,就会收到编译错误。

使用-I标志将其添加到路径中。像这样:

g++ <blah blah> -I/home/username/mylib/include 

最后,如果您没有使用makefile进行编译,我建议为您的目标放置一个简单的makefile。