码迷,mamicode.com
首页 > 编程语言 > 详细

C 和 C++ 互相调用

时间:2016-06-09 12:16:31      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:

 

前言

    没什么好说的,直接上代码,一图胜千言,而在这里我相信一代码胜千言,代码会持续更新,当然是我意识到代码中出现什么缺失,或者漏洞的时候,代码在 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)
# ======================================================================================


 

    请等待更新...


参考文档
相关文档
随便转载,我不介意,本文档遵从 wtfpl 协议
我是留白;我是留白;我是留白;(重要的事情说三遍)

C 和 C++ 互相调用

标签:

原文地址:http://www.cnblogs.com/mojies/p/5572163.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!