g ++编译FFMPEG时在Mac上链接错误

S B*_*S B 3 macos ffmpeg g++ linker-errors

Snow Leopard上的g ++会在下面的代码中抛出链接错误

TEST.CPP

#include <iostream>
using namespace std;
#include <libavcodec/avcodec.h>    // required headers
#include <libavformat/avformat.h>
int main(int argc, char**argv) {
    av_register_all();             // offending library call
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

当我尝试使用以下命令编译它时

g++ test.cpp -I/usr/local/include -L/usr/local/lib \
-lavcodec -lavformat -lavutil -lz -lm -o test
Run Code Online (Sandbox Code Playgroud)

我收到错误未定义的符号:"av_register_all()",引自:ccUD1ueX中的_main.l ld:未找到符号collect2:ld返回1退出状态

有趣的是,如果我有一个等效的c代码,test.c

#include <stdio.h>
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
int main(int argc, char**argv) {
    av_register_all();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

gcc编译得很好

gcc test.c -I/usr/local/include -L/usr/local/lib \
-lavcodec -lavformat -lavutil -lz -lm -o test
Run Code Online (Sandbox Code Playgroud)

我使用的是Mac OS X 10.6.5

$ g++ --version
i686-apple-darwin10-g++-4.2.1 (GCC) 4.2.1 (Apple Inc. build 5664)
$ gcc --version
i686-apple-darwin10-gcc-4.2.1 (GCC) 4.2.1 (Apple Inc. build 5664)
Run Code Online (Sandbox Code Playgroud)

FFMPEG的libavcodec,libavformat等是C库,我已经在我的机器上构建它们,如下所示:

./configure --enable-gpl --enable-pthreads --enable-shared \
--disable-doc --enable-libx264
make && sudo make install
Run Code Online (Sandbox Code Playgroud)

正如人们所料,libavformat确实包含符号av_register_all

$ nm /usr/local/lib/libavformat.a | grep av_register_all
0000000000000000 T _av_register_all
00000000000089b0 S _av_register_all.eh
Run Code Online (Sandbox Code Playgroud)

我倾向于相信g ++和gcc对我机器上的库有不同的看法.g ++无法选择合适的库.任何线索?

Syl*_*sne 12

这可能是因为av_register_all函数不在extern "C"块中,因此当C++编译器解释前向声明时,它的名称会被破坏.尝试将您的代码更改为:

#include <iostream>
using namespace std;
extern "C" {
#include <libavcodec/avcodec.h>    // required headers
#include <libavformat/avformat.h>
}
int main(int argc, char**argv) {
    av_register_all();             // offending library call
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

C++编译器使用名称修改来允许使用不同的参数覆盖相同的函数,但不是由C编译器执行(不提供函数覆盖).

通常,用C编写并且可以包含在C++文件中的头应具有以下结构以防止发生此类错误.您应该通知ffmpeg开发人员修改他们的代码:

// Standard includes guards
#ifndef INCLUDED_AVCODEC_H
#define INCLUDED_AVCODEC_H

// Protection against inclusion by a C++ file
#ifdef __cplusplus
extern "C" {
#endif

// C code
// ....

// Closing the protection against inclusion by a C++ file
#ifdef __cplusplus
}
#endif
#endif
Run Code Online (Sandbox Code Playgroud)

[编辑]:我刚刚发现这是在FFmpeg wiki上提到的.