簡體   English   中英

g ++編譯FFMPEG時在Mac上鏈接錯誤

[英]g++ Linking Error on Mac while compiling FFMPEG

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;
}

當我嘗試使用以下命令編譯它時

g++ test.cpp -I/usr/local/include -L/usr/local/lib \
-lavcodec -lavformat -lavutil -lz -lm -o test

我收到錯誤未定義的符號:“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;
}

gcc編譯得很好

gcc test.c -I/usr/local/include -L/usr/local/lib \
-lavcodec -lavformat -lavutil -lz -lm -o test

我使用的是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)

FFMPEG的libavcodec,libavformat等是C庫,我已經在我的機器上構建它們,如下所示:

./configure --enable-gpl --enable-pthreads --enable-shared \
--disable-doc --enable-libx264
make && sudo make install

正如人們所料,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

我傾向於相信g ++和gcc對我機器上的庫有不同的看法。 g ++無法選擇合適的庫。 任何線索?

這可能是因為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;
}

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

[編輯]:我剛剛發現這是在FFmpeg wiki上提到的。

如果你的目標是安裝ffmpeg ,你可以隨時使用MacPorts 以下適用於我:

 sudo port install ffmpeg

您會發現許多針對UNIX的開源項目都會針對特定於Linux的假設,這些假設需要在Mac上正確配置和安裝補丁。 復制確定和創建此類補丁的工作通常是浪費時間,這就是使用MacPorts更有意義的原因。 如果您想知道所需的具體修復是什么,您可以檢查portfile以找出應用了哪些源代碼補丁以及對構建命令的任何修改。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM