2015-12-10 45 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。