Solved

calling C function from VC++/C++

Posted on 2000-02-23
10
242 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 50 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

828 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