GCC wrong? .c vs. .cpp compilation...


Some times, compiling .c can get what's expected. If changing .c to .cpp directly without revising the contents in the file, the .cpp can't be compiled correctly. What's wrong with GCC???

jiapei@jiapei-laptop:~/MyPrograms/Java/Eclipse/SpeakingMouth/src$ gcc -I/usr/include -L/usr/lib -lavformat -lavcodec tutorial01.c -o tutorial01.o
tutorial01.c: In function ‘main’:
tutorial01.c:135: warning: ‘img_convert’ is deprecated (declared at /usr/include/ffmpeg/avcodec.h:2461)
jiapei@jiapei-laptop:~/MyPrograms/Java/Eclipse/SpeakingMouth/src$ ls
tutorial01.c tutorial01.o


Now, nothing changed for file "tutorial01.c", only except changing the name to "tutorial01.cpp". Everything else including the command line keeps the same as above.


jiapei@jiapei-laptop:~/MyPrograms/Java/Eclipse/SpeakingMouth/src$ gcc -I/usr/include -L/usr/lib -lavformat -lavcodec tutorial01.cpp -o tutorial01.obj
tutorial01.cpp: In function ‘int main(int, char**)’:
tutorial01.cpp:135: warning: ‘int img_convert(AVPicture*, int, const AVPicture*, int, int, int)’ is deprecated (declared at /usr/include/ffmpeg/avcodec.h:2463)
tutorial01.cpp:137: warning: ‘int img_convert(AVPicture*, int, const AVPicture*, int, int, int)’ is deprecated (declared at /usr/include/ffmpeg/avcodec.h:2463)
/tmp/ccgnQ7qM.o: In function `main':
tutorial01.cpp:(.text+0x13b): undefined reference to `av_register_all()'
tutorial01.cpp:(.text+0x16d): undefined reference to `av_open_input_file(AVFormatContext**, char const*, AVInputFormat*, int, AVFormatParameters*)'
tutorial01.cpp:(.text+0x18d): undefined reference to `av_find_stream_info(AVFormatContext*)'
tutorial01.cpp:(.text+0x1ca): undefined reference to `dump_format(AVFormatContext*, int, char const*, int)'
tutorial01.cpp:(.text+0x243): undefined reference to `avcodec_find_decoder(CodecID)'
tutorial01.cpp:(.text+0x28f): undefined reference to `avcodec_open(AVCodecContext*, AVCodec*)'
tutorial01.cpp:(.text+0x2a7): undefined reference to `avcodec_alloc_frame()'
tutorial01.cpp:(.text+0x2af): undefined reference to `avcodec_alloc_frame()'
tutorial01.cpp:(.text+0x2e4): undefined reference to `avpicture_get_size(int, int, int)'
tutorial01.cpp:(.text+0x2f2): undefined reference to `av_malloc(unsigned int)'
tutorial01.cpp:(.text+0x323): undefined reference to `avpicture_fill(AVPicture*, unsigned char*, int, int, int)'
tutorial01.cpp:(.text+0x362): undefined reference to `avcodec_decode_video(AVCodecContext*, AVFrame*, int*, unsigned char const*, int)'
tutorial01.cpp:(.text+0x3a1): undefined reference to `img_convert(AVPicture*, int, AVPicture const*, int, int, int)'
tutorial01.cpp:(.text+0x3f0): undefined reference to `av_read_frame(AVFormatContext*, AVPacket*)'
tutorial01.cpp:(.text+0x408): undefined reference to `av_free(void*)'
tutorial01.cpp:(.text+0x413): undefined reference to `av_free(void*)'
tutorial01.cpp:(.text+0x41e): undefined reference to `av_free(void*)'
tutorial01.cpp:(.text+0x429): undefined reference to `avcodec_close(AVCodecContext*)'
tutorial01.cpp:(.text+0x434): undefined reference to `av_close_input_file(AVFormatContext*)'
/tmp/ccgnQ7qM.o:(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status






gcc interprets files ending in .c as C programs and .cpp as C++ programs. They are not interchangeable.

Is your program a C or C++ program?
Even if the one file is compatible with C++, you would have to use extern "C" to link with functions from C objects/libraries. That is, I think...I'm just writing this off of the top of my head. I think it has something to do with C++'s name mangling being suppressed or something.
Try the same command using g++ instead of gcc.
Topic archived. No new replies allowed.