目的: 无论是交叉编译还是ubuntu上gcc编译的ffmpeg,编译生成include和lib等后,写一个简单的测试程序,那么就涉及到链接问题;
1. 程序调用动态库/静态库3大步骤;
(a) -I指定头文件;
(b) -L指定库路径;
(c) -l库名 (可能不需要);
本文以ffmpeg库为例来测试Makefile指定路径;
提前编译好ffmpeg,位于/usr/local/ffmpeg下;
Makefile中指定路径
在sample下找一个测试例程, 如encode_video.c
Makefile配置如下:
default:
gcc encode_video.c -o encode_video \
-I/usr/local/ffmpeg/include \
-L/usr/local/ffmpeg/lib \
-lavcodec -lavdevice -lavfilter -lavformat -lavutil -lpostproc -lswresample -lswscale若只有-I指定头文件的情况; 打印如下:
/tmp/ccvTv2zi.o: In function `av_make_error_string':
encode_video.c:(.text+0x24): undefined reference to `av_strerror'
/tmp/ccvTv2zi.o: In function `encode':
encode_video.c:(.text+0x79): undefined reference to `avcodec_send_frame'
encode_video.c:(.text+0xc1): undefined reference to `avcodec_receive_packet'
encode_video.c:(.text+0x159): undefined reference to `av_packet_unref'
/tmp/ccvTv2zi.o: In function `main':
encode_video.c:(.text+0x20a): undefined reference to `avcodec_find_encoder_by_name'
encode_video.c:(.text+0x248): undefined reference to `avcodec_alloc_context3'
encode_video.c:(.text+0x288): undefined reference to `av_packet_alloc'
encode_video.c:(.text+0x361): undefined reference to `av_opt_set'
encode_video.c:(.text+0x37c): undefined reference to `avcodec_open2'
encode_video.c:(.text+0x424): undefined reference to `av_frame_alloc'
encode_video.c:(.text+0x4b2): undefined reference to `av_frame_get_buffer'
encode_video.c:(.text+0x516): undefined reference to `av_frame_make_writable'
encode_video.c:(.text+0x77a): undefined reference to `avcodec_free_context'
encode_video.c:(.text+0x789): undefined reference to `av_frame_free'
encode_video.c:(.text+0x798): undefined reference to `av_packet_free'
collect2: error: ld returned 1 exit status
Makefile:3: recipe for target 'default' failed
make: *** [default] Error 1
只有-I指定头文件 和-L指定lib路径;打印同上:
只有-I指定头文件和-l指定库链接名,打印同上;
唯有-I指定头文件,-L指定lib路径,-l指定库链接名时才正常;