fun.c
#include<stdio.h>
int my_add(int x,int y)
{
return x+y;
}
int my_sub(int x,int y)
{
return x-y;
}
fun.h
#ifndef FUN_H
#define FUN_H
#if __cplusplus//如果是在c++编译器中这句话就会被执行
extern "C"{ //调用c的编译器,注意是大写的C
#endif
extern int my_add(int x,int y);
extern int my_sub(int x,int y);
#if __cplusplus
}
#endif
#endif // FUN_H
main.cpp
#include <iostream>
#include"fun.h"
using namespace std;
int main(int argc, char *argv[])
{
cout<<my_add(100,200)<<endl;
cout<<my_sub(100,200)<<endl;
return 0;
}
ubuntu情况下
混合编译步骤:
gcc -c fun.c -o fun.o//陷将c文件用c编译器编译成二进制
g++ main.cpp fun.o -o main