沒什麼好說的,直接上代碼,一圖勝千言,而在這裏我相信一代碼勝千言,代碼會持續更新,固然是我意識到代碼中出現什麼缺失,或者漏洞的時候,代碼在 archlinux 上編譯經過,至於其餘平臺,沒有費心去驗證了。linux
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) # ======================================================================================
請等待更新...c++