This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: gcc -x c does link with -ljpeg, gcc -x c++ does not
- From: Matthieu Moy <Matthieu dot Moy at st dot com>
- To: Stefan Hetzl <shetzl at chello dot at>
- Cc: <gcc-help at gcc dot gnu dot org>
- Date: Fri, 28 Mar 2003 10:57:39 +0100
- Subject: Re: gcc -x c does link with -ljpeg, gcc -x c++ does not
- References: <200303280918.28662.shetzl@chello.at>
Stefan Hetzl <shetzl at chello dot 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"
directive.
Something like
extern "C" {
#include "jpeg.h"
}
May do.
--
Matthieu MOY
Ph. D Student at STMicroelectronics and Verimag -- SysAr Team