Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

c++ calling c functions

 
 
Ian Collins
Guest
Posts: n/a
 
      12-06-2007
CBFalconer wrote:

>
> Maybe we are confusing the direction of linking? You can call C
> from C++, but not C++ from C (without impossible diddling).
>

No, we have been here at least once before.

A C++ function declared as extern "C" may be called from C, that's one
of the reasons C++ has the linkage specifier. It is also used to
identify C function prototypes.

Otherwise C++ would not be able to call C library functions which have a
function pointer as a parameter.

--
Ian Collins.
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      12-06-2007
CBFalconer <(E-Mail Removed)> writes:
> Keith Thompson wrote:
>> CBFalconer <(E-Mail Removed)> writes:
>> [...]
>>
>>> No, you can't link C++ to C, in general.

>>
>> I don't believe that's correct. Further information would be
>> topical in comp.lang.c++, not in comp.lang.c (since it's C++, not
>> C, that defines the mechanisms), but see questions 32.5 and 32.6
>> in the "C++ FAQ Lite" at <http://www.parashift.com/c++-faq-lite/>.

>
> The point is that C++ function names are modified, to express the
> parameter types, in the linkable object code sections. C does not
> do this, since it doesn't have shared function names etc. Thus the
> C++ code can be told (with the "extern C {...}") that selected C
> functions are to be accessed (from C++) with unmodified names. The
> reverse is not possible.
>
> Thus I gave the general method for making C object code available
> to aa C++ program.
>
> Maybe we are confusing the direction of linking? You can call C
> from C++, but not C++ from C (without impossible diddling).


No, I'm asserting that it's possible to call C from C++ *and* to call
C++ from C.

The C++ FAQ agrees, and directly contradicts your claim. Its authors
certainly know C++ better than I do.

Furthermore, I've just done it. Here are my source files:

==> func.h <==
#ifdef __cplusplus
extern "C" {
#endif
void func(int i, char c, float x);
#ifdef __cplusplus
}
#endif

==> func.C <==
#include "func.h"
#include <iostream>
void func(int i, char c, float x)
{
std::cout << "In func, sizeof 'a' = " << sizeof 'a' << "\n";
std::cout << "i = " << i << ", c = '" << c << "', x = " << x << "\n";
}

==> c_main.c <==
#include "func.h"
#include <stdio.h>
int main(void)
{
printf("In C main, sizeof 'a' = %d\n", (int)sizeof 'a');
func(42, '$', 123.456);
return 0;
}

==> cpp_main.C <==
#include "func.h"
#include <iostream>
int main()
{
std::cout << "In C++ main, sizeof 'a' = " << sizeof 'a' << "\n";
func(42, '$', 123.456);
return 0;
}

Here's the output of the C main program:

In C main, sizeof 'a' = 4
In func, sizeof 'a' = 1
i = 42, c = '$', x = 123.456

Here's the output of the C++ main program:

In C++ main, sizeof 'a' = 1
In func, sizeof 'a' = 1
i = 42, c = '$', x = 123.456

<OT>Both main programs had to be linked with "g++", because both need
the C++ standard library.</OT>

But as you can see the C program was compiled as C and successfully
called a C++ function.

Obviously this requires some compatibility between the C and C++
implementations.

--
Keith Thompson (The_Other_Keith) <(E-Mail Removed)>
Looking for software development work in the San Diego area.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
 
Mark McIntyre
Guest
Posts: n/a
 
      12-06-2007
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 the C program was compiled as C and 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. The C
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-c was 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 the C and C++
> implementations.


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

 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      12-06-2007
Mark McIntyre said:

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


No, not really. That was just a quick way to tell the linker how to do The
Right Thing.

>> But as you can see the C program was compiled as C and 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.


But wasn't it extern "C" qualified? The whole point of that - or at least
one of the points - is to switch off name-mangling.

> The C
> 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-c was 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 the C and C++
>> implementations.

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


To use a C library from a C program requires some compatibility between the
C implementation used for the library and the one used for the program. So
are you saying that, in general, it isn't possible to call C libraries
from C?

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      12-06-2007
Mark McIntyre 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...
>

Nope.

>> But as you can see the C program was compiled as C and 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. The C
> compiler would have generated a call to _func or whatever. The linker
> can't resolve this.


No it would not. The function was declared extern "C", so its name and
calling convections will be as they are in C.

> The only way the modules will link is if main.lowercase-c was compiled
> as C++ /OR/ if some implementation-specific magic went on to tell the
> linker to look for multiple possible versions of the name.
>

Nonsense. If that were the case, no C++ application could call a C
library function.

>> Obviously this requires some compatibility between the C and C++
>> implementations.

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

Nonsense, the same applies to any two C compilers on the same platform.

--
Ian Collins.
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      12-06-2007
CBFalconer wrote:
> Keith Thompson wrote:
>> CBFalconer <(E-Mail Removed)> writes:
>> [...]
>>
>>> No, you can't link C++ to C, in general.

>> I don't believe that's correct. Further information would be
>> topical in comp.lang.c++, not in comp.lang.c (since it's C++, not
>> C, that defines the mechanisms), but see questions 32.5 and 32.6
>> in the "C++ FAQ Lite" at <http://www.parashift.com/c++-faq-lite/>.

>
> The point is that C++ function names are modified, to express the
> parameter types, in the linkable object code sections. C does not
> do this, since it doesn't have shared function names etc. Thus the
> C++ code can be told (with the "extern C {...}") that selected C
> functions are to be accessed (from C++) with unmodified names. The
> reverse is not possible.
>
> Thus I gave the general method for making C object code available
> to aa C++ program.
>
> Maybe we are confusing the direction of linking? You can call C
> from C++, but not C++ from C (without impossible diddling).


You can compile C++ routines after declaring that they have C linkage.
If you do so, they will be callable from C. In order for this to work,
the arguments and return type of the C++ function must use only features
in the common subset of C and C++. However, the body of the function can
use all features of C++ (though throwing an exception is problematic).

Therefore, you can't call all C++ functions this way, but you can call
any C++ functions that are designed to be called from C. And those
functions CAN call other C++ functions, without any of those restrictions.
 
Reply With Quote
 
James Fang
Guest
Posts: n/a
 
      12-06-2007
On Dec 5, 12:20 pm, teju <(E-Mail Removed)> wrote:
> hi,
>
> i am trying 2 merge 2 projects into one project.One project is using c
> language and the other one is using c++ code.
>
> both are working very fine independently.But now i need to merge both
> and my c++ code should call c code.but when i tried to call a function
> in c code externing that function in my c++ code, i am getting
> unresolved external symbol error. Whatever i try its giving more and
> more errrors...so is it possible to merge 2 projects?
>
> if so how can i do that?
>
> please reply....
>
> - Thejaswini


Try
extern "C"
{
extern C_API();
}

in your cpp file , and then call the C_API().
 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      12-06-2007
Richard Heathfield wrote:
> Mark McIntyre said:
>
>> 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...

>
> No, not really. That was just a quick way to tell the linker how to do The
> Right Thing.


Riight - implementation-specific magic, in other words!

> But wasn't it extern "C" qualified?


I don't recall that, but ICBW. If so, the function in question wasn't
actually copmiled as C++ so I'm not sure it was a valid test...

> To use a C library from a C program requires some compatibility between the
> C implementation used for the library and the one used for the program. So
> are you saying that, in general, it isn't possible to call C libraries
> from C?



Correct. Try calliing a Turbo C 2 library from Watcom 32-bit C, or an
MSC 5 library from Vax C 5....
gd&r
 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      12-06-2007
Ian Collins wrote:

> Nonsense. If that were the case, no C++ application could call a C
> library function.


Firstly that's true, if the function isn't declared as extern "C".
Secondly we were dicsussing C calling C++, not the other way round.
Lastly compiling the C++ routine as C doesn't count.


>>> Obviously this requires some compatibility between the C and C++
>>> implementations.

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

> Nonsense, the same applies to any two C compilers on the same platform.


Indeed, and more so between C and C++. So *in general* it isn't
possible, without invoking implementation-specific features.

You like the word nonsense, don't you? Myself I like the word
Balderdash, its much more evocative.


 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      12-06-2007
Mark McIntyre wrote, On 06/12/07 18:53:
> Ian Collins wrote:
>
>> Nonsense. If that were the case, no C++ application could call a C
>> library function.

>
> Firstly that's true, if the function isn't declared as extern "C".
> Secondly we were dicsussing C calling C++, not the other way round.
> Lastly compiling the C++ routine as C doesn't count.


<snip>

Isn't it time for everyone to take this next door to comp.lang.c++ where
they know all about the compatibility between C and C++? Although they
will probably just point you at the FAQ and/or standard which tell you
how to make your C++ callable from C without compiling your C++ as C or
your C as C++.
--
Flash Gordon
 
Reply With Quote
 
 
 
Reply

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



Advertisments