1. 假设我们下面几个c++文件
wherewhen.h wherewhen.c
countdown.h countdown.c 包含了math.h, 需要连接库文件
main.c 主函数, main.c 包含了两个头文件 wherewhen.h and countdown.h
第一种编译方法
生成可执行文件myprogram
第二中编译方法, 分别编译各个文件,
如果我改变了wherewhen.c, 只需要做下面两步编译
2. 采用Makefile
类似上面,有三个文件Student.h,Student.cpp and main.cpp, main.cpp 包含头文件Student.h
then run make
wherewhen.h wherewhen.c
countdown.h countdown.c 包含了math.h, 需要连接库文件
main.c 主函数, main.c 包含了两个头文件 wherewhen.h and countdown.h
第一种编译方法
g++ -Wall -g wherewhen.c countdown.c main.c -lm -o myprogram
生成可执行文件myprogram
第二中编译方法, 分别编译各个文件,
g++ -Wall -g -c wherewhen.c
g++ -Wall -g -c countdown.c
g++ -Wall -g -c main.c
g++ -g wherewhen.o countdown.o main.o -lm -o myprogram
如果我改变了wherewhen.c, 只需要做下面两步编译
g++ -Wall -g -c wherewhen.c
g++ -g wherewhen.o countdown.o main.o -lm -o myprogram
2. 采用Makefile
类似上面,有三个文件Student.h,Student.cpp and main.cpp, main.cpp 包含头文件Student.h
CC = g++
CFLAGS = -Wall -g
LDFLAGS = -lm
all: clean Student.o main.o myprogram
Student.o : Student.h
${CC} ${CFLAGS} -c Student.cpp
main.o : Student.h
${CC} ${CFLAGS} -c main.cpp
myprogram : Student.o main.o
${CC} ${CFLAGS} Student.o main.o ${LDFLAGS} -o myprogram
clean:
rm -rf *.o
rm -rf myprogram
then run make