For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > October 2004 > mixing C and C++ libs









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author mixing C and C++ libs
Uwe Mayer

2004-10-26, 3:56 am

Hi,

I have problems linking a c++ program to a C static archive (.a). I don't
know wether this is at all possible nor am I sure that this is the source
of my problems, but the g++ outputs the following:

g++ sf2raw.c \
-I../Smartspace/Development/Infrastructure/sources/sffile \
-I/usr/include/qt3 \
-lqt-mt -L$(QTDIR) \
-L ../Smartspace/Development/Infrastructure/sources/sffile \
../Smartspace/Development/Infrastructure/sources/sffile/libsffile.a \
-o sf2raw

/tmp/cckWYOAh.o(.text+0x297): In function `main':
: undefined reference to `sf_file_attach(int, sf_file_error*)'
/tmp/cckWYOAh.o(.text+0x2bd): In function `main':
: undefined reference to `sf_file_get_type(sf_file*, sf_file_error*)'

[...] //some more "undefined reference" errors

/tmp/cckWYOAh.o(.text+0x7d2): In function `main':
: undefined reference to `sf_file_detach(sf_file*, sf_file_error*)'
/tmp/cckWYOAh.o(.gnu.linkonce.t._Z19handle_sf_fileErrorPKc+0x1e): In
function `handle_sf_fileError(char const*)':
: undefined reference to `sf_file_perror(sf_file_error const*, char const*)'
collect2: ld returned 1 exit status


The sf_file_attach, etc. are defined in sffile.h, which reside in the above
include folder and there is also its object file sffile.o and the
libsffile.a.

sf2raw.c was originally a C program, so is all the Smartspace stuff, but I
changed the source of sf2raw.c to compile with g++ in order to be able to
link it against the qt libraries.

Anyone an idea what's wrong with the above?

Thanks in advance
Uwe
DINH Viet Hoa

2004-10-26, 3:56 am

Uwe Mayer wrote :

> The sf_file_attach, etc. are defined in sffile.h, which reside in the above
> include folder and there is also its object file sffile.o and the
> libsffile.a.


is there some :

#ifdef _cplusplus
extern "C" {
#endif

/* content_of_h_file */

#ifdef _cplusplus
}
#endif

in you sffile.h ?

--
DINH V. Hoa,

"j'arrête l'al" -- Arsunique

Uwe Mayer

2004-10-26, 3:56 am

Tuesday 26 October 2004 09:30 am DINH Viet Hoa wrote:

> Uwe Mayer wrote :
>
[color=darkred]
> is there some :
>
> #ifdef _cplusplus
> extern "C" {
> #endif
>
> /* content_of_h_file */
>
> #ifdef _cplusplus
> }
> #endif
>
> in you sffile.h ?


no. :(

Is that all to be added?

Thanks,
Uwe
Nils O. Selćsdal

2004-10-26, 9:00 am

Uwe Mayer wrote:
> Tuesday 26 October 2004 09:30 am DINH Viet Hoa wrote:
>
>
>
>
>
>
>
> no. :(
>
> Is that all to be added?

Yes, the C++ compiler have to be told that the code is C,
else it will apply C++ check/namemangling/etc to it.


--
Nils O. Selćsdal
www.utelsystems.com
Uwe Mayer

2004-10-26, 9:00 am

Tuesday 26 October 2004 09:47 am "Nils O. SelÄsdal" wrote:

> Yes, the C++ compiler have to be told that the code is C,
> else it will apply C++ check/namemangling/etc to it.


Graphic! It worked. I had to compile the sffile with g++, too, of course.
But: I guess it won't work with ordinary C programs any more, right?

I'd go symlink the .c file to a .cc file and modify the Makefile to produce
two versions of the archife.
Is there any naming convention for libs /archives which have to be present
as both C and C++ binaries?

Thanks,
Uwe

Nils O. SelÄsdal

2004-10-26, 9:00 am

Uwe Mayer wrote:
> Tuesday 26 October 2004 09:47 am "Nils O. SelÄsdal" wrote:
>
>
>
>
> Graphic! It worked. I had to compile the sffile with g++, too, of course.
> But: I guess it won't work with ordinary C programs any more, right?

hmm, you shouldn't need that though. You did apply the
extern "C" .. to all C header files ?


--
Nils O. SelÄsdal
www.utelsystems.com
Uwe Mayer

2004-10-26, 3:58 pm

Tuesday 26 October 2004 13:24 pm "Nils O. SelÄsdal" wrote:

[color=darkred]
> hmm, you shouldn't need that though. You did apply the
> extern "C" .. to all C header files ?


The archive really only exists from one header file. I modified this one. I
just wondered wether other C programs that are compiled with gcc can link
to this archive which was created with g++.

Ciao
Uwe
Paul Pluzhnikov

2004-10-26, 3:58 pm

Uwe Mayer <merkosh@hadiko.de> writes:

> I just wondered wether other C programs that are compiled with gcc can link
> to this archive which was created with g++.


Rather than "just wonder", you should read-up on C++ name mangling.

Once you *understand* how it works and why 'extern "C"' was
necessary, you can answer your own question (and make the first
step to becoming enlightened :)

Yes, archive libraries created from objects compiled with g++
can be linked into "plain C" programs, prvided none of the C++
features (exceptions, constructors, operator new, etc.) were used
in the source.

Cheers,
--
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com