C++ gcc:未定义的引用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22426574/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 00:04:31  来源:igfitidea点击:

gcc: undefined reference to

c++cgcclinkerundefined-reference

提问by jamie_y

I would like to compile this.

我想编译这个。

program.c

程序.c

#include <libavcodec/avcodec.h>

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

Running this

运行这个

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

gives error

给出错误

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

However, avpicture_get_size is defined. Why is this happening?

但是,定义了 avpicture_get_size。为什么会这样?

回答by alk

However, avpicture_get_size is defined.

但是,定义了 avpicture_get_size。

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

不,因为标题 ( <libavcodec/avcodec.h>) 只是声明了它。

The definitionis in the library itself.

定义是库本身。

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

所以你可能想libavcodec在调用 gcc 时添加链接器选项来链接:

-lavcodec


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

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

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

Notlike this:

不是这样的:

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


Update:

更新:

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

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

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

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

对于未存储在链接器标准位置中的库,该选项-L指定了一个额外的搜索路径来查找使用该-l选项指定的库,即libavcodec.x.y.z在本例中。



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

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

回答by Technophile

Are you mixing C and C++? One issue that can occur is that the declarations in the .h file for a .c file need to be surrounded by:

你在混合 C 和 C++ 吗?可能发生的一个问题是 .c 文件的 .h 文件中的声明需要被包围:

#if defined(__cplusplus)
  extern "C" {                 // Make sure we have C-declarations in C++ programs
#endif

and:

和:

#if defined(__cplusplus)
  }
#endif