Hello guys,
Ok here is the issue/roadblock. I am trying to call a method that is a member of a c++ class. I know I need to have a wrapper function to give C a pointer to the member function. I have created a shared header file <header.h> and 2 source files. <cSource.c> and <cppSource.cpp> . Then of course the main file written in c. <cMain.c>
If anyone could help me get this small example working so that I can experiment with the actual problem I'm trying to overcome it would be absolutely terrific.
Here is the code I have:
(the commands to compile and errors located at the end of post)
header.h
cSource.c
cppSource.cpp
cMain.c
$>gcc -c cMain.c -o main.o
$>gcc -c cSource.c -o csource.o
$>g++ -c cppSource.cpp -o cppsource.o
$>gcc -o main.o csource.o cppsource.o //This gives the following error.
csource.o: In function `cSpeak':cSourc e.c:(.text+0xd) : undefined reference to `cSayHello'
collect2: ld returned 1 exit status
Thanks for all the help in advance!
Ok here is the issue/roadblock. I am trying to call a method that is a member of a c++ class. I know I need to have a wrapper function to give C a pointer to the member function. I have created a shared header file <header.h> and 2 source files. <cSource.c> and <cppSource.cpp> . Then of course the main file written in c. <cMain.c>
If anyone could help me get this small example working so that I can experiment with the actual problem I'm trying to overcome it would be absolutely terrific.
Here is the code I have:
(the commands to compile and errors located at the end of post)
header.h
Code:
/*Start CPP header information */
#ifndef HEADER_H
#define HEADER_H
#ifdef __cplusplus
class cClass
{
public:
void sayHello();
cClass (){};
};
extern "C" cClass* cSayHello(cClass* cclass);
#else
/*End CPP header information */
///////////////////////////////////
/*Start C Header Information */
typedef struct cClass {}cClass;
#endif
#ifdef __cplusplus
extern "C"
{
#endif
void cSpeak(struct cClass* cclass);
#ifdef __cplusplus
}
#endif
/* End C Header Information */
#endif //For Header.h define check.
Code:
#include <stdio.h>
#include "header.h"
void cSpeak(cClass* cclass)
{
cSayHello(cclass);
}
Code:
#include <stdio.h>
#include "header.h"
void cClass::sayHello() //This is the function we are trying to run from C
{
printf ("This is how you say hello, using C that calls a C++ function");
}
extern "C" cClass* cSayHello(cClass* cclass) //This returns a pointer for the C function!
{
cclass->sayHello();
return cclass;
}
Code:
//Includes
#include <stdio.h>
#include "header.h"
int main()
{
cClass cclass;
printf ("void cSpeak(cClass* cclass);");
cSpeak(&cclass);
return 0;
}
$>gcc -c cMain.c -o main.o
$>gcc -c cSource.c -o csource.o
$>g++ -c cppSource.cpp -o cppsource.o
$>gcc -o main.o csource.o cppsource.o //This gives the following error.
csource.o: In function `cSpeak':cSourc e.c:(.text+0xd) : undefined reference to `cSayHello'
collect2: ld returned 1 exit status
Thanks for all the help in advance!
Comment