C 鍜?C++ 浜掔浉璋冪敤

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C 鍜?C++ 浜掔浉璋冪敤相关的知识,希望对你有一定的参考价值。

鏍囩锛?/p>

 

鍓嶈█

    娌′粈涔堝ソ璇寸殑锛岀洿鎺ヤ笂浠g爜锛屼竴鍥捐儨鍗冭█锛岃€屽湪杩欓噷鎴戠浉淇′竴浠g爜鑳滃崈瑷€锛屼唬鐮佷細鎸佺画鏇存柊锛屽綋鐒舵槸鎴戞剰璇嗗埌浠g爜涓嚭鐜颁粈涔堢己澶憋紝鎴栬€呮紡娲炵殑鏃跺€欙紝浠g爜鍦?archlinux 涓婄紪璇戦€氳繃锛岃嚦浜庡叾浠栧钩鍙帮紝娌℃湁璐瑰績鍘婚獙璇佷簡銆?/p>

C 璋?C++
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>



$ cat a.cpp
# --------------------------------------------------------------------------------------
#include 
#include 

class student{
    public: std::string name;
    public: int age;
    public: void show_age(void){
        std::cout << name << "鈥榮 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 [email protected]

$(CPPOBJS):%.o:%.cpp
	$(CPP) -c $< -o [email protected]

# $(CDEF):%.d:%.c
# 	$(MAKEDEPEND) $(<:.c=.o) $< $(DEFINC) > [email protected]

# $(CPPDEF):%.d:%.cpp
# 	$(MAKEDEPEND) $(<:.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++ 璋?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 [email protected]

$(CPPOBJS):%.o:%.cpp
	$(CPP) -c $< -o [email protected]

# $(CDEF):%.d:%.c
# 	$(MAKEDEPEND) $(<:.c=.o) $< $(DEFINC) > [email protected]

# $(CPPDEF):%.d:%.cpp
# 	$(MAKEDEPEND) $(<:.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)
# ======================================================================================


 

    璇风瓑寰呮洿鏂?..


鍙傝€冩枃妗?/div>
鐩稿叧鏂囨。
闅忎究杞浇锛屾垜涓嶄粙鎰忥紝鏈枃妗i伒浠?wtfpl 鍗忚
鎴戞槸鐣欑櫧锛涙垜鏄暀鐧斤紱鎴戞槸鐣欑櫧锛涳紙閲嶈鐨勪簨鎯呰涓夐亶锛?/div>

以上是关于C 鍜?C++ 浜掔浉璋冪敤的主要内容,如果未能解决你的问题,请参考以下文章

Python 妯″潡璋冪敤

IHttpClientFactory璋冪敤鎺ュ彛

PHP瀹炵幇杩滅▼杩囩▼璋冪敤RPC

濂楁帴瀛椾箣sendmsg绯荤粺璋冪敤

寰俊api璋冪敤闄愬埗锛?5009 reach max api daily quota limit 瑙e喅鏂规硶

灏忕▼搴忎簯鍑芥暟璋冪敤webservice鎺ュ彛