-
December 17th, 2005, 12:44 AM
#1
calling c++ function from c
Hi,
how to call a c++ function from a c program.my code
abc.h file
class one
{
private :
public:
}
void function(char *str);
the defination of function(char *str) is in abc.cpp file.
now i need to call this function from a .c file.
please help regarding this.
Last edited by harshandu; December 17th, 2005 at 01:00 AM.
-
December 17th, 2005, 02:07 AM
#2
Re: calling c++ function from c
Note First Thing you Should Know the difference Between c and C++
in C there is no class.so you can't call a c++ code to c but yes you can call you C code to C++.but as according to your code you are not doing anything which is related with C++.
So make a simple structure and use it in your c code.and you can make a call to your .h file header file .will suggest you to go for preprocessor put your function in the your newly created h file and then call this to you in your code.
-
December 17th, 2005, 05:59 AM
#3
Re: calling c++ function from c
Read up on extern "C" - Using extern to Specify Linkage. Apart from the fact that Humpty told (no class constructs in C...), there is one more major difference. The name mangling/decoration in C++ makes it difficult to make calls to C++ routines from C. Note - you can ony use POD types to operate between C and C++ code. You can get to know what POD types are in this thread - Destruction??? Hope this helps. Regards.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
December 17th, 2005, 02:57 PM
#4
Re: calling c++ function from c
Put the function in a DLL and call it from your C program.
-
December 17th, 2005, 09:37 PM
#5
Re: calling c++ function from c
It doesn't have to be in a DLL either, you can have a .c file and a .cpp file in the same project.
extern "C" is used to avoid name-mangling but note it must be declared from within C++ not from within C.
Therefore you use the __cplusplus pre-processor to determine which language you are in.
Code:
#ifdef __cplusplus
extern "C" {
#endif
then later on you must close the brace with
#ifdef __cplusplus
}
#endif
It would be better in my opinion if there was a standard pre-processor that worked for both, eg
EXTERN_C_BEGIN
// put stuff in here
EXTERN_C_END
If you did that you could have a header
Code:
//extern_c.h
#ifndef EXTERN_C_H
#define EXTERN_C_H
#ifdef __cplusplus
#define EXTERN_C_BEGIN extern "C" {
#define EXTERN_C_END }
#else
#define EXTERN_C_BEGIN
#define EXTERN_C_END
#endif
#endif /* define EXTERN_C_H */
(This is one of the places where a #define is useful).
The fact that a function is declared extern "C" doesn't mean it can be called from C. It must also only types that are valid in C. However contrary to belief, you may includes pointers to classes if they are only forwardly declared as structs, although you should also include the word "struct" in the function prototype. Thus:
Code:
int someFunction( struct someType * );
is a valid C prototype and someType in reality could be a C++ class.
-
December 18th, 2005, 05:14 AM
#6
Re: calling c++ function from c
Originally Posted by NMTop40
....is a valid C prototype and someType in reality could be a C++ class.
with certain limitations as quoted below from C++ FAQ Lite:
A POD type is a C++ type that has an equivalent in C, and that uses the same rules as C uses for initialization, copying, layout, and addressing.
As an example, the C declaration struct Fred x; does not initialize the members of the Fred variable x. To make this same behavior happen in C++, Fred would need to not have any constructors. Similarly to make the C++ version of copying the same as the C version, the C++ Fred must not have overloaded the assignment operator. To make sure the other rules match, the C++ version must not have virtual functions, base classes, non-static members that are private or protected, or a destructor. It can, however, have static data members, static member functions, and non-static non-virtual member functions.
The actual definition of a POD type is recursive and gets a little gnarly. Here's a slightly simplified definition of POD: a POD type's non-static data members must be public and can be of any of these types: bool, any numeric type including the various char variants, any enumeration type, any data-pointer type (that is, any type convertible to void*), any pointer-to-function type, or any POD type, including arrays of any of these. Note: data-pointers and pointers-to-function are okay, but pointers-to-member are not. Also note that references are not allowed. In addition, a POD type can't have constructors, virtual functions, base classes, or an overloaded assignment operator.
Regards.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
December 18th, 2005, 06:01 AM
#7
Re: calling c++ function from c
It's true that you could not give a C function an instance to a C++ class but you can give it an "incomplete" pointer. Then the C function could call C-style methods that take a pointer to the struct.
Example:
Code:
// foo.hpp - full definition of foo
struct foo
{
int publicMethod1() const;
void publicMethod2( int ); // non-const
// other stuff including private members etcf.
};
// foo.h:
struct foo;
EXTERN_C_BEGIN
struct foo * createAFoo();
void destroyAFoo( const struct foo * pFoo );
int fooMethod1( const struct foo * pFoo );
void fooMethod2( struct foo * pFoo, int x );
EXTERN_C_END
// foo.cpp
#include "foo.hpp"
#include "foo.h"
// implement C++ member functions
struct foo* getAFoo()
{
return new foo;
}
void destroyAFoo( const struct foo * pFoo )
{
delete pFoo;
}
int fooMethod1( const struct foo * pFoo )
{
return pFoo->publicMethod1();
}
void fooMethod2( struct foo * pFoo, int x )
{
pFoo->publicMethod2( x );
}
// some C code
#include "foo.h"
int func()
{
struct foo * pFoo = getAFoo();
int y = fooMethod1( pFoo );
fooMethod2( pFoo, y );
destroyAFoo( pFoo );
}
and there you have it, C code using a C++ class.
Last edited by NMTop40; December 18th, 2005 at 07:28 AM.
-
December 19th, 2005, 05:07 AM
#8
Re: calling c++ function from c
Hi all,
Thanks all.My problem got solved by using extern "c" and __cplusplus.The solution given by NMTOP40 worked.Thanks a lot.
Regards,
Harsha.
Last edited by harshandu; December 19th, 2005 at 05:11 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|