2013-03-25 152 views
9

這裏是我的問題:Libav鏈接錯誤:未定義的引用

  • 我建立從源代碼的ffmpeg(1.2版),libav *庫在/ usr/local/lib目錄,他們是靜態
  • 我編譯一個ns3(www.nsnam.org)模塊,所以我唯一的控制鏈接器是通過env變量LINKFLAGS
  • 在源文件中,頭文件位於「extern C」塊中,所以它不是通常的g ++名稱損壞
  • 我設置了LINKFLAGS =「 - I/usr/local/include/libavformat -I/usr/local/include/libavcodec -I/usr/local/include/l ibavutil -L/usr/local/lib -lavformat -lavcodec -lavutil「,鏈接器似乎無法找到任何我調用的libav *函數(我得到了很多」未定義的引用「,然後是」collect2:error :ld返回狀態1「

任何人都可以幫我嗎?謝謝...

編輯:這裏有一些未定義的參考消息:

./libns3.14.1-qoe-monitor-debug.so: undefined reference to `av_guess_format' 
    ./libns3.14.1-qoe-monitor-debug.so: undefined reference to `av_read_frame' 
    ./libns3.14.1-qoe-monitor-debug.so: undefined reference to `avformat_write_header' 
    ./libns3.14.1-qoe-monitor-debug.so: undefined reference to `av_interleaved_write_frame' 
    ./libns3.14.1-qoe-monitor-debug.so: undefined reference to `av_find_stream_info' 
    ./libns3.14.1-qoe-monitor-debug.so: undefined reference to `av_register_all' 
    ./libns3.14.1-qoe-monitor-debug.so: undefined reference to `av_init_packet' 
    ./libns3.14.1-qoe-monitor-debug.so: undefined reference to `avformat_alloc_context' 
    ./libns3.14.1-qoe-monitor-debug.so: undefined reference to `av_dump_format' 
    ./libns3.14.1-qoe-monitor-debug.so: undefined reference to `avio_close' 

EDIT2:這裏是 「構建失敗」 後,我得到消息:

-> task in 'scratch-simulator' failed (exit status 1): 
{task 53952272: cxxprogram scratch-simulator.cc.1.o -> scratch-simulator} 
['/usr/bin/g++', '-I/usr/local/include/libavcodec', '-I/usr/local/include/libavformat/', 
'-I/usr/local/include/libavutil/', '-L/usr/local/lib', '-I/usr/local 
/include/libavcodec', '-I/usr/local/include/libavformat/', '-I/usr/local/include 
/libavutil/', '-L/usr/local/lib', '-pthread', '-pthread', '-Wl,-z,relro', 
'scratch/scratch-simulator.cc.1.o', '-o', '/home/fede/Thesis/ns-allinone-3.14.1 
/ns-3.14.1/build/scratch/scratch-simulator', '-Wl,-Bstatic', '-Wl,-Bdynamic', 
'-Wl,--no-as-needed', '-L.', '-L.', '-L.', '-L.', '-L.', '-L.', '-L.', '-L.', '-L.', 
'-L.', '-L.', '-L.', '-L.', '-L.', '-L.', '-L.', '-L.', '-L.', '-L.', '-L.', '-L.', 
'-L.', '-L.', '-L.', '-L.', '-L.', '-L.', '-L.', '-L.', '-L.', '-L.', '-L.', '-L.', 
'-L.', '-L.', '-L.', '-L.', '-L.', '-L/usr/lib', '-lns3.14.1-test-debug', '-lns3.14.1- 
csma-layout-debug', '-lns3.14.1-point-to-point-layout-debug', '-lns3.14.1-netanim- 
debug', '-lns3.14.1-lte-debug', '-lns3.14.1-spectrum-debug', '-lns3.14.1-antenna- 
debug', '-lns3.14.1-aodv-debug', '-lns3.14.1-dsdv-debug', '-lns3.14.1-dsr-debug', 
'-lns3.14.1-mesh-debug', '-lns3.14.1-olsr-debug', '-lns3.14.1-csma-debug', '-lns3.14.1- 
wimax-debug', '-lns3.14.1-applications-debug', '-lns3.14.1-virtual-net-device-debug', 
'-lns3.14.1-uan-debug', '-lns3.14.1-energy-debug', '-lns3.14.1-flow-monitor-debug', 
'-lns3.14.1-nix-vector-routing-debug', '-lns3.14.1-tap-bridge-debug', '-lns3.14.1- 
visualizer-debug', '-lns3.14.1-internet-debug', '-lns3.14.1-bridge-debug', '-lns3.14.1- 
point-to-point-debug', '-lns3.14.1-mpi-debug', '-lns3.14.1-wifi-debug', '-lns3.14.1- 
buildings-debug', '-lns3.14.1-propagation-debug', '-lns3.14.1-mobility-debug', 
'-lns3.14.1-config-store-debug', '-lns3.14.1-tools-debug', '-lns3.14.1-stats-debug', 
'-lns3.14.1-emu-debug', '-lns3.14.1-topology-read-debug', '-lns3.14.1-network-debug', 
'-lns3.14.1-qoe-monitor-debug', '-lns3.14.1-core-debug', '-lrt', '-lgsl', 
'-lgslcblas', '-lm', '-ldl', '-lgtk-x11-2.0', '-lgdk-x11-2.0', '-latk-1.0', 
'-lgio-2.0', '-lpangoft2-1.0', '-lpangocairo-1.0', '-lgdk_pixbuf-2.0', '-lcairo', 
'-lpango-1.0', '-lfreetype', '-lfontconfig', '-lgobject-2.0', '-lglib-2.0', '-lxml2', 
'-lpython2.7'] 
+0

是什麼幾個未定義的引用?你能證實他們住在libav * .a嗎?另外,'-I'標誌適用於編譯時間;你可以把它們放在LINKFLAGS變量之外。另一個想法:通過插入像'-lblahblah'這樣的虛假信息來驗證鏈接器是否遵守LINKFLAGS。除非你的系統上真的有libblahblah.a,否則鏈接器應該抱怨它找不到庫。 – 2013-03-26 06:45:34

+0

如果我在LINKFLAGS中添加一個僞造庫,鏈接器會發出抱怨。 當我調用nm mux.o(mux。c是我調用的一些函數的源文件),我用T標誌獲得函數,並用U標誌函數,但是這兩種類型都是「未定義的引用」。 例如,avformat_write_header顯示爲T,av_guess_format顯示爲U,但我同時獲得了「未定義的avformat_write_header引用」和「未定義的av_guess_format引用」 – Fedech 2013-03-26 18:36:54

+0

感謝您提供更多數據 - 只需盡職盡責地獲取感受你的環境。當你執行'nm /usr/local/lib/libavformat.a | grep avformat_write_header',該函數是否顯示出'T'和一個地址?這是它應該居住的圖書館(並且該函數出現在我的本地副本中)。 – 2013-03-26 23:30:24

回答

21

的庫是C.編譯爲C++時,庫頭文件不包含extern "C"。在C++源代碼做到這一點:

extern "C" { 
#include <libavcodec/avcodec.h> 
#include <libavformat/avformat.h> 
} 

,或者更一般地,對於混合的C和C++源:

#ifdef __cplusplus 
extern "C" { 
#endif 
#include <libavcodec/avcodec.h> 
#include <libavformat/avformat.h> 
#ifdef __cplusplus 
} 
#endif 

而且你應該罰款。

1

我有這個問題av_find_stream_info和問題是這個功能已被棄用。問題是我在盒子上安裝了兩個ffmpeg,它從一個安裝中讀取頭文件,並使用來自不同安裝的庫。

刪除系統默認庫後,問題解決。

更新:對於Ubuntu的我沒下:

$ sudo dpkg -r libavcodec53 
$ sudo dpkg -r libavformat53 
$ sudo dpkg -r libavutil51 
$ sudo dpkg -r libswscale2 
+0

嗨, 你是如何刪除系統默認庫的?我的意思是在哪裏找到他們? – user2452253 2014-10-23 10:24:28

+1

我在Ubuntu上遇到了這個問題,並使用'dpkg -r'刪除了ffmpeg軟件包。用一組命令更新我的答案 – 2014-10-26 17:35:53

1

只是碰到這種問題,在我自己的搜索來了。

在waf構建系統中,應該使用STLIB和STLIBPATH來表示靜態庫名稱和路徑,而不是使用LINKFLAGS。請注意,您應該將STLIB設置爲不帶前綴或擴展名的靜態庫名稱列表。

在WScript的文件中,添加

conf.env.append_value("STLIBPATH", ["/usr/local/lib"]) 
conf.env.append_value("STLIB", ["av*,av**"])