makefile中使用变量

时间:2022-12-19 12:53:17

                  直接看代码:

[xxx@localhost makefile]$ ls
makefile test1.cpp test2.cpp
[xxx@localhost makefile]$ cat makefile
all:test1 test2
XX=g++
CFLAGS=-Wall -O -g


test1:test1.o
$(XX) test1.o -o test1
test2:test2.o
$(XX) test2.o -o test2
test1.o:test1.cpp
$(XX) $(CFLAGS) -c test1.cpp -o test1.o
test2.o:test2.cpp
$(XX) $(CFLAGS) -c test2.cpp -o test2.o

clean:
rm -fr *.o
[xxx@localhost makefile]$ cat test1.cpp
#include<stdio.h>

int main()
{
printf("this is test1\n");

return 0;
}
[xxx@localhost makefile]$ cat test2.cpp
#include<stdio.h>

int main()
{
printf("this is test2\n");

return 0;
}
[xxx@localhost makefile]$

执行make:

[xxx@localhost makefile]$ make
g++ -Wall -O -g -c test1.cpp -o test1.o
g++ test1.o -o test1
g++ -Wall -O -g -c test2.cpp -o test2.o
g++ test2.o -o test2
[xxx@localhost makefile]$ ls
makefile test1 test1.cpp test1.o test2 test2.cpp test2.o
[xxx@localhost makefile]$ ./test1
this is test1
[xxx@localhost makefile]$ ./test2
this is test2
[xxx@localhost makefile]$ make clean
rm -fr *.o
[xxx@localhost makefile]$
-Wall表示输出所以警告信息,-O表示编译时进行优化,-g表示编译debug版本