Solved

calling C function from VC++/C++

Posted on 2000-02-23
10
241 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
maze travler 6 55
Fast, free, private online code repository / editor 7 97
Excel/Word Add-in in what language? 5 118
How to split this in C++ 4 101
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

815 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now