gcc:未定义的引用

2021-12-18 00:00:00 gcc c linker c++ undefined-reference

我想编译这个.

program.c

#include <libavcodec/avcodec.h>

int main(){
    int i = avpicture_get_size(AV_PIX_FMT_RGB24,300,300);
}

运行这个

gcc -I$HOME/ffmpeg/include program.c

报错

/tmp/ccxMLBme.o: In function `main':
program.c:(.text+0x18): undefined reference to `avpicture_get_size'
collect2: ld returned 1 exit status

然而,定义了avpicture_get_size.为什么会发生这种情况?

However, avpicture_get_size is defined. Why is this happening?

推荐答案

但是,定义了 avpicture_get_size.

However, avpicture_get_size is defined.

不,因为标题 () 只是声明它.

No, as the header (<libavcodec/avcodec.h>) just declares it.

定义在图书馆本身.

因此,您可能希望在调用 gcc 时添加链接器选项以链接 libavcodec:

So you might like to add the linker option to link libavcodec when invoking gcc:

-lavcodec


另请注意,需要在命令行在需要它们的文件之后指定库:


Please also note that libraries need to be specified on the command line after the files needing them:

gcc -I$HOME/ffmpeg/include program.c -lavcodec

不是像这样:

gcc -lavcodec -I$HOME/ffmpeg/include program.c


参考Wyzard的评论,完整的命令可能如下所示:


Referring to Wyzard's comment, the complete command might look like this:

gcc -I$HOME/ffmpeg/include program.c -L$HOME/ffmpeg/lib -lavcodec

对于没有存储在链接器标准位置的库,选项 -L 指定一个额外的搜索路径来查找使用 -l 选项指定的库,即 libavcodec.xyz 在这种情况下.

For libraries not stored in the linkers standard location the option -L specifies an additional search path to lookup libraries specified using the -l option, that is libavcodec.x.y.z in this case.

有关 GCC 链接器选项的详细参考,请阅读此处.

For a detailed reference on GCC's linker option, please read here.

相关文章