Linux C程序多文件编译

导读:本篇文章讲解 Linux C程序多文件编译,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

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
  1 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/day02$ ls
add.c  add.h  main.c  max.c  max.h
wys@DESKTOP-2OU3HRV:~/mycode/day02$ gcc -c add.c
wys@DESKTOP-2OU3HRV:~/mycode/day02$ gcc -c max.c
wys@DESKTOP-2OU3HRV:~/mycode/day02$ gcc -c main.c
wys@DESKTOP-2OU3HRV:~/mycode/day02$ ls
add.c  add.h  add.o  main.c  main.o  max.c  max.h  max.o
wys@DESKTOP-2OU3HRV:~/mycode/day02$ gcc -o main main.o add.o max.o
wys@DESKTOP-2OU3HRV:~/mycode/day02$ ls
add.c  add.h  add.o  main  main.c  main.o  max.c  max.h  max.o
wys@DESKTOP-2OU3HRV:~/mycode/day02$ ./main
a + b=5
max=5
wys@DESKTOP-2OU3HRV:~/mycode/day02$

7.一步编译

wys@DESKTOP-2OU3HRV:~/mycode/day02$ ls
add.c  add.h  main  main.c  max.c  max.h
wys@DESKTOP-2OU3HRV:~/mycode/day02$ ./main
a + b=5
max=5
wys@DESKTOP-2OU3HRV:~/mycode/day02$

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/95590.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!