vitperov
vitperov

Reputation: 1397

'undefined reference to' when compiling with g++

I am trying to link my program with libjpeg.

arm-mandriva-linux-gnueabi-g++ v4l2grab2.cpp -o v4l2grab -Wall -Llib -ljpeg

Errors:

v4l2grab2.cpp:(.text+0xa28): undefined reference to `jpeg_std_error(jpeg_error_mgr*)'
v4l2grab2.cpp:(.text+0xa44): undefined reference to `jpeg_CreateCompress(jpeg_compress_struct*, int, unsigned int)'
v4l2grab2.cpp:(.text+0xa54): undefined reference to `jpeg_stdio_dest(jpeg_compress_struct*, _IO_FILE*)'
v4l2grab2.cpp:(.text+0xa88): undefined reference to `jpeg_set_defaults(jpeg_compress_struct*)'
v4l2grab2.cpp:(.text+0xaa4): undefined reference to `jpeg_set_quality(jpeg_compress_struct*, int, int)'
v4l2grab2.cpp:(.text+0xab4): undefined reference to `jpeg_start_compress(jpeg_compress_struct*, int)'
v4l2grab2.cpp:(.text+0xaf0): undefined reference to `jpeg_write_scanlines(jpeg_compress_struct*, unsigned char**, unsigned int)'
v4l2grab2.cpp:(.text+0xb1c): undefined reference to `jpeg_finish_compress(jpeg_compress_struct*)'
v4l2grab2.cpp:(.text+0xb28): undefined reference to `jpeg_destroy_compress(jpeg_compress_struct*)'

But, when i rename the file to *.c and try to compile it with arm-mandriva-linux-gnueabi-gcc (instead of g++) everything works!

I really need to use it inside C++ code. How can I compile this code with g++?

Upvotes: 1

Views: 2224

Answers (1)

Dietmar Kühl
Dietmar Kühl

Reputation: 153830

It seems you are including a C header without C++ support. Try including the relevant header from within an extern "C" block:

extern "C" {
#include <jpeglib.h>
}

(since you didn't post any code I'm not sure which header you try to include and just guessed <jpeglib.h>).

In case you need to include <jpeglib.h> from a header which should also be viable to be included from C, you'd wrap it with a test for C++:

#ifdef __cplusplus
extern "C" {
#endif
#include <jpeglib.h>
#ifdef __cplusplus
}
#endif

Upvotes: 7

Related Questions