Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C calling C++ functions

Status
Not open for further replies.

gzkg1m

Programmer
May 23, 2001
3
US
Hello all-
This might get lengthy so hold on...

I'm simply trying to call a C++ function from C. Here's an update as to where I am and I what I've tried.

The following is my C++ .h file:
#ifndef MYSERVER_H
#define MYSERVER_H

#ifdef __cplusplus
class myServer
{
public:
myServer();
~myServer();

int addANI(struct ANIstruct*);
};
#else
typedef struct myServer myserver;
#endif

#ifdef __cplusplus
extern "C" {
#endif
#if defined(__STDC__) || defined(__cplusplus)
extern int addANI_cpp(void*, struct ANIstruct*);
#endif

#ifdef __cplusplus
}
#endif
#endif
/*****END OF C++ .h FILE*****/

The following is the C++ .cc file
#include "myServer.h"
#include Many Others ...

Constructor and Destructor do nothing for now...

int myServer::addANI(struct ANIstruct* aniStruct)
{
int result;
/* do something simple here */
return(result);
}

int addANI_cpp(myServer* myserver,
struct ANIstruct* aniStruct)
{
return(myserver->addANI(aniStruct));
}
/*****END OF C++ .cc FILE ********/

The following is the C .c file:
#include "myServer.h"
#include Others...

int *insert_ani_1_svc(ANIstruct *argp,
struct svc_req *rqstp)
{
static int result;
myserver* ms;
result = addANI_cpp(ms, argp);
return(&result);
}
/******END of C .c FILE******/

With that all said here is some background information on what is going on here.
-This is on a Unix sun platform.
-The C code is mostly generated by rpcgen.
-Therefore the main() is in the C code.
-The above example, although it may have typos right now,
compiles but fails during linking with the following error:
"ild: (undefined symbol) addANI_cpp -- refereneced in the
text segment of itms_server.o"
(itms_server.o is the C code)
-The above example is the most complicated I could find on
the internet. However, even the simple approach of
defining the C++ function addANI as an extern "C" function
yields the same results.
-I've had no problem going from C++ to C.
-The Makefile for the project was created by Workshop.


That is all I can think of right now. I'm wondering if I'm missing some type of flag on the compiler or something like that. If anyone has experience with this, any and all suggestions would be greatly appreciated.

Thanks for the help,
Eric
 
Do you have any warning during compilation?
Also try to put exact definition of addANI_cpp:
in h file:
extern int addANI_cpp(myServer* myserver,
struct ANIstruct* aniStruct);
and in cpp:
int addANI_cpp(myServer* myserver,
struct ANIstruct* aniStruct)
{
...
}


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top