Linux下c语言基础——makefile

目标:linux下c语言外部函数调用,完成Makefile (demo)

main.c  test1.c  test1.h
  1. test1.h头文件,负责申明函数,引入依赖
#ifndef _MYTOOL_1_H 
#define _MYTOOL_1_H 
#include <stdio.h>
void mytool1_print(char *print_str);#申明函数

#endif
~      
  1. test1.c 实现函数;
#include "test1.h"  #引入头文件
void mytool1_print(char *print_str)
{
        printf("已进入test1.c中的方法.\n");
        printf("入参 =%s ",print_str);

}
  1. main.c 主函数调用test1中的函数
#include "test1.h"  #同样需要引入头文件
int main(int args,char **argv)
{
        printf("进入main方法.\n");
        mytool1_print("hello");  #调用函数
}
  • 编译 gcc test1.c main.c -o main
  • 执行 ./main
  • 输出如下
进入main方法.
已进入test1.c中的方法.
入参 =hello 

8.定义makefile文件——Makefile

#this is a makefile
main: main.o test1.o
    gcc -o main main.o test1.o 
main.o: main.c test1.h
    gcc -c -o main.o main.c
test1.o: test1.c test1.h
    gcc -c -o test1.o test1.c

注意点:

  • 文件名为Makefile
  • 按照makefile格式:
target: components 
	TAB rule 
  • 命令必须TAB缩进,如果缩进超过4个空格,需要在vim /etc/vimrc 最后加上
set ts=4
set expandtab
set autoindent
  • 不能有中文标点符号。
  • 如果不是正确的缩进方式,或者有中文标点,则会提示Makefile:2: *** missing separator. Stop.

输入make main命令后会生成自动编译生成

[root@localhost demo]# make main
gcc -o main main.o test1.o 
[root@localhost demo]# ls
  main  main.c  main.o  Makefile  test1.c  test1.h  test1.o
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值