如题所述
C source fileçå®ç°ã
#include "obj.h"
int main(const int argc, const char **argv){
void *temp_obj = getObj(100);
printString(temp_obj);//å®ç°Cè¯è¨è°ç¨QT/C++UIçé¢
return (0);
}
åä¸ä¸ªC++ source fileåc source fileé½å¯ä»¥å ±åå å«ç头æ件ï¼å¦ä¸æ示ã
#include <stdint.h>
#ifdef __cplusplus
class Obj{
public:
Obj(int32_t a=50)
~Obj();
std::string toString();
private:
int32_t k;
};
#endif
#ifdef __cplusplus
extern "C" {
#endif
void* getObj(int32_t a);
void destroyObj(void *obj);
void printString(void *obj);
#ifdef __cplusplus
}
#endif
C++ source fileçå®ç°,å ¶ä¸Cè¯è¨å½æ°æ¯ä¾C source fileè°ç¨çã
#include <iostream>
#include <sstream>
#include <iomanip>
#include <stdint.h>
#include "obj.h"
Obj::Obj(int32_t a){
k = a << 1;
}
Obj::~Obj(){
/* don't really need to do anything here */
/* k = 0 only for example purposes */
k = 0;
}
std::string Obj::toString(){
std::ostringstream os;
os << "Obj is currently: " << this->k << std::endl;
return os.str();
}
void* getObj(int32_t a){
Obj *out = new Obj(a);
return ((void*)out);
}
void destroyObj(void* obj){
delete (((Foo*)obj));
}
void printString(void *obj){
std::string s = ((Obj*)obj)->toString();
std::cout << s;
}
Makefile
make fileçå®ç°ã
CC ?= gcc
CXX ?= g++
CFLAGS = -O0 -g
CXXFLGS = -00 -g
OBJ = main obj
OBJS = $(addsuffix .o,$(OBJ))
all:
make compile
compile:
make $(OBJS)
make objexe
fooexe: $(OBJS)
$(CXX) -o fooexe $(OBJS)
main.o: main.c
$(CC) -c -o main.o main.c
obj.o: obj.cpp
$(CXX) -c -o obj.o obj.cpp
clean:
rm -rf $(OBJS) objexe *.dSYM
#include "obj.h"
int main(const int argc, const char **argv){
void *temp_obj = getObj(100);
printString(temp_obj);//å®ç°Cè¯è¨è°ç¨QT/C++UIçé¢
return (0);
}
åä¸ä¸ªC++ source fileåc source fileé½å¯ä»¥å ±åå å«ç头æ件ï¼å¦ä¸æ示ã
#include <stdint.h>
#ifdef __cplusplus
class Obj{
public:
Obj(int32_t a=50)
~Obj();
std::string toString();
private:
int32_t k;
};
#endif
#ifdef __cplusplus
extern "C" {
#endif
void* getObj(int32_t a);
void destroyObj(void *obj);
void printString(void *obj);
#ifdef __cplusplus
}
#endif
C++ source fileçå®ç°,å ¶ä¸Cè¯è¨å½æ°æ¯ä¾C source fileè°ç¨çã
#include <iostream>
#include <sstream>
#include <iomanip>
#include <stdint.h>
#include "obj.h"
Obj::Obj(int32_t a){
k = a << 1;
}
Obj::~Obj(){
/* don't really need to do anything here */
/* k = 0 only for example purposes */
k = 0;
}
std::string Obj::toString(){
std::ostringstream os;
os << "Obj is currently: " << this->k << std::endl;
return os.str();
}
void* getObj(int32_t a){
Obj *out = new Obj(a);
return ((void*)out);
}
void destroyObj(void* obj){
delete (((Foo*)obj));
}
void printString(void *obj){
std::string s = ((Obj*)obj)->toString();
std::cout << s;
}
Makefile
make fileçå®ç°ã
CC ?= gcc
CXX ?= g++
CFLAGS = -O0 -g
CXXFLGS = -00 -g
OBJ = main obj
OBJS = $(addsuffix .o,$(OBJ))
all:
make compile
compile:
make $(OBJS)
make objexe
fooexe: $(OBJS)
$(CXX) -o fooexe $(OBJS)
main.o: main.c
$(CC) -c -o main.o main.c
obj.o: obj.cpp
$(CXX) -c -o obj.o obj.cpp
clean:
rm -rf $(OBJS) objexe *.dSYM
温馨提示:答案为网友推荐,仅供参考