2017-03-14 12 views
-1

与えられたファイルのビデオ期間を決定するために、私はlibavformatを使用します。次のように私のプログラムが見えます:gccはlibavformatコードをコンパイルしますが、g ++はありません

#include <stdio.h> 
#include <libavformat/avformat.h> 
#include <libavutil/dict.h> 
int main (int argc, char **argv) { 
    AVFormatContext *fmt_ctx = NULL; 
    int ret; 
    if (argc != 2) { 
     printf("usage: %s <input_file>\n", argv[0]); 
     return 1; 
    } 
    av_register_all(); 
    if ((ret = avformat_open_input(&fmt_ctx, argv[1], NULL, NULL))) 
     return ret; 
    int64_t duration = fmt_ctx->duration; 
    int hours, mins, secs; 
    secs = duration/AV_TIME_BASE; 
    mins = secs/60; 
    secs %= 60; 
    hours = mins/60; 
    mins %= 60; 
    printf("Duration: %02d:%02d:%02d\n", hours, mins, secs); 
    avformat_free_context(fmt_ctx); 
    return 0; 
} 

私の問題は、gccがコードだけで罰金をコンパイルしながら、文句なしにもなく、作成したオブジェクトファイルそう++ gはどちらもGCCもグラム++でリンクすることはできないということです。または、より正確には:

gcc -c duration.c 
gcc -o duration duration.o -lavformat 
./duration my_movie.mp4 

作品です。しかし、この

g++ -c duration.C# "works" as in "g++ does not complain" 
g++ -o duration duration.o -lavformat # (gcc produces the same output after compiling with g++) 
duration.o: In function `main': 
duration.c:(.text+0x41): undefined reference to `av_register_all()' 
duration.c:(.text+0x62): undefined reference to `avformat_open_input(AVFormatContext**, char const*, AVInputFormat*, AVDictionary**)' 
duration.c:(.text+0x18c): undefined reference to `avformat_free_context(AVFormatContext*)' 
collect2: error: ld returned 1 exit status 

は動作しません。これは、g ++が(この例では)適切にリンクできるコードを生成しないという結論につながります。

私は実際にg ++でこの作業をしたいと思っています。これはもっと大きなC++プロジェクトの一部であり、gccでこのライブラリを使用するファイルをコンパイルする必要があります。なぜ誰かがg ++がこのプログラムを正しくコンパイルしないのか知っていますか?

+1

多分名前のマングリングの問題ですか?インクルードを 'extern" C "' – Dmitri

+1

でラップしてみてください。ネームマングリングに問題があるようです。あなたは 'extern" C ""を振りかけることができます。あるいは、CとC++が異なる言語であることを受け入れます。 – EOF

+0

なぜCコードを別の言語としてコンパイルするべきだと思いますか? – Olaf

答えて

1

このエラーは、我々が知っておく必要があるすべてを教えてくれる:それはC++の機能だと思っていない限り

duration.c:(.text+0x62): undefined reference to `avformat_open_input(AVFormatContext**, char const*, AVInputFormat*, AVDictionary**)' 

、リンカーは、引数のタイプを知ることはできません。だから、これを実行する必要があります。

extern "C" { 
#include <libavformat/avformat.h> 
#include <libavutil/dict.h> 
} 

通常、ヘッダファイルがそれらでextern "C"部分を持っているでしょう。 FFmpegプロジェクトはこれをやることに興味がないようです。一部のヘッダーには、FFmpeg ticket #3626に記載されているように、C++と互換性のないC構造が含まれている場合があります。

このような問題に遭遇した場合、Cでシムを書く必要があります。

+0

ああ男の子、それは簡単だった!前にこの言語の構造を見たことはありませんでしたが、それは仕事でした、ありがとう! – ryan91

関連する問題