简体   繁体   English

使用g ++进行编译时“未定义的引用”

[英]'undefined reference to' when compiling with g++

I am trying to link my program with libjpeg. 我正在尝试将我的程序与libjpeg链接。

arm-mandriva-linux-gnueabi-g++ v4l2grab2.cpp -o v4l2grab -Wall -Llib -ljpeg 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 ! 但是,当我将文件重命名为* .c并尝试使用arm-mandriva-linux-gnueabi-gcc (而不是g ++)进行编译时,一切正常

I really need to use it inside C++ code. 我真的需要在C ++代码中使用它。 How can I compile this code with g++ ? 如何使用g ++编译此代码?

It seems you are including a C header without C++ support. 似乎您包括了不带C ++支持的C标头。 Try including the relevant header from within an extern "C" block: 尝试从extern "C"块中包含相关的标头:

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> ). (因为您没有发布任何代码,所以我不确定您尝试包含哪个标头,只是猜到了<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++: 万一您需要从标头中包含<jpeglib.h> ,而标头也应该包含在C中,则可以使用针对C ++的测试将其包装起来:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM