hab das mit der Wrapper-Geschichte mal ausprobiert und es funktioniert bei mir leider nicht.
Hab folgendes gemacht:
- eine einfache C++ Klasse erstellt (also cplusplus.h und cplusplus.cpp ähnlich wie im Beispielcode s.o.)
- Wrapper erstellt (.h und .cpp)
wrapper.h
Code:
#ifndef _WRAPPER_H_
#define _WRAPPER_H_
struct Cpp;
#ifdef __cplusplus
extern "C"
{
#endif /* ifdef __cplusplus */
12: Cpp* createCPP(int);
13: int getNum(Cpp*);
14: void deleteCpp(Cpp*);
#ifdef __cplusplus
}
#endif /* ifdef __cplusplus */
#endif
wrapper.cpp
Code:
#include "wrapper.h"
#include "cplusplus.h"
#ifdef __cplusplus
extern "C"
{
#endif /* ifdef __cplusplus */
Cpp* createCPP(int n) {
return new Cpp(n);
}
int getNum(Cpp* temp) {
return temp->num();
}
void deleteCpp(Cpp* temp) {
delete temp;
}
#ifdef __cplusplus
}
#endif /* ifdef __cplusplus */
- test.c mit main()
test.c
Code:
#include <stdio.h>
#include "wrapper.h"
int main(int argc, char** argv) {
Cpp* test;
test = createCPP(5);
printf("Test: %d\n", getNum(test));
deleteCpp(test);
return 0;
}
- compiliert
c++ Klasse mit "g++ -c cplusplus.cpp"
wrapper mit "g++ -c wrapper.cpp"
test.c mit "gcc -c test.c"
cplusplus.cpp und wrapper.cpp werden ohne Fehler compiliert. Bei der test.c gibt es Fehler:
Code:
seven@seven:~/dipl/tmp> gcc -Wall -c test.c
In file included from test.c:3:
wrapper.h:12: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
wrapper.h:13: error: expected ‘)’ before ‘*’ token
wrapper.h:14: error: expected ‘)’ before ‘*’ token
test.c: In function ‘main’:
test.c:7: error: ‘Cpp’ undeclared (first use in this function)
test.c:7: error: (Each undeclared identifier is reported only once
test.c:7: error: for each function it appears in.)
test.c:7: error: ‘test’ undeclared (first use in this function)
test.c:9: warning: implicit declaration of function ‘createCPP’
test.c:10: warning: implicit declaration of function ‘getNum’
test.c:11: warning: implicit declaration of function ‘deleteCpp’
Es scheint als ob er das Cpp* test; nicht mag. In der wrapper.h ist ein struct Cpp; definiert.
Der Zeiger test (aus test.c) müsste ja eigentlich auf eine struct Cpp zeigen. Die Funktion createCpp() (aus wrapper.cpp) liefert mir aber doch einen Zeiger auf eine Instanz der Klasse Cpp zurück. Kann das überhaupt funktionieren oder hab ich irgendwas falsch verstanden?
Lesezeichen