Linux中Makefile工具的简单使用

 时间:2026-02-14 06:38:10

1、Ctrl+Alt+T打开终端,查看GCC和VIM(vim-nox)软件包是否安装成功,可输入gcc --version或vim查看版本号确认安装成功。

Linux中Makefile工具的简单使用

Linux中Makefile工具的简单使用

2、使用VIM编辑器编写Makefile文件:

(1)首先编写一个““Hello,World!”程序”

建立一个hello_test.c程序,源代码如下:

#include <stdio.h>

         int main(void)

         {

          printf("Hello,World!\n");  /* 打印字符串Hello,World!到屏幕 */

          return 0;

         }

(2)在hello_test.c所在目录:输入vi Makefile(vim Makefile)

(3)输入程序:

hello_test : hello_test.c

               gcc -o hello_test hello_test.c

clean :

               rm -fr hello_test *.o *.core

(4)按下Esc键后输入:wq保存文件并退出。

Linux中Makefile工具的简单使用

Linux中Makefile工具的简单使用

Linux中Makefile工具的简单使用

Linux中Makefile工具的简单使用

3、对hello_test.c编译:make

(1)如果没错,则屏幕弹出“gcc –o hello_test hello_test.c”

(2)找到hello_test可执行文件(绿色)

(3)执行文件:./hello_test

Linux中Makefile工具的简单使用

Linux中Makefile工具的简单使用

4、通过make的参数指定执行clean依赖关系:

输入make clean,查看hello_test可执行文件的变化。

Linux中Makefile工具的简单使用

Linux中Makefile工具的简单使用

1、使用vim编写mytool1.c 、mytool2.c、main.c、mytool1.h、mytool2.h、makefile文件。

mytool1.c                             

#include “mytool1.h”

#include<stdio.h>

void mytool1_print(char *print_str)

{:

  printf(“This is mytool1 print:%s”,print_str);

}

 mytool2.c

#include “mytool2.h”

#include<stdio.h>

void mytool2 _print(char *print_str)

{

  printf(“This is mytool2 print:%s”,print_str);

}

main.c

#include “mytool1.h”

#include “mytool2.h”

int main()

{

mytool1_print(“hello mytool1!”);

mytool2_print(“hello mytool2!”);

return 0;

}

mytool1.h

#ifndef  _MYTOOL_1_H

#define  _MYTOOL_1_H

void mytool1_print(char *print_str);

#endif 

mytool2.h

#ifndef  _MYTOOL_2_H

#define  _MYTOOL_2_H

void mytool2_print(char *print_str);

#endif

makefile

target: mytool1.o mytool2.o main.o      

         gcc -g -o target mytool1.o mytool2.o main.o

mytool1.o: mytool1.c mytool1.h

         gcc -g -c mytool1.c

mytool2.o: mytool2.c mytool2.h

         gcc -g -c mytool2.c

main.o: main.c mytool1.h mytool2.h

         gcc -g -c main.c

Linux中Makefile工具的简单使用

Linux中Makefile工具的简单使用

Linux中Makefile工具的简单使用

Linux中Makefile工具的简单使用

Linux中Makefile工具的简单使用

Linux中Makefile工具的简单使用

2、(1)执行make,

(2)查看文件ls,可以看见生成了一个可执行文件target

(3)执行文件显示结果:./target

Linux中Makefile工具的简单使用

Linux中Makefile工具的简单使用

Linux中Makefile工具的简单使用

1、更改makefile文件

改进一:

SRCS=mytool1.c mytool2.c main.c

PROG=target

CC=gcc

CFLAGS=-g

OBJS=$(SRCS:.c=.o)

$(PROG):$(OBJS)

         $(CC)  $(CFLAGS) -o $@ $^

$(OBJS):   mytool1.h mytool2.h

clean:

         rm –f  $(OBJS)  $(PROG)

Linux中Makefile工具的简单使用

2、先删除可执行文件target,用ls查看文件是否删除成功,在执行make,用ls查看发现生成一个可执行文件target,执行文件。执行make clean命令,用ls查看文件的变化。

Linux中Makefile工具的简单使用

Linux中Makefile工具的简单使用

Linux中Makefile工具的简单使用

Linux中Makefile工具的简单使用

Linux中Makefile工具的简单使用

Linux中Makefile工具的简单使用

3、改进二:

trget:main.o mytool1.o mytool2.o

         gcc -g -o target main.o mytool1.o mytool2.o

main.o mytool1.o mytool2.o: mytool1.h mytool2.h

Linux中Makefile工具的简单使用

4、先删除可执行文件target,用ls查看文件是否删除成功,在执行make,用ls查看发现生成一个可执行文件target,执行文件。

Linux中Makefile工具的简单使用

Linux中Makefile工具的简单使用

Linux中Makefile工具的简单使用

Linux中Makefile工具的简单使用

Linux中Makefile工具的简单使用

  • 如何获取div中a标签的id
  • Visual Studio如何打开通知窗口
  • .net的controller中如何让页面跳转
  • 奇迹暖暖怎么利用大喵的指引完成套装
  • QQ飞车师徒系统介绍、礼包、奖励
  • 热门搜索
    炒苦瓜的做法 鸡肉的做法大全家常菜 鱼香肉丝的做法 thin怎么读 红烧排骨怎么做 开关怎么接线 鳕鱼炖豆腐的做法 酸梅汤的做法与配方 小鲫鱼的做法大全 赣怎么读