C and C + + call each other

Source: Internet
Author: User

Objective

Nothing to say, directly on the code, a picture to win thousands of words, and here I believe a code wins thousands of words, the code will continue to update, of course, I am aware of the code in the absence of any, or loopholes, the code is compiled on the ArchLinux, as for other platforms, did not bother to verify.

C-Tune 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 was:" << 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 = "Xiao Ming";    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=gcccpp=g++makedepend=gcc-mm- Mtdefinc =ldflags + =-lstdc++csrcs + B.ccppsrcs + = A.CPPCOBJS = $ (CSRCS:.C=.O) Cppobjs = $ (CPPSRCS:.CPP=.O) # CDEF = $ (CSRC S:.C=.D) # Cppdef = $ (CPPSRCS:.CPP=.D) Tar_name=testdef_runtag + = Show_all_paramdef_ruNTAG + = $ (tar_name) All: $ (def_runtag) $ (COBJS):%.o:%.c$ (CC)-C $<-O [email protected]$ (CPPOBJS):%.o:%.cpp$ ( CPP)-C $<-O [email protected]# $ (CDEF):%.d:%.c# $ (makedepend) $ (&LT;:.C=.O) $< $ (definc) > [EMAIL&NBSP;PR otected]# $ (cppdef):%.d:%.cpp# $ (makedepend) $ (&LT;:.CPP=.O) $< $ (definc) > [email protected]$ (TAR_NAME): $ (COBJS) $ (CPPOBJS) $ (CC)-O $ (tar_name) $ (COBJS) $ (CPPOBJS) $ (ldflags) [email protected]-rf $ (COBJS) [email  Protected]-RF $ (CPPOBJS) [email protected]-rf $ (cdef) [email protected]-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:[email protected]-rf $ (COBJS) [email protected]-rf $ (CPPOBJS) [email protected]-rf $ ( CDEF) [email protected]-rf $ (cppdef) [email protected]-rf $ (tar_name) #include $ (cdef) #include $ (cppdef) # = = ====================================================================================c++ Tune C>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>$ Cat c.c#------------------------------------------------------ --------------------------------#include int show_student_msg (const char *iname, int iage) {const char *tname = (CONST CH ar*) 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 ("Xiaoming", "*tname1", const char = "Xiao Hua"; std::string tname2 (tname1); Show_student_msg (Tname2.c_str (), 24);} # ======================================================================================$ Cat Makefile#---------- ----------------------------------------------------------------------------cc=gcccpp=g++makedepend=gcc-mm- Mtdefinc =ldflags + =-lstdc++csrcs + C.ccppsrcs + = D.CPPCOBJS = $ (CSRCS:.C=.O) Cppobjs = $ (CPPSRCS:.CPP=.O) # CDEF = $ (CSRC S:.C=.D) # Cppdef = $ (CPPSRCS:.CPP=.D) Tar_name=testdef_runtag + = Show_all_paramdef_runtag + $ (TAR_NAME) All: $ (DEF_ Runtag) $ (COBJS):%.o:%.c$ (CC)-C $<-O [email protected]$ (CPPOBJS):%.o:%.cpp$ (CPP)-C $<-O [email  protected]# $ (cdef):%.d:%.c# $ (makedepend) $ (&LT;:.C=.O) $< $ (definc) > [email protected]# $ (CPPDEF):%.d:%. cpp# $ (makedepend) $ (&LT;:.CPP=.O) $< $ (definc) > [email protected]$ (Tar_name): $ (COBJS) $ (CPPOBJS) $ (CPP)-O $ (tar_name) $ (COBJS) $ (CPPOBJS) $ (ldflags) [email protected]-rf $ (COBJS) [email protected]-rf $ (CPPOBJS) [email protected]-rf $ (cdef) [email protected]-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:[email protected]-rf $ (COBJS) [email protected]-rf $ (CPPOBJS) [email protected]-rf $ (CDEF) [email protected]-rf $ (cppdef) [email protected]-rf $ (tar_name) #include $ (cdef) #include $ (cppdef) # ======================================================================================

Please wait for the update ...

Refer to documents related documents casually reproduced, I do not mind, this document complies with the WTFPL Protocol I was left white; I was white; (important thing to say three times)

C and C + + call each other

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.