当前位置:网站首页>Record a Makefile just written
Record a Makefile just written
2022-08-11 07:01:00 【Thousands of copies】
Makefile
.PHONY:all
export MY_VAR="who are you"
all:
@echo "hello world"
a1=abc
b1=$(a1)def
a1=gh
test1:
@echo ${b1}
a2:=abc
b2:=$(a2)def
a2:=gh
test2:
@echo ${b2}
VPATH=./ protocol
path:
@echo `pwd`
@echo ${VPATH}
objs:=main.o calc.o server.o client.o pipe.o
objs+=stack.o
include protocol/child.mk
app:${objs}
gcc -o app $^ -pthread -lm
echo ${MY_VAR}
%.o:%.c
gcc -o [email protected] $< -c -DMY_VAR=\"$(MY_VAR)\"
.PHONY:test3
test3:main.c a.c
echo *.c
echo ?.c
echo [12].c
echo [email protected]
echo $<
echo $^
# echo {1 2 main}.c
.PHONY: clean
clean:
rm -f *.o app
=和:=的执行区别
[email protected]:~/learn/makefile/01$ make test1
ghdef
[email protected]:~/learn/makefile/01$ make test2
abcdef
Then there are the contents of the files contained within
main.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
extern int add(int a,int b);
extern int server_init();
extern int client_init();
extern int stack_init();
extern int pipe_init();
extern int protocol_init();
int main(int argc,char *argv[])
{
double my_d = -5;
server_init();
client_init();
stack_init();
pipe_init();
protocol_init();
#ifdef MY_VAR
//在这里获取MakefileThe passed macro
printf("MY_VAR = %s\n",MY_VAR);
#else
printf("no MY_VAR\n");
#endif
printf("hello c\n");
//验证:这里尝试获取MY_VAR,执行结果为空,说明获取不到
printf("%s \n",getenv("MY_VAR"));
printf("5 + 10 =%d c\n",add(5,10));
printf("my_double = %g fabs(my_double) = %g\n",
my_d,fabs(my_d));
return 0;
}
calc.c
#include <stdio.h>
int add(int a,int b)
{
return a + b;
}
server.c
#include <stdio.h>
int server_init(void)
{
printf("server_init\n");
return 0;
}
client.c
#include <stdio.h>
int client_init(void)
{
printf("client_init\n");
return 0;
}
stack.c
#include <stdio.h>
int stack_init(void)
{
printf("stack_init\n");
return 0;
}
pipe.c
#include <stdio.h>
int pipe_init(void)
{
printf("pipe_init\n");
return 0;
}
protocol子目录中的文件
[email protected]:~/learn/makefile/01/protocol$ ls
child.mk protocol.c
protocol.c
#include <stdio.h>
int protocol_init(void)
{
printf("protocol_init\n");
return 0;
}
child.mk:This is in a subdirectoryMakefile
objs += protocol.o
执行结果
先make app
[email protected]:~/learn/makefile/01$ make app
gcc -o main.o main.c -c -DMY_VAR=\""who are you"\"
gcc -o calc.o calc.c -c -DMY_VAR=\""who are you"\"
gcc -o server.o server.c -c -DMY_VAR=\""who are you"\"
gcc -o client.o client.c -c -DMY_VAR=\""who are you"\"
gcc -o pipe.o pipe.c -c -DMY_VAR=\""who are you"\"
gcc -o stack.o stack.c -c -DMY_VAR=\""who are you"\"
gcc -o protocol.o protocol/protocol.c -c -DMY_VAR=\""who are you"\"
gcc -o app main.o calc.o server.o client.o pipe.o stack.o protocol.o -pthread -lm
echo "who are you"
who are you
然后执行app
[email protected]:~/learn/makefile/01$ ./app
server_init
client_init
stack_init
pipe_init
protocol_init
MY_VAR = who are you //This line indicates that the macro has been obtained
hello c
(null) //This line verifiesMakefileenvironment variables and runtime environment variables are different,Runtime environment variables are terminal-dependent,不能搞混
5 + 10 =15 c
my_double = -5 fabs(my_double) = 5
Test automatic variables
This node is used to test some automatic [email protected] $< $^,Look at the test results first
Prepare a few test files first
[email protected]:~/learn/makefile/01$ touch 1.c 2.c 3.c a.c 11.c执行测试
[email protected]:~/learn/makefile/01$ make test3
echo *.c
1.c 11.c 2.c 3.c a.c calc.c client.c main.c pipe.c server.c stack.c
echo ?.c
1.c 2.c 3.c a.c
echo [12].c
1.c 2.c
echo test3 //[email protected] 目标名
test3
echo main.c //$< The first filename of the dependency
main.c
echo main.c a.c //@^ A collection of dependent files
main.c a.c
总结
这个代码,The following issues were verified
1.如何include子目录
2.How to get macro definitions
3.MakefileGlobal variables defined in cannot be equivalent to runtime environment variables,app运行时的环境变量,Depends on the environment variables of the terminal or device it is running on,It can be known from the basic definition.This place cannot be confused.
Why write so much,Certainly not for word count,只是说明,这样写,Just to write test code,正式项目,还是算了吧
边栏推荐
猜你喜欢

ETCD cluster fault emergency recovery - to recover from the snapshot

ETCD cluster fault emergency recovery - local data is available

CLUSTER DAY03( Ceph概述 、 部署Ceph集群 、 Ceph块存储)

window7开启远程桌面功能

SECURITY DAY06 ( iptables firewall, filter table control, extended matching, typical application of nat table)

FusionCompute8.0.0实验(0)CNA及VRM安装(2280v2)

CLUSTER DAY03 (Ceph overview, the deployment of Ceph CLUSTER, Ceph block storage)

web网络安全笔记

vulnhub靶机--6Day_Lab-v1.0.1

本地yum源搭建
随机推荐
2022年全国职业技能大赛网络安全竞赛试题B模块自己解析思路(2)
无胁科技-TVD每日漏洞情报-2022-7-22
windows10安全中心显示“修正未完成”
无胁科技-TVD每日漏洞情报-2022-7-31
2022年全国职业技能大赛网络安全竞赛试题B模块自己解析思路(9)
本地yum源搭建
CLUSTER DAY03( Ceph概述 、 部署Ceph集群 、 Ceph块存储)
无胁科技-TVD每日漏洞情报-2022-7-27
Raspberry Pi set static IP address
Threatless Technology-TVD Daily Vulnerability Intelligence-2022-7-26
ovnif摄像头修改ip
Threatless Technology-TVD Daily Vulnerability Intelligence-2022-7-25
日志收集分析器(ELK)
2022年全国职业技能大赛网络安全竞赛试题B模块自己解析思路(6)
内存调试工具Electric Fence
处理eking.Devos勒索病毒防范解密恢复操作攻略
Solve the problem that port 8080 is occupied
无胁科技-TVD每日漏洞情报-2022-8-1
Deep Learning Matlab Toolbox Code Comments
SECURITY DAY05 (Kali system, scanning and caught, SSH basic protection, service SECURITY)