与えられたファイルのビデオ期間を決定するために、私は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 ++がこのプログラムを正しくコンパイルしないのか知っていますか?
多分名前のマングリングの問題ですか?インクルードを 'extern" C "' – Dmitri
でラップしてみてください。ネームマングリングに問題があるようです。あなたは 'extern" C ""を振りかけることができます。あるいは、CとC++が異なる言語であることを受け入れます。 – EOF
なぜCコードを別の言語としてコンパイルするべきだと思いますか? – Olaf