FFmpeg – C++中使用ffmpeg庫

來源:互聯網
上載者:User

http://blog.sina.com.cn/s/blog_8cfe05150100uhm2.html

ffmpeg庫的介面都是c函數,其標頭檔也沒有extern "C"的聲明,所以在cpp檔案裡調用ffmpeg函數要注意了。
一般來說,一個用C寫成的庫如果想被C/C++同時可以使用,那在標頭檔應該加上
#ifdef __cplusplus
extern "C" {
#endif

#ifdef __cplusplus
} // endof extern "C"
#endif

如果檔案名稱是main.c,裡面調用ffmpeg的介面沒有問題;但換成main.cpp後,就會報錯 undefined reference。
這是因為.cpp裡的符號名不是簡單的函數名,而函數後加尾碼標誌。

例如,代碼裡有一句av_register_all()調用
int main(int argc, char** argv)
{
 av_register_all();
}
如果該檔案名稱是 main.c,則main.o裡的符號為 (用nm命令查看)
$ nm  src/main.o
 U _av_register_all

如果該檔案名稱是 main.cpp,則main.o裡的符號為
$ nm src/main.o
  U __Z15av_register_allv
顯然,.c和.cpp的函數符號名是不一樣的。再看ffmpeg庫的符號名
$ nm libavdevice.a | grep register
00000000 T _avdevice_register_all

這裡我們就明白了,如果在.cpp裡調用av_register_all()在連結時將找到不符號,因為.cpp要求的符號名
和ffmpeg庫提供的符號名不一致。

可以這麼解決:
extern "C"
{
#include <libavutil/avutil.h>
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.