Chien-Yu Chan
Chien-Yu Chan

Reputation: 51

FFMPEG library usage for own C++ project

I would like to create my own project using FFMPEG to build a player, but creating a new file to include ffmpeg library seems not easy. I have configure the ffmpeg and make build.

./configure

./make

The ffmpeg hello world program (myffmpeg.c):

#include <libavformat/avformat.h>
int main(int argc, char *argv[]) {
  av_register_all();
  return 0;
}

but it shows

clang: warning: treating 'c' input as 'c++' when in C++ mode, this behavior is deprecated

Undefined symbols for architecture x86_64:

"av_register_all()", referenced from:

_main in myffmpeg-61ec1b.o

ld: symbol(s) not found for architecture x86_64

clang: error: linker command failed with exit code 1 (use -v to see invocation

When I try to link the allformats.o file, which has av_register_all function inside. I got:

clang: warning: treating 'c' input as 'c++' when in C++ mode, this behavior is deprecated Undefined symbols for architecture x86_64: "_av_register_input_format", referenced from: _register_all in allformats.o "_av_register_output_format", referenced from: _register_all in allformats.o "_avcodec_register_all", referenced from: _register_all in allformats.o "_ff_a64_muxer", referenced from: _register_all in allformats.o

...

"_ff_yuv4mpegpipe_muxer", referenced from: _register_all in allformats.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)

It should be my bad Makefile knowledge, could anyone give me some hint on how to call ffmpeg library functions? Or even how to modify the Makefile to build my own program? Thanks!

*Update: It could be my compiling command problem, is it the way to compile?

g++ myffmpeg.c

Upvotes: 2

Views: 5845

Answers (2)

Chien-Yu Chan
Chien-Yu Chan

Reputation: 51

I think I could answer myself.

g++ -o main.o main.c `pkg-config --cflags --libs libavformat`

I don't know the reason now, it is from https://soledadpenades.com/2009/11/24/linking-with-ffmpegs-libav/

We still need the extern, see answer below.

Upvotes: 1

maybe the problem is that c++ is decorating functions. So the same function compiled with C compiler and C++ looks different to linker. try this:

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

this code means that you told your compiler to not decorate functions in that file.

Upvotes: 5

Related Questions