标签:
没什么好说的,直接上代码,一图胜千言,而在这里我相信一代码胜千言,代码会持续更新,当然是我意识到代码中出现什么缺失,或者漏洞的时候,代码在 archlinux 上编译通过,至于其他平台,没有费心去验证了。
C 调 C++ >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> $ cat a.cpp # -------------------------------------------------------------------------------------- #include #include class student{ public: std::string name; public: int age; public: void show_age(void){ std::cout << name << "‘s age is : " << age << std::endl; } }; extern "C" void f(int); void f(int i){ std::cout << "in f function i value is : " << i <<std::endl; } extern "C" void *create_student( void ); void *create_student( void ){ student *xiaoming = NULL; xiaoming = new student(); xiaoming->student::name = "小明"; xiaoming->student::age = 24; std::cout << "create successful" << std::endl; return xiaoming; } extern "C" void show_student( void *std ); void show_student( void *std){ ((student*)std)->student::show_age(); } # ====================================================================================== $ cat b.c # -------------------------------------------------------------------------------------- #include extern void f(int); extern void *create_student(void); extern void show_student(void*); int main( int argv, char **argc ){ void *std; std = create_student(); show_student( std ); return 0; } # ====================================================================================== $ cat Makefile # -------------------------------------------------------------------------------------- CC=gcc CPP=g++ MAKEDEPEND=gcc -MM -MT DEFINC = LDFLAGS += -lstdc++ CSRCS += b.c CPPSRCS += a.cpp COBJS = $(CSRCS:.c=.o) CPPOBJS = $(CPPSRCS:.cpp=.o) # CDEF = $(CSRCS:.c=.d) # CPPDEF = $(CPPSRCS:.cpp=.d) TAR_NAME=test DEF_RUNTAG += show_all_param DEF_RUNTAG += $(TAR_NAME) ALL: $(DEF_RUNTAG) $(COBJS):%.o:%.c $(CC) -c $< -o $@ $(CPPOBJS):%.o:%.cpp $(CPP) -c $< -o $@ # $(CDEF):%.d:%.c # $(MAKEDEPEND) $(<:.c=.o) $< $(DEFINC) > $@ # $(CPPDEF):%.d:%.cpp # $(MAKEDEPEND) $(<:.cpp=.o) $< $(DEFINC) > $@ $(TAR_NAME):$(COBJS) $(CPPOBJS) $(CC) -o $(TAR_NAME) $(COBJS) $(CPPOBJS) $(LDFLAGS) -@rm -rf $(COBJS) -@rm -rf $(CPPOBJS) -@rm -rf $(CDEF) -@rm -rf $(CPPDEF) show_all_param: @echo $(CC) @echo $(CPP) @echo $(CSRCS) @echo $(CPPSRCS) @echo $(COBJS) @echo $(CPPOBJS) @echo $(CDEF) @echo $(CPPDEF) @echo $(DEF_RUNTAG) clean: -@rm -rf $(COBJS) -@rm -rf $(CPPOBJS) -@rm -rf $(CDEF) -@rm -rf $(CPPDEF) -@rm -rf $(TAR_NAME) #include $(CDEF) #include $(CPPDEF) # ====================================================================================== C++ 调 C >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> $ cat c.c # -------------------------------------------------------------------------------------- #include int show_student_msg( const char *iname, int iage ){ const char *tname = (const char*)iname; printf("student %s age: %d\n", tname, iage); return -1; } # ====================================================================================== $ cat d.cpp # -------------------------------------------------------------------------------------- #include extern "C"{ extern int show_student_msg( const char *iname, int iage ); } int main( int argv, char *argc[] ){ std::cout<< "hello world!" << std::endl; show_student_msg( "小明", 24 ); const char *tname1 = "小华"; std::string tname2( tname1 ); show_student_msg( tname2.c_str(), 24 ); } # ====================================================================================== $ cat Makefile # -------------------------------------------------------------------------------------- CC=gcc CPP=g++ MAKEDEPEND=gcc -MM -MT DEFINC = LDFLAGS += -lstdc++ CSRCS += c.c CPPSRCS += d.cpp COBJS = $(CSRCS:.c=.o) CPPOBJS = $(CPPSRCS:.cpp=.o) # CDEF = $(CSRCS:.c=.d) # CPPDEF = $(CPPSRCS:.cpp=.d) TAR_NAME=test DEF_RUNTAG += show_all_param DEF_RUNTAG += $(TAR_NAME) ALL: $(DEF_RUNTAG) $(COBJS):%.o:%.c $(CC) -c $< -o $@ $(CPPOBJS):%.o:%.cpp $(CPP) -c $< -o $@ # $(CDEF):%.d:%.c # $(MAKEDEPEND) $(<:.c=.o) $< $(DEFINC) > $@ # $(CPPDEF):%.d:%.cpp # $(MAKEDEPEND) $(<:.cpp=.o) $< $(DEFINC) > $@ $(TAR_NAME):$(COBJS) $(CPPOBJS) $(CPP) -o $(TAR_NAME) $(COBJS) $(CPPOBJS) $(LDFLAGS) -@rm -rf $(COBJS) -@rm -rf $(CPPOBJS) -@rm -rf $(CDEF) -@rm -rf $(CPPDEF) show_all_param: @echo $(CC) @echo $(CPP) @echo $(CSRCS) @echo $(CPPSRCS) @echo $(COBJS) @echo $(CPPOBJS) @echo $(CDEF) @echo $(CPPDEF) @echo $(DEF_RUNTAG) clean: -@rm -rf $(COBJS) -@rm -rf $(CPPOBJS) -@rm -rf $(CDEF) -@rm -rf $(CPPDEF) -@rm -rf $(TAR_NAME) #include $(CDEF) #include $(CPPDEF) # ======================================================================================
请等待更新...
标签:
原文地址:http://www.cnblogs.com/mojies/p/5572163.html