Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

c++ calling c functions

 
 
teju
Guest
Posts: n/a
 
      12-05-2007
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
 
Reply With Quote
 
 
 
 
Sheth Raxit
Guest
Posts: n/a
 
      12-05-2007
On Dec 5, 9:20 am, 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?

this is nice article about what you want <mixing of c and c++ code>
http://developers.sun.com/solaris/articles/mixing.html
hope it will helpful.
>
> please reply....
>
> - Thejaswini


-Raxit Sheth
 
Reply With Quote
 
 
 
 
CBFalconer
Guest
Posts: n/a
 
      12-05-2007
teju wrote:
>
> 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?


Follow the following organization. The result works for both C and
C++.

----------- note from here on --------------
/* a C header file for linking to C++ */

/* the normal provisions for skipping on a second request */

#ifdef __cplusplus
extern "C"
{
#endif

/* Here is the complete .h file, as used in C */
/* barring the above skip on second request */

#ifdef __cplusplus
}
#endif
--------- end of repaired c header -------

By the way, all this is perfectly standard ISO C.

--
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>
Try the download section.


--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
Bill Reid
Guest
Posts: n/a
 
      12-05-2007

CBFalconer <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> teju wrote:
> >
> > 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?

>
> Follow the following organization. The result works for both C and
> C++.


We're getting closer here...

> ----------- note from here on --------------
> /* a C header file for linking to C++ */


....or C++ header file for linking to C...

> /* the normal provisions for skipping on a second request */
>

....and here are any normal external data declarations...then for
the external function declarations:

> #ifdef __cplusplus
> extern "C"
> {
> #endif
>
> /* Here is the complete .h file, as used in C */


No, just the external function declarations, in the usual form:

extern void my_callable_function(void);
....

If you are calling a C function from C++, that is all you need to do.
However, check your external C++ function declarations carefully
for return values and arguments of types not derived from or of the
fundamental types allowed by the C programming language.

If so, you can't call that function from C. You'll have to write a
C++ "wrapper" function to translate the arguments and/or return
value into allowed C types, or in some other way modify your
include file typedefs or C++ source to account for this discrepancy.

> /* barring the above skip on second request */


???

> #ifdef __cplusplus
> }
> #endif
> --------- end of repaired c header -------
>
> By the way, all this is perfectly standard ISO C.


Well, yeah, in general...

---
William Ernest Reid



 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      12-05-2007
teju 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.


You want the newsgroup comp.lang.c++. First, find their FAQ, which I
believe has a section on this topic.




Brian
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      12-05-2007
Bill Reid wrote:
> CBFalconer <(E-Mail Removed)> wrote:
>> teju wrote:
>>>
>>> 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?

>>
>> Follow the following organization. The result works for both C and
>> C++.

>
> We're getting closer here...
>
> > ----------- note from here on --------------
> > /* a C header file for linking to C++ */

>
> ...or C++ header file for linking to C...


No, you can't link C++ to C, in general.

--
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>
Try the download section.


--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      12-05-2007
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/>.

--
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
 
jameskuyper@verizon.net
Guest
Posts: n/a
 
      12-06-2007
CBFalconer wrote:
....
> No, you can't link C++ to C, in general.


I'm curious in what sense you mean that. C++ provides the concept of C
language linkage, and I believe that most C++ implementations support
that feature. Without getting into the details of how it works, it's
supposed to allow precisely what you've said can't be done. Are you
saying that most C++ implementations don't support this feature? Are
you saying that this feature doesn't work, or that it does work in
some special cases, but that in general it doesn't? Could you expand
upon that?
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      12-06-2007
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).

--
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>
Try the download section.



--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      12-06-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
>
> CBFalconer wrote:
> ...
> > No, you can't link C++ to C, in general.

>
> I'm curious in what sense you mean that. C++ provides the concept of C
> language linkage, and I believe that most C++ implementations support
> that feature. Without getting into the details of how it works, it's
> supposed to allow precisely what you've said can't be done. Are you
> saying that most C++ implementations don't support this feature? Are
> you saying that this feature doesn't work, or that it does work in
> some special cases, but that in general it doesn't? Could you expand
> upon that?


See my reply to Keith.

--
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>
Try the download section.



--
Posted via a free Usenet account from http://www.teranews.com

 
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