歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> C函數和C++函數相互調用

C函數和C++函數相互調用

日期:2017/3/1 9:57:26   编辑:Linux編程

C函數和C++函數相互調用

test.c

int call_MyMath_sum (int,int);

int sum(inta , intb) {
return call_MyMath_sum(a,b);
}

main.cpp

#include <iostream>
using namespace std;

extern"C" {
int sum(int, int);
}

class MyMath {
public:
static int sum(int, int);
};
intMyMath::sum(inta, intb) {
return(a + b);
}

extern"C" int call_MyMath_sum (inta , intb) {
return(MyMath::sum(a,b));
}

int main(void) {
cout<<sum(5,6);
return0;
}

Makefile

main.o:
g++ -c -o main.o main.cpp
test.o:
gcc -c -o test.o test.c
main: main.o test.o
g++ -o main main.o test.o
all: clean main
clean:
rm -f test.o main.o

Copyright © Linux教程網 All Rights Reserved