f



mixing C and C++ libs

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 
0
Uwe
10/26/2004 7:21:32 AM
comp.unix.programmer 10848 articles. 0 followers. kokososo56 (350) is leader. Post Follow

7 Replies
441 Views

Similar Articles

[PageSpeed] 38

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'alcool" -- Arsunique

0
DINH
10/26/2004 7:30:45 AM
Tuesday 26 October 2004 09:30 am DINH Viet Hoa wrote:

> 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 ?

no. :(

Is that all to be added? 

Thanks,
Uwe 
0
Uwe
10/26/2004 7:38:09 AM
Uwe Mayer wrote:
> Tuesday 26 October 2004 09:30 am DINH Viet Hoa wrote:
> 
> 
>>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 ?
> 
> 
> 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
0
ISO
10/26/2004 7:47:56 AM
Tuesday 26 October 2004 09:47 am "Nils O. Selåsdal" wrote:

>> 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.

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
 
0
Uwe
10/26/2004 8:52:19 AM
Uwe Mayer wrote:
> Tuesday 26 October 2004 09:47 am "Nils O. Selåsdal" wrote:
> 
> 
>>>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.
> 
> 
> 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
0
UTF
10/26/2004 11:24:21 AM
Tuesday 26 October 2004 13:24 pm "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 ?

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 
0
Uwe
10/26/2004 1:29:08 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.
0
Paul
10/26/2004 2:15:41 PM
Reply: