f



Linking a C++ library to a C program.

Hi,

I am writing a C program which interfaces with a C++ library. The C++
library works fine with C. I was wondering if I need to use a C++
compiler (g++ in my case) to compile the C program or can I use a C
compiler (gcc).

Thanks.

0
7/8/2005 10:57:50 PM
comp.lang.c 30657 articles. 5 followers. spinoza1111 (3246) is leader. Post Follow

8 Replies
745 Views

Similar Articles

[PageSpeed] 15

On 8 Jul 2005 15:57:50 -0700, "a2x" <add_aaron_2_x@hotmail.com> wrote
in comp.lang.c:

> Hi,
> 
> I am writing a C program which interfaces with a C++ library. The C++
> library works fine with C. I was wondering if I need to use a C++
> compiler (g++ in my case) to compile the C program or can I use a C
> compiler (gcc).
> 
> Thanks.

C does not define linkage to any other language at all.  C++, which is
off-topic in comp.lang.c, does not define the mechanics of linkage.
You need to ask on either one of the gnu.gcc.* support groups, or one
specific to you platform.

-- 
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
0
jackklein (3930)
7/9/2005 3:27:56 AM
a2x wrote:
> I am writing a C program which interfaces with a C++ library. The C++
> library works fine with C. I was wondering if I need to use a C++
> compiler (g++ in my case) to compile the C program or can I use a C
> compiler (gcc).

Ok, first, learn what extern "C" means, and learn how to use and apply
it to interface C and C++.  Then consider that you have two options: 1)
You can simply put extern "C" around every interface that you wish to
share with C and 2) You can compile the C code with your C++ compiler
and actually make everying actually C++.  The first is the cleanest way
but might require more intrusive source modifications.  The second is
easiest so long as you aren't doing weird things in C like making
declarations like: struct foo foo;

-- 
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/

0
websnarf (1153)
7/9/2005 6:12:22 AM
"a2x" <add_aaron_2_x@hotmail.com> wrote
>
> I am writing a C program which interfaces with a C++ library. The C++
> library works fine with C. I was wondering if I need to use a C++
> compiler (g++ in my case) to compile the C program or can I use a C
> compiler (gcc).
>
If you want to call a function written in C++ from a C file your best bet is 
to write the C code in the common subset of the two languages and run it 
through a C++ compiler. The alternative is to mess about with C++ name 
mangling and linkage conventions. 


0
regniztar (3128)
7/11/2005 8:23:58 PM
Malcolm wrote:
> "a2x" <add_aaron_2_x@hotmail.com> wrote
>>
>> I am writing a C program which interfaces with a C++ library. The
>> C++ library works fine with C. I was wondering if I need to use a
>> C++ compiler (g++ in my case) to compile the C program or can I
>> use a C compiler (gcc).
>
> If you want to call a function written in C++ from a C file your
> best bet is to write the C code in the common subset of the two
> languages and run it through a C++ compiler. The alternative is to
> mess about with C++ name mangling and linkage conventions.

For which alternative, AFAICT, there exists nothing even remotely
resembling a standard, so that everything is totally non-portable.

-- 
"If you want to post a followup via groups.google.com, don't use
 the broken "Reply" link at the bottom of the article.  Click on 
 "show options" at the top of the article, then click on the 
 "Reply" at the bottom of the article headers." - Keith Thompson


0
cbfalconer (19194)
7/11/2005 10:54:07 PM

CBFalconer wrote:
> Malcolm wrote:
> > "a2x" <add_aaron_2_x@hotmail.com> wrote
> >>
> >> I am writing a C program which interfaces with a C++ library. The
> >> C++ library works fine with C. I was wondering if I need to use a
> >> C++ compiler (g++ in my case) to compile the C program or can I
> >> use a C compiler (gcc).
> >
> > If you want to call a function written in C++ from a C file your
> > best bet is to write the C code in the common subset of the two
> > languages and run it through a C++ compiler. The alternative is to
> > mess about with C++ name mangling and linkage conventions.
>
> For which alternative, AFAICT, there exists nothing even remotely
> resembling a standard, so that everything is totally non-portable.


Correct, but there is a C++ standard way to specify C-style linkage
(extern "C"). That prevents you from calling overloaded functions from
the C module, of course.

Whether a C object file can actually be linked with a C++ object file
or library is still implementation dependent. In practice it works
pretty well.

All of this should be discussed on comp.lang.c++, not here.



Brian

0
defaultuserbr (3656)
7/11/2005 11:03:48 PM
Default User wrote:
> CBFalconer wrote:
>> Malcolm wrote:
>>> "a2x" <add_aaron_2_x@hotmail.com> wrote
>>>>
>>>> I am writing a C program which interfaces with a C++ library. The
>>>> C++ library works fine with C. I was wondering if I need to use a
>>>> C++ compiler (g++ in my case) to compile the C program or can I
>>>> use a C compiler (gcc).
>>>
>>> If you want to call a function written in C++ from a C file your
>>> best bet is to write the C code in the common subset of the two
>>> languages and run it through a C++ compiler. The alternative is to
>>> mess about with C++ name mangling and linkage conventions.
>>
>> For which alternative, AFAICT, there exists nothing even remotely
>> resembling a standard, so that everything is totally non-portable.
> 
> Correct, but there is a C++ standard way to specify C-style linkage
> (extern "C"). That prevents you from calling overloaded functions
> from the C module, of course.
> 
> Whether a C object file can actually be linked with a C++ object
> file or library is still implementation dependent. In practice it
> works pretty well.
> 
> All of this should be discussed on comp.lang.c++, not here.

Not quite.  I am quite willing for C++ users to call my code, so I
habitually insert the #ifdef __cplusplus__ guards for the 'extern
"C" {}' in the header files.  That __cplusplus__ is not allowed to
be defined in the C system, for just this purpose.

-- 
"If you want to post a followup via groups.google.com, don't use
 the broken "Reply" link at the bottom of the article.  Click on 
 "show options" at the top of the article, then click on the 
 "Reply" at the bottom of the article headers." - Keith Thompson


0
cbfalconer (19194)
7/12/2005 2:24:47 AM
CBFalconer <cbfalconer@yahoo.com> wrote:

> Not quite.  I am quite willing for C++ users to call my code, so I
> habitually insert the #ifdef __cplusplus__ guards for the 'extern
> "C" {}' in the header files.  That __cplusplus__ is not allowed to
> be defined in the C system, for just this purpose.
 
ITYM:  __cplusplus

-- 
Stan Tobias
mailx `echo siXtY@FamOuS.BedBuG.pAlS.INVALID | sed s/[[:upper:]]//g`
0
siXtY (303)
7/12/2005 3:44:41 PM
"S.Tobias" wrote:
> CBFalconer <cbfalconer@yahoo.com> wrote:
> 
>> Not quite.  I am quite willing for C++ users to call my code, so I
>> habitually insert the #ifdef __cplusplus__ guards for the 'extern
>> "C" {}' in the header files.  That __cplusplus__ is not allowed to
>> be defined in the C system, for just this purpose.
> 
> ITYM:  __cplusplus

Yup, I always have to check that when I write a header file.  At
least I know I am usually wrong.

-- 
"If you want to post a followup via groups.google.com, don't use
 the broken "Reply" link at the bottom of the article.  Click on 
 "show options" at the top of the article, then click on the 
 "Reply" at the bottom of the article headers." - Keith Thompson


0
cbfalconer (19194)
7/12/2005 5:05:49 PM
Reply: