You are not logged in.

#1 2005-01-19 19:37:05

Blaster
Member
Registered: 2004-09-17
Posts: 49

libjpeg c / c++

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

#2 2005-01-19 19:43:45

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: libjpeg c / c++

what errors does it give you?

Offline

#3 2005-01-19 21:09:23

Blaster
Member
Registered: 2004-09-17
Posts: 49

Re: libjpeg c / c++

/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
roll

Offline

#4 2005-01-20 00:31:10

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: libjpeg c / c++

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

#5 2005-01-20 00:42:46

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: libjpeg c / c++

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

#6 2005-01-20 09:21:52

Blaster
Member
Registered: 2004-09-17
Posts: 49

Re: libjpeg c / c++

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

#7 2005-01-20 19:33:56

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: libjpeg c / c++

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

#8 2005-01-20 20:18:40

Blaster
Member
Registered: 2004-09-17
Posts: 49

Re: libjpeg c / c++

#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  big_smile

Offline

#9 2005-01-21 16:09:36

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: libjpeg c / c++

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

#10 2005-01-24 21:07:15

Blaster
Member
Registered: 2004-09-17
Posts: 49

Re: libjpeg c / c++

Weird i still get the same error i even copy pasted from ur post  sad

Offline

#11 2005-01-25 09:02:22

STiAT
Member
From: Vienna, Austria
Registered: 2004-12-23
Posts: 603

Re: libjpeg c / c++

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

#12 2005-01-25 23:37:53

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: libjpeg c / c++

STiAT wrote:

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

#13 2005-01-26 07:26:16

STiAT
Member
From: Vienna, Austria
Registered: 2004-12-23
Posts: 603

Re: libjpeg c / c++

Someitimes small things can cause huge troubles wink. 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

#14 2005-01-26 16:35:03

Blaster
Member
Registered: 2004-09-17
Posts: 49

Re: libjpeg c / c++

sweet it works once again tnx alot  big_smile

Offline

#15 2005-01-26 22:31:33

STiAT
Member
From: Vienna, Austria
Registered: 2004-12-23
Posts: 603

Re: libjpeg c / c++

Was me a pleasure wink

// STi


Ability is nothing without opportunity.

Offline

Board footer

Powered by FluxBB