473,406 Members | 2,954 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Linking a C++ library to a C program.

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

Nov 15 '05 #1
8 7109
On 8 Jul 2005 15:57:50 -0700, "a2x" <ad***********@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/~a...FAQ-acllc.html
Nov 15 '05 #2
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/

Nov 15 '05 #3
"a2x" <ad***********@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.
Nov 15 '05 #4
Malcolm wrote:
"a2x" <ad***********@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
Nov 15 '05 #5


CBFalconer wrote:
Malcolm wrote:
"a2x" <ad***********@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

Nov 15 '05 #6
Default User wrote:
CBFalconer wrote:
Malcolm wrote:
"a2x" <ad***********@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
Nov 15 '05 #7
CBFalconer <cb********@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 si***@FamOuS.BedBuG.pAlS.INVALID | sed s/[[:upper:]]//g`
Nov 15 '05 #8
"S.Tobias" wrote:
CBFalconer <cb********@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
Nov 15 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Rob Ratcliff | last post by:
I'm compiling the latest version of a CORBA ORB called MICO on a Cray X1. It makes heavy use of templates and namespaces. Up until the link step, the C++ source code compiled flawlessly. But, when...
7
by: Steven T. Hatton | last post by:
Is there anything that gives a good description of how source code is converted into a translation unit, then object code, and then linked. I'm particularly interested in understanding why putting...
20
by: Steven T. Hatton | last post by:
I just read this in the description of how C++ is supposed to be implemented: "All external object and function references are resolved. Library components are linked to satisfy external...
7
by: wmkew | last post by:
Hello everyone I'm encountering a R6002 Runtime error and several bugs when trying to generate a simple Managed C++ application with .NET 2003. The main problem seems to arise from linking with...
1
by: Kay | last post by:
I already specified to ignore specific library: MSVCPRT.lib MSVCRT.lib LIBC.lib MSVCRTD.lib LIBCD.lib command line is like: /INCREMENTAL /NOLOGO /DLL /NODEFAULTLIB:"MSVCPRT.lib MSVCRT.lib LIBC.lib...
10
by: Julian | last post by:
I get the following error when i try to link a fortran library to a c++ code in .NET 2005. LINK : fatal error LNK1104: cannot open file 'libc.lib' the code was working fine when built using...
2
by: =?ISO-8859-15?Q?Luigi_Malag=F2?= | last post by:
Hello, i'm having a problem with "multiple definition error" linking a library. I have a program with a main. I have another program with classes and a main too. I created a library of the second...
2
by: Mohsen A. Momeni | last post by:
Hi, Does it have any difference in security, whether to compile a function as a static lib and link it with a program or just add the function to the source? In other words, suppose we have two...
6
by: Joe.pHsiao | last post by:
Hi, I tried to link a C program to a library which is written by me in C+ +. I read some posts about linking a C program to C++ libraries. It seems doable by adding extern "C" to the C++ head...
5
by: Visa Inquirer | last post by:
I need my program to always link to a particular shared library, not the one in LD_LIBRARY_PATH. Is there a way to specify shared library by full path when linking so that when running...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.