473,385 Members | 1,602 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,385 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 7107
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.