当前位置:网站首页>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,正式项目,还是算了吧
边栏推荐
- 记录一个刚写的Makefile
- Threatless Technology-TVD Daily Vulnerability Intelligence-2022-7-30
- 查看CPU和其他硬件温度的软件
- Solve win10 installed portal v13 / v15 asked repeatedly to restart problem.
- 升级到Window11体验
- 无胁科技-TVD每日漏洞情报-2022-7-28
- MoreFileRename batch file renaming tool
- (一)软件测试理论(0基础了解基础知识)
- Threatless Technology-TVD Daily Vulnerability Intelligence-2022-7-22
- CLUSTER DAY01 (Introduction to cluster and LVS, LVS-NAT cluster, LVS-DR cluster)
猜你喜欢
随机推荐
中小微企业需要使用SSL证书吗?
windows10安全中心显示“修正未完成”
SECURITY DAY06 ( iptables firewall, filter table control, extended matching, typical application of nat table)
Two hundred questions in C language (0 basic continuous update) (1~5)
ovnif摄像头修改ip
Deep Learning Matlab Toolbox Code Comments
无胁科技-TVD每日漏洞情报-2022-7-30
CLUSTER DAY01(集群及LVS简介 、 LVS-NAT集群 、 LVS-DR集群)
无胁科技-TVD每日漏洞情报-2022-7-26
VMware workstation 16 installation and configuration
uboot设置默认的bootdelay
ETCD cluster fault emergency recovery - to recover from the snapshot
Vulnhub靶机--Chronos
解决win10安装portal v13/v15要求反复重启问题。
SECURITY DAY01 (Monitoring Overview, Zabbix Basics, Zabbix Monitoring Services)
vnc remote desktop installation (available for personal testing on 2021-10-20)
无胁科技-TVD每日漏洞情报-2022-7-19
SECURITY DAY02 (Zabbix alarm mechanism, Zabbix advanced operation and monitoring case)
2022年全国职业技能大赛网络安全竞赛试题B模块自己解析思路(4)
Basic use of Slurm