C程序多文件编译
在Linux平台C编程,实现求两数最大值和两数之和的功能
1.编写add.c
wys@DESKTOP-2OU3HRV:~/mycode/day02$ vi add.c
1 int add(int x,int y)
2 {
3 return x + y;
4 }
2.编写头文件add.h
wys@DESKTOP-2OU3HRV:~/mycode/day02$ vi add.h
int add(int x,int y);
3.编写max.c
wys@DESKTOP-2OU3HRV:~/mycode/day02$ vi max.c
1 int max(int x,int y)
2 {
3 return x > y ? x : y;
4 }
4.编写max.h
wys@DESKTOP-2OU3HRV:~/mycode/day02$ vi max.h
1 int max(int x,int y);
5.编写main.c
wys@DESKTOP-2OU3HRV:~/mycode/day02$ vi main.c
1 #include<stdio.h>
2 #include"add.h"
3 #include"max.h"
4 int main()
5 {
6 int a = 2;
7 int b = 3;
8 printf("a + b=%d\n",add(a,b));
9 printf("max=%d\n",max(a,b));
10 return 0;
11 }
~
6.分布编译链接
wys@DESKTOP-2OU3HRV:~/mycode/day04$ ls
add.c add.h main.c max.c max.h
wys@DESKTOP-2OU3HRV:~/mycode/day04$ gcc -c add.c
wys@DESKTOP-2OU3HRV:~/mycode/day04$ gcc -c max.c
wys@DESKTOP-2OU3HRV:~/mycode/day04$ gcc -c main.c
wys@DESKTOP-2OU3HRV:~/mycode/day04$ ls
add.c add.h add.o main.c main.o max.c max.h max.o
wys@DESKTOP-2OU3HRV:~/mycode/day04$ gcc -o main main.o add.o max.o
wys@DESKTOP-2OU3HRV:~/mycode/day04$ ls
add.c add.h add.o main main.c main.o max.c max.h max.o
wys@DESKTOP-2OU3HRV:~/mycode/day04$ ./main
a + b=5
max=5
wys@DESKTOP-2OU3HRV:~/mycode/day04$
7.一步编译
wys@DESKTOP-2OU3HRV:~/mycode/day04$ ls
add.c add.h main main.c max.c max.h
wys@DESKTOP-2OU3HRV:~/mycode/day04$ ./main
a + b=5
max=5
wys@DESKTOP-2OU3HRV:~/mycode/day04$