You are not logged in.
Pages: 1
Trying to make some simple jpeg things in c++ or c
but i am wondering why this c files compiles
#include <stdlib.h>
#include <stdio.h>
//#include <iostream>
#include <jpeglib.h>
//using namespace std;
int main(void){
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_decompress(&cinfo);
return 0;
}
while the 100 % identic file doesnt compile as a c++ file both files are compiled with
c-file
gcc test.c /usr/lib/libjpeg.a -o test
c++-file
gcc test.cc /usr/lib/libjpeg.a -o test
Offline
Offline
/tmp/cc2gfkRW.o(.text+0x29): In function `main':
: undefined reference to `jpeg_std_error(jpeg_error_mgr*)'
/tmp/cc2gfkRW.o(.text+0x4d): In function `main':
: undefined reference to `jpeg_CreateDecompress(jpeg_decompress_struct*, int, unsigned int)'
/tmp/cc2gfkRW.o(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
Offline
For your information -
using gcc:
1) run gcc -print-search-dirs and take note of the output. these directories are searched by default for include files and/or libraries. when specifying a library to include, the correct way to do it is with the "-l" (lowercase L) flag and the name of the library, without "lib" and ".a". So gcc -lX is used for libX.a. This searches in the path explained above. If the lib is not in that path, you can add a path with the -L flag. So -L/home/me/mylibs will search for libraries in that directory.
you should use -ljpeg instead of the path to libjpeg.a
about the errors, I'm not exactly sure, but I'll look at it in a bit
Offline
Ah ok, looks like the linker errors have to do with C++ name mangling. The jpeg names are getting mangled and not matching to the C library.
This happens alot. When linking against an external library that is compiled in C, you need to tell the compiler to handle the header files differently.
extern "C" {
#include <jpeglib.h>
}
the compiler sees this as "this header has external C code" and does not mangle the names
Offline
tried a little bit of every thing now both by linking correct :oops:
and the extern c thing
none of them seems to work
with extern c i get
/tmp/cciuwPw3.o(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
Offline
make sure you put the extern "C" chunk only around jpeglib.h - repaste the code from the original post with what you have now and the commands you're using to compile.
Offline
#include <stdlib.h>
#include <stdio.h>
extern "C" {
#include <jpeglib.h>
}
int main(void){
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_decompress(&cinfo);
return 0;
}
compile command:
gcc test.cc -ljpeg -o test
feed back
/tmp/ccEA9NTl.o(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
and tnx for ur help so far
Offline
try this: C++ replaces the old C standard headers with ones of their own, just to make everything work all nice:
#include <cstdlib>
#include <cstdio>
extern "C" {
#include <jpeglib.h>
}
int main(void)
{
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_decompress(&cinfo);
return 0;
}
the C headers bacially migrate from HEADER_NAME.h to cHEADER_NAME under C++ - that *may* work.
Offline
Weird i still get the same error i even copy pasted from ur post
Offline
The solution is very easy.
Use g++ to compile c++ files. Use gcc to compile c files.
__gxx_personality_v0 is a reference to a function described by the libstdc++.
You also can use gcc passing by -lstdc++ as linking option.
// STi
Ability is nothing without opportunity.
Offline
The solution is very easy.
Use g++ to compile c++ files. Use gcc to compile c files.
__gxx_personality_v0 is a reference to a function described by the libstdc++.
You also can use gcc passing by -lstdc++ as linking option.
// STi
heh, wow that was my oversight... I totally missed that in the posts
Offline
Someitimes small things can cause huge troubles . And havn't we all been in the situation to think about a stupid problem for hours, and a guy just passes by and says something so simple that you didn't even think on this?
// STi
Ability is nothing without opportunity.
Offline
sweet it works once again tnx alot
Offline
Was me a pleasure
// STi
Ability is nothing without opportunity.
Offline
Pages: 1