ubuntu 下makefile的gcc编译学习笔记(一)

根据这个博客按部就班的学习:https://blog.csdn.net/tototuzuoquan/article/details/38459119

1.入门级的makefile:[包含三个文件:test1.h、test1.cpp、main.cpp、makefile;输出编译后的文件:app、myapp]

不使用makefile的时候:

hualulu

使用makefile编译:

start:
		g++ -o main.o -c main.cpp
		g++ -o test1.o -c test1.cpp
		g++ -o myapp test1.o main.o

clean:
		rm -rf main.o test1.o

2.在makefile中定义变量:

CC=g++          //定义变量
start:
		$(CC) -o main.o -c main.cpp
		$(CC) -o test1.o -c test1.cpp
		$(CC) -o myapp test1.o main.o

clean:
		rm -rf main.o test1.o

3.编写makefile的依赖:

start:后面接.o标识,如果文件中没有所需的依赖.o文件,则先重新编译生成没有编译的那个文件。

CC=g++
start:test1.o main.o
		$(CC) -o myapp test1.o main.o
main.o:
		$(CC) -o main.o -c main.cpp
test1.o:
		$(CC) -o test1.o -c test1.cpp

clean:
		rm -rf main.o test1.o

4.最终的makefile文件:

CC=g++
//表示使用g++
SRCS=main.cpp\
	test1.cpp
//表示项目中的源文件,可以后续添加
OBJS=$(SRCS:.cpp=.o)
//.cpp文件对生成的.o文件
EXEC=myapp
//生成执行文件myapp
//以上定义变量。
start:$(OBJS)
		$(CC) -o $(EXEC) $(OBJS)
//等价于:
//$(CC) -o myapp main.o test1.o
.CPP.o:
		$(CC) -o $@ -c $<
//等价于:
//main.o:
//        $(CC) -o main.cpp -c main.o
//test1.o:
//        $(CC) -o test1.cpp -c test1.o
clean:
		rm -rf $(OBJS)
//等价于:rm -rf main.o test1.o

5.makefile不管理.h文件,编译器来管理.h文件

7.vi指令:

i在光标之前插入

a在光标后面插入

x删除后面的字符

dd删除整行

猜你喜欢

转载自blog.csdn.net/weixin_38715903/article/details/84716096
今日推荐