mixing C and C++ libs
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > mixing C and C++ libs




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    mixing C and C++ libs  
Uwe Mayer


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
10-26-04 07:47 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





[ Post a follow-up to this message ]



    Re: mixing C and C++ libs  
DINH Viet Hoa


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
10-26-04 07:47 AM

Uwe Mayer wrote :

> The sf_file_attach, etc. are defined in sffile.h, which reside in the abov
e
> 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






[ Post a follow-up to this message ]



    Re: mixing C and C++ libs  
Uwe Mayer


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
10-26-04 07:47 AM

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

> Uwe Mayer wrote :
> 
[vbcol=seagreen]
> 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





[ Post a follow-up to this message ]



    Re: mixing C and C++ libs  
Nils O. Selćsdal


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
10-26-04 07:47 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





[ Post a follow-up to this message ]



    Re: mixing C and C++ libs  
Uwe Mayer


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
10-26-04 12:47 PM

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






[ Post a follow-up to this message ]



    Re: mixing C and C++ libs  
Nils O. SelÄsdal


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
10-26-04 12:47 PM

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





[ Post a follow-up to this message ]



    Re: mixing C and C++ libs  
Uwe Mayer


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
10-26-04 12:47 PM

Tuesday 26 October 2004 13:24 pm "Nils O. SelÄsdal" wrote:
 
[vbcol=seagreen]
> 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





[ Post a follow-up to this message ]



    Re: mixing C and C++ libs  
Paul Pluzhnikov


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
10-26-04 10:51 PM

Uwe Mayer <merkosh@hadiko.de> writes:

> I just wondered wether other C programs that are compiled with gcc can lin
k
> 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.





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 12:55 PM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register