make文件目录
1 2 3 4 5 6
| root@LAPTOP-49LFJ4NT:/root/make# tree . ├── Makefile ├── conf.sh ├── main.cpp └── shell.sh
|
这里使用了shell脚本来实现对于make的编译,由于使用的是wsl
在windows下操作导致编码格式含有\r\n
,以此有两个shell脚本。
conf.sh
1 2 3 4 5 6 7 8
| # 实现对于make的编译与运行 #!/bin/bash if [[ $1 == clean ]]; then make clean elif [[ $1 == all ]]; then make clean && make -j8 && ./main else make -j8 && ./main
|
shell.sh
1 2 3
| # 实现将conf.sh的dos编码转化为unix编码,并执行 #!/bin/bash dos2unix conf.sh && ./conf.sh
|
Makefile
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
|
CC = gcc CPP = g++ LINK = g++
LIBS = -lpthread
CCFLAGS = -c -g -fPIC CPPFLAGS = -c -g -fPIC CCFLAGS = -c -g -fPIC
TARGET=main
INCLUDES = -I.
CPPFILES = $(wildcard *.cpp ) CCFILES = $(wildcard *.cc ) CFILES = $(wildcard *.c )
OBJFILE = $(CFILES:.c=.o) $(CPPFILES:.cpp=.o) $(CCFILES:.cc=.o)
all:$(TARGET)
$(TARGET):$(OBJFILE)
$(LINK) $^ $(LIBS) -Wall -O2 -o $@
%.o:%.c $(CC) -o $@ $(CCFLAGS) $< $(INCLUDES)
%.o:%.cpp $(CPP) -o $@ $(CPPFLAGS) $< $(INCLUDES)
%.o:%.cc $(CPP) -o $@ $(CCFLAGS) $< $(INCLUDES)
build: make rm -rf $(OBJFILE)
clean: rm -rf $(TARGET) rm -rf $(OBJFILE)
|
main.c
1 2 3 4 5 6 7 8
| #include <stdio.h> int main() { printf("Hello, World! \n"); return 0; }
|
运行