一 编译需要的文件
1 file1.h
#ifndef FILE1_H_
#define FILE1_H_
#ifdef __cplusplus
extern "C" {
#endif
void File1Print();
#ifdef __cplusplus
}
#endif
#endif
2 file1.cpp
#include <iostream>
#include "file1.h"
using namespace std;
void File1Print(){
cout<<"Print file1**********************"<<endl;
}
3 file2.cpp
#include <iostream>
#include "file1.h"
using namespace std;
int main(){
cout<<"Print file2**********************"<<endl;
File1Print();
return 0;
}
二 在makefile中使用变量
1 makefile
OBJS = file1.o file2.o
XX = g++
CFLAGS = -Wall -O -g
helloworld : $(OBJS)
$(XX) $(OBJS) -o helloworld
file2.o : file2.cpp file1.h
$(XX) $(CFLAGS) -c file2.cpp -o file2.o
file1.o : file1.cpp file1.h
$(XX) $(CFLAGS) -c file1.cpp -o file1.o
clean:
rm -rf *.o helloworld
2 编译
[root@localhost 0404]# ll
total 16
-rw-r--r--. 1 root root 140 May 1 08:52 file1.cpp
-rw-r--r--. 1 root root 170 May 1 08:52 file1.h
-rw-r--r--. 1 root root 167 May 1 08:52 file2.cpp
-rw-r--r--. 1 root root 290 May 1 08:52 Makefile
[root@localhost 0404]# make
g++ -Wall -O -g -c file1.cpp -o file1.o
g++ -Wall -O -g -c file2.cpp -o file2.o
g++ file1.o file2.o -o helloworld
[root@localhost 0404]# ll
total 100
-rw-r--r--. 1 root root 140 May 1 08:52 file1.cpp
-rw-r--r--. 1 root root 170 May 1 08:52 file1.h
-rw-r--r--. 1 root root 26488 May 10 21:58 file1.o
-rw-r--r--. 1 root root 167 May 1 08:52 file2.cpp
-rw-r--r--. 1 root root 24136 May 10 21:58 file2.o
-rwxr-xr-x. 1 root root 31520 May 10 21:58 helloworld
-rw-r--r--. 1 root root 290 May 1 08:52 Makefile
3 说明
要设定一个变量,只要在一行的前端写下这个变量的名字,后面跟一个"="号,后面跟要设定的这个值即可。以后要引用这个变量,只写一个"$"符合,后面在括号中写上变量名。
-Wall:输出所有的警告信息。
-O :编译时进行优化。
-g:表示编译debug版本