Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > c++ calling c functions

Thread Tools

c++ calling c functions

Ian Collins
Posts: n/a
CBFalconer wrote:
> Chris Torek wrote:
>> CBFalconer <(E-Mail Removed)> wrote:
>>> Keith Thompson wrote:
>>>> What exactly do you mean by "normal"? Does the use of extern "C",
>>>> a feature defined by the C++ standard, render code abnormal?
>>> I think we are getting at the heart of the disagreement. To me, a
>>> normal C++ 'module' contains such things as functions with modified
>>> names, reflecting the parameters, and used to separate the various
>>> forms.

>> The thing is, you can tell *every* [%] C++ compiler *not* to use "mangled
>> names", simply by writing one special C++ declaration in front of
>> the function definition.
>> [% See parenthetical caveat below, also marked with [%].]
>> Imagine, if you will, that we have -- instead of C and C++ -- the
>> two languages P and L. In P, we write:

> .... snip ...
> I have put this aside for further study, along with Keiths separate
> example. I think we are defining interconnection differently, but
> am not sure now.
> I am talking about complete modules, taking advantages of all the
> abilities of their respective language, being used in object form
> in the other language.

So are we. It does work, honest! I produce a lot of mixed C and C++
applications and from a C++ perspective, there are only a couple of
restrictions imposed on functions declared extern "C".

Due to C not having function overloading, extern "C" functions can't be
overloaded by other extern "C" functions.

You can't declare a function template as extern "C".

Other than those, and C++ function with extern "C" linkage is exactly
that, a C++ free function with an alternative linkage specification.
There aren't any restrictions on what the function does, or calls.

Ian Collins.
Reply With Quote
Posts: n/a
On Dec 6, 2:15 pm, Mark McIntyre <(E-Mail Removed)> wrote:
> Keith Thompson wrote:
> > <OT>Both main programs had to be linked with "g++", because both need
> > the C++ standard library.</OT>

> This is where this 'goes wrong' I think...
> > But as you can see theCprogram was compiled asCand successfully
> > called a C++ function.

> I don't think so. The C++ compiler would have turned func into something
> like func::func@as:33:22 or some such other decorated nonsense. TheC
> compiler would have generated a call to _func or whatever. The linker
> can't resolve this.
> The only way the modules will link is if main.lowercase-cwas compiled
> as C++ /OR/ if some implementation-specific magic went on to tell the
> linker to look for multiple possible versions of the name.
> > Obviously this requires some compatibility between theCand C++
> > implementations.

> Indeed - so _in general_ this isn't possible, as CBF said in the first
> place?

with extern "c" much worked and after making some changes now both
projects merged and working fine but now i need to access c++ object
from my c code...

now how can i do this?
Reply With Quote
Ian Collins
Posts: n/a
teju wrote:
> with extern "c" much worked and after making some changes now both
> projects merged and working fine but now i need to access c++ object
> from my c code...
> now how can i do this?

Please, don't rake this thread up again, it has just died down. Read
this thread (and the countless others here and down the hall in c.l.c++).

Ian Collins.
Reply With Quote
Bill Reid
Posts: n/a

Another old one...this one really illustrates one of the REAL
problems here...a LOT of the most frequent posters couldn't
program a computer to save their lives, but they sure know
how to jabber endlessly debating about cut-and-dried technical
details of programming languages...

CBFalconer <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Keith Thompson wrote:
> > CBFalconer <(E-Mail Removed)> writes:
> >

> ... snip ...
> >
> > A C++ source file containing an extern "C" directive is 100% pure C++.
> >
> >> To call that system from a C module, something has to supply the
> >> modified function name. There is no such provision in C. It is
> >> hard to add controlled gubris to a function name. Therefore you
> >> can't call the C++ code from C.

> >
> > Then please explain how I'm able to do exactly that. You can do so
> > only by assigning nonsensical meanings to the phrases "C code" and
> > "C++ code".

> In other words you are wrapping the _entire_ C++ source module with
> the 'extern "C" {}' wrapper. That means you need to create a
> separate .h header file, with the same wrapper, for use only in the
> C linkage (This is hazy). It also means that there is no header
> file for C++ use (unless you create another one).
> The module that you compiled with the C++ compiler is not usable in
> a C++ program without the extra effort of the separate header
> file. To me this is not what is desired.
> I will retract the 'impossible' claim, and replace it with
> 'impracticable'.

You were informed by me several billion posts ago in plain English
in this thread exactly how to do it. This was about the third time I've
posted this fairly simple information. You've been informed by several
others that they do it routinely. I do it routinely. You've been shown
simple example code illustrating exactly how to do it. You've been
pointed to a very well-written explanation how to do it in the C++
FAQ. You've had the all the technical considerations involved
described to you in atomic detail.

But you're still arguing about it. Therefore, I can only come to
one conclusion: you're a useless moron.

As far as practicality is concerned, there really aren't that many
good C libraries to perform modern non-trivial computing tasks these
days. So I just go ahead and use C++ libraries, called from my
C code. I also much prefer C++ for certain types of work (particularly
some types of simulations), and I like to be able to mix and match
some other modules I've written in C with these C++ modules.

And of course, my language mixing doesn't just end with C++
and C, but then again, as I've always said, I like to get things
done...but that's not the "charter" here, is it? You've got your
own "special" talents that you so unashamedly share with everybody
here...and what a wonderful world, you've found a "home" with
others of similar intellect...

William Ernest Reid

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Shared functions vs Non-Shared Functions tshad ASP .Net 11 05-27-2005 05:53 PM
Variadic functions calling variadic functions with the argument list, HLL bit shifts on LE processors Ross A. Finlayson C Programming 19 03-10-2005 03:57 AM
Function pointers, variable argument functions calling other variable-argument functions (sort of) S?ren Gammelmark C Programming 1 01-07-2005 09:41 PM
please help me in distinguish redefining functions, overloading functions and overriding functions. Xiangliang Meng C++ 1 06-21-2004 03:11 AM
External inline functions calling internal inline functions Daniel Vallstrom C Programming 2 11-21-2003 01:57 PM