Solved

calling C function from VC++/C++

Posted on 2000-02-23
10
238 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
Comment Utility
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
Comment Utility
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
Comment Utility
snifong changed the proposed answer to a comment
0
 
LVL 3

Expert Comment

by:Shay050799
Comment Utility
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
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Expert Comment

by:tebsk
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
I agree "extern" linkage specifier  Keeps track of type safety also.


Regards,

Baskar
0
 

Author Comment

by:tridev
Comment Utility
thanks.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 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.

762 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

8 Experts available now in Live!

Get 1:1 Help Now