首页 技术 正文
技术 2022年11月10日
0 收藏 857 点赞 4,245 浏览 1937 个字

下午闲来无聊,就打开很久没动过的linux系统想熟悉熟悉在linux上面编译代码,结果一个makefile文件搞到晚上才搞定,哈哈!

先把代码简单贴上来,就写了一个冒泡排序:

sort.h:

#ifndef SORT_H
#define SORT_H#include<stdio.h>
#include<time.h>
#include<stdlib.h>#define N 10
#define swap(a,b) {a^=b;b^=a;a^=b;}
#define ins(a,b,c) for( a = b ; a < c ; ++a)void bub_sort();#endif

sort.c:

#include"sort.h"
void bub_sort(){
int *in;
int i , j;
in = malloc(N*sizeof(int));
srand((unsigned)time(NULL));
ins(i,0,N){
in[i] = rand()%100;
}ins(i,0,N){
printf("%d%s",in[i],i == N-1 ? "\n" : "->");
}ins(i,0,N-1){
ins(j,i,N){
if(in[i] > in[j]){
swap(in[i] , in[j]);
}
}
}ins(i,0,N){
printf("%d%s" , in[i] , i == N-1 ? "\n" : "->");
}
free(in);
in = NULL;
}

main.c:

#include"sort.h"int main(){
bub_sort();
return 0;
}

如果直接编译的话得用三条指令:

gcc -c sort.c -o sort.ogcc -c main.c -o main.ogcc main.o sort.o -o main

  

每一次编译的时候如果都得敲这三行代码就显得效率很低了,所以尝试着写一个makefile文件;

编写makefile时有一定的规则:

目标(target) : 需要的条件(dependencies) (注意冒号两边有空格)

    命令(system command)  (注意前面用tab键开头)

  解释一下:

  1 目标可以是一个或多个,可以是Object File,也可以是执行文件,甚至可以是一个标签。

  2 需要的条件就是生成目标所需要的文件或目标

  3 命令就是生成目标所需要执行的脚本

  总结一下,就是说一条makefile规则规定了编译的依赖关系,也就是目标文件依赖于条件,生成规则用命令来描述。在编译时,如果需要的条件的文件比目标更新的话,就会执行生成命令来更新目标。

makefile:

OBJS = main.o sort.omain: $(OBJS)
gcc $(OBJS) -o main //注意:命令前面必须为(tab)
main.o: main.c sort.h
gcc -c main.c -o main.o
sort.o: sort.c sort.h
gcc -c sort.c -o sort.o
clean:
rm -rf *.o main

上面的makefile文件中执行四条指令:

第一:

main: $(OBJS)                #main依赖于main.o sort.o(tab)gcc $(OBJS) -o main     #命令行,前面必须为tab,编译出main可执行文件。-o表示你指定 的目标文件名。
第二:
main.o: main.c sort.h            #main.o依赖于main.c sort.h
  gcc -c main.c -o main.o         编译出file1.o文件。-c表示gcc 只把给它的文件编译成目标文件, 用源码文件的文件名命名但把其后缀由“.c”变成“.o”。在这句中,可以省略-o file1.o,编译器默认生成file1.o文件,这就是-c的作用。
第三:
sort.o: sort.c sort.h            #与第二类似
gcc -c sort.c -o sort.o
第四:
clean:
rm -rf *.o main
当用户键入make clean命令时,会删除*.o 和helloworld文件。

写好makefile,在终端里面直接键入make就会执行makefile中的指令了:

m@m-computer:~/c学习/hello$ make
gcc main.o sort.o -o mainm@m-computer:~/c学习/hello$ ls
main main.c main.o makefile sort.c sort.h sort.om@m-computer:~/c学习/hello$ ./main
84->6->29->53->82->74->42->55->20->56
6->20->29->42->53->55->56->74->82->84

  

参考:http://goodcandle.cnblogs.com/archive/2006/03/30/278702.html

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,086
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,561
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,410
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,183
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,820
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,903