1. 当我们需要在main.cpp文件中导入自定义的头文件test.h时,首先需要在main.cpp中“声明”:
#include "test.h"
2. 然后编写test.h文件。test.h文件只能声明需要的函数,类,变量。为了避免重复导入,需要加上
#ifndef TEST_H
#define TEST_H
......code部分.....
#endif
3. test.cpp文件负责实现test中函数的功能,需要"声明:
#include "test.h"
单个文件编译时直接shell命令即可,当文件过多时,需要借助make命令。编写Makefile文件。makefile文件主要形式如下:
目标文件:该目标文件依赖的文件
由依赖文件生成目标文件的命令(行首tab)
目标文件:该目标文件依赖的文件
由依赖文件生成目标文件的命令(行首tab)
实例如下:
hello: hello.o test.o
g++ -o hello hello.o test.o
hello.o: hello.cpp
g++ -c hello.cpp
test.o: test.cpp
g++ -c test.cpp
clean:
rm -rf *.o