Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

calling C function from VC++/C++

Posted on 2000-02-23
10
Medium Priority
?
249 Views
Last Modified: 2010-04-10
How I can call a C function from C++/ VC++ ?
Learning  C++/VC++.
Any info pls ?
thanks in advance.
0
Comment
Question by:tridev
  • 3
  • 2
  • 2
  • +3
10 Comments
 
LVL 9

Expert Comment

by:jasonclarke
ID: 2551089
depends a bit on what you mean.  Do you mean it is compiled with C, and in a library somewhere,  if so, declare it in an extern "C" block,

i.e.
extern "C" {
   int MyCFunc()
};

void main()
{
   MyCFunc();
}

the standard convention in header files for C stuff that might be used in C++ code is to put something like the following around them:

i.e. in cheader.h

#ifdef __cplusplus
extern "C" {
#endif

/* All the normal C stuff */

#ifdef __cplusplus
}
#endif

then the header can be used in both C and C++ code.
0
 
LVL 1

Accepted Solution

by:
snifong earned 150 total points
ID: 2551102
You have to declare it like:
extern "C" foo ();
This stops the compiler from complianing about undefined symbols.
0
 
LVL 1

Expert Comment

by:snifong
ID: 2551109
snifong changed the proposed answer to a comment
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:Shay050799
ID: 2552486
can't agree more with jasonclarke
but i want to say that using EXTERN C...

by C++ code only if the function was linked with a C compiler.

u will get link error if u won't do that, cause the C++ linker doesn't recognize this function u have to tell him to link this function as C function.

Shay
0
 

Expert Comment

by:tebsk
ID: 2553514
C function can be called in C++ by using "extern" linkage specifier.

Syntax is : extern "C" { functions prototype};


Normally for linking C and C++ handles different kind of mechanism. Compilers give a symbolic name for each and every function used in the program and these symbolic names are used by the linkers.

in C, the function int sum(int) may be given the symbolic name as _isum.

But, in C++ it is different because in C++ compilation time polymorphism is allowed.  

Therefore if a C function is called from a C++ program then the linker will search only the C++ symbolic name for that particular function in its symbol table unless it is not specified by "extern" linkage specifier. If not available then it will give the run time error.

If the "extern" specifier is used then the linker is instructed to use C symbolic name rather than C++.
0
 

Expert Comment

by:tebsk
ID: 2553515
Hi,

C function can be called in C++ by using "extern" linkage specifier.

Syntax is : extern "C" { functions prototype};


Normally for linking C and C++ handles different kind of mechanism. Compilers give a symbolic name for each and every function used in the program and these symbolic names are used by the linkers.

in C, the function int sum(int) may be given the symbolic name as _isum.

But, in C++ it is different because in C++ compilation time polymorphism is allowed.  

Therefore if a C function is called from a C++ program then the linker will search only the C++ symbolic name for that particular function in its symbol table unless it is not specified by "extern" linkage specifier. If not available then it will give the run time error.

If the "extern" specifier is used then the linker is instructed to use C symbolic name rather than C++.

Regards,

Baskar
0
 
LVL 1

Expert Comment

by:lar_jens
ID: 2553518
Eh??? Aren't all of you just saying the same thing?? In different ways, perhaps, but it is still the same thing..

0
 
LVL 9

Expert Comment

by:jasonclarke
ID: 2553749
Also, what tebsk has to say is not quite correct, names are mangled in C++ not just to allow compile time polymorphism (by this I think function and operator overloading is meant), but also to enable type safety.

C does not check that the parameters of a method against the call.

so in C if you had:

a definition:
int f(float, char)
{ /* ... */ }

and a use like this:

extern int f();
....
f();
....

the compiler would not spot the error.  This has nothing to do with overloading, just type safety.

To continue his analogy further, the C++ mangled name for:
   void Foo(int)
in visual C++ is
   ?Foo@@YAXH@Z
which is some internal representation for the C signature.  It could easily be something like:
   int_Foo_int
in some other compiler.  This is also why you can't use libraries built with one C++ compiler with code built with a different compiler - the mangled names could be completely different.

In the Windows world at least, C compilers just add an _ before the method name, so void Foo(int) would be _Foo.  So, you can link code generated with one C compiler with code generated by another (with some restrictions).
0
 

Expert Comment

by:tebsk
ID: 2553861
I agree "extern" linkage specifier  Keeps track of type safety also.


Regards,

Baskar
0
 

Author Comment

by:tridev
ID: 2554510
thanks.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

886 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question