Makefile

head.h

#ifndef HEAD_H_
#define HEAD_H_
    #ifdef _cplusplus
        extern "C"{
    #endif
    void File2Print();
    #ifdef _cplusplus
        }
    #endif
#endif

file1.c

#include <stdio.h>
#include "head.h"
int main(){
    printf("print file 1 $$$$$$$$$$$$$$$$$$\n");
    File2Print();
    return 0;
}

file2.c

#include "head.h"
void File2Print(){
    printf("Print file2 **************\n");
}
makefile-1

helloworld:file1.o file2.o                              //  helloworld依赖 file1.o 和 file2.o 两个目标文件
    gcc file1.o file2.o -o helloworld              // 编译helloworld可执行文件     -o 指定的目标文件
file1.o:file1.c
    gcc -c file1.c -o file1.o                             // -c 只把源码文件编译成目标文件
file2.o:file2.c head.h                                  //file2.c中无stdio.h 必须加 head.h
    gcc -c file2.c -o file2.o
clean:                                                            //make clean 会删除
    rm -rf *.o helloworld

命令模式

A:B

(tab) <command>

makefile-2

OBJS = file1.o file2.o
CC = gcc
CFLAGS = -Wall -O -g                                //输出所有警告信息         在编译时进行优化          编译debug版本helloworld:$(OBJS)
    $(CC) $(OBJS) -o helloworld
file1.o:file1.c
    $(CC) $(CFLAGS) -c file1.c -o file1.o
file2.o:file2.c head.h
    $(CC) $(CFLAGS) -c file2.c -o file2.o
clean:
    rm -rf *.o helloworld

makefile-3

XX = g++
CC = gcc
CFLAGS = -Wall -O -g
TARGET = ./helloworld
%.o:%.c
    $(CC) $(CFLAGS) -c $< -o $@                          // $@ 扩展为当前规则的目的文件名   $< 扩展为依靠列表中的第一个依靠文件  $^整个依靠的列表
%.o:%.cpp
    $(XX) $(CFLAGS) -c $< -o $@
SOURCES = $(wildcard *.c *.cpp)                        //wildcard 产生一个所有以.c, .cpp结尾的文件的列表,然后存入变量里
OBJS = $(patsubst %.c, %.o, $(patsubst %.cpp, %.o, $(SOURCES)))   //匹配替换,第一个是需要匹配的样式,第二个是用什么来替换它,第三个是一个需要被处理的由空格分隔的列表
$(TARGET):$(OBJS)
    $(XX) $(OBJS) -o $(TARGET)
    chmod a+x $(TARGET)
clean:
    rm -rf *.o helloworld



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值