/* head.h */
#ifndef __SUM_H__ #define __SUM_H__ #ifdef __cplusplus extern "C" { #endif int add(int a, int b); int sub(int a, int b); #ifdef __cplusplus } #endif #endif /* __SUM_H__ */
sum.cpp
#include <iostream> #include "head.h" using namespace std; int main() { int a, b; cout << "请输入两个数字:" ; cin >> a >> b; cout << "相加:" << add(a, b) << endl; cout << "相减:" << sub(a, b) << endl; }
test.c
#include <stdio.h> #include "head.h" int add(int a, int b) { return a+b; } int sub(int a, int b) { return a-b; }
编译测试:g++ sum.cpp test.c
或者将c文件编译成.o文件:gcc -c test.c 生成test.o 然后: g++ sum.cpp test.o