gcc -x c does link with -ljpeg, gcc -x c++ does not

Matthieu Moy Matthieu.Moy@st.com
Fri Mar 28 10:33:00 GMT 2003

Stefan Hetzl <shetzl@chello.at> writes:

> Hi,
> I want to compile and link a small program (c code) that uses libjpeg. When I 
> invoke "gcc -x c -v readcoeff.c -ljpeg" everything works fine, but invoking 
> "gcc -x c++ -v readcoeff.c -ljpeg" gives "undefined reference..."-error 
> messages for every function from libjpeg (see the attached outputs).
> Any Ideas what could be causing this ?

I suppose this is a mangling problem :

In C,  a function is uniquely defined  by its name, so,  the symbol in
the object code can be just the name of the function. 

In  C++,  you can  use  function overloading,  so,  you  need to  have
different  symbols for  f(int) and  f(int, int)  for example.  This is
called mangling. 

The solution is to tell your program that the functions in libjpeg are
C functions compiled with a C compiler by using the

extern "C"


Something like 

extern "C" {
#include "jpeg.h"

May do. 

Matthieu MOY
Ph. D Student at STMicroelectronics and Verimag -- SysAr Team

More information about the Gcc-help mailing list