Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

calling C function from VC++/C++

Posted on 2000-02-23
10
Medium Priority
?
248 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
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.

721 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