当前位置:网站首页>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,正式项目,还是算了吧
边栏推荐
- Project Notes - Random 2
- Threatless Technology-TVD Daily Vulnerability Intelligence-2022-7-28
- Vulnhub靶机--DC8
- Login error in mysql: ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)ERROR
- SSL证书为什么要选付费?
- SECURITY DAY04( Prometheus服务器 、 Prometheus被监控端 、 Grafana 、 监控数据库)
- 2022年全国职业技能大赛网络安全竞赛试题B模块自己解析思路(2)
- VMware workstation 16 安装与配置
- China Mobile Communications Group Co., Ltd.: Business Power of Attorney
- ETCD容器化搭建集群
猜你喜欢
lvm multi-disk mount, combined use
文本三剑客——awk 截取+过滤+统计
mongo-express 远程代码执行漏洞复现
Raspberry Pi set static IP address
mysql数据库安装教程(超级超级详细)
Drupal 远程代码执行漏洞复现(CVE-2018-7602)
SECURITY DAY04 (Prometheus server, Prometheus monitored terminal, Grafana, monitoring database)
中小微企业需要使用SSL证书吗?
Vulnhub靶机--born2root
AUTOMATION DAY07 (Ansible Vault, ordinary users use ansible)
随机推荐
本地yum源搭建
AUTOMATION DAY07( Ansible Vault 、 普通用户使用ansible)
IP证书申请
mysql 中登录报错:ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)ERROR
项目笔记——随机2
无胁科技-TVD每日漏洞情报-2022-8-7
Threatless Technology-TVD Daily Vulnerability Intelligence-2022-8-5
CLUSTER DAY03 (Ceph overview, the deployment of Ceph CLUSTER, Ceph block storage)
实操指南:多个域名该买哪种SSL证书?
2022年全国职业技能大赛网络安全竞赛试题B模块自己解析思路(5)
China Mobile Communications Group Co., Ltd.: Business Power of Attorney
Project Notes - Random 2
Threatless Technology-TVD Daily Vulnerability Intelligence-2022-7-31
文本三剑客——grep过滤
2022年全国职业技能大赛网络安全竞赛试题B模块自己解析思路(4)
Threatless Technology-TVD Daily Vulnerability Intelligence-2022-7-20
uboot设置默认的bootdelay
网络安全学习小结--kali基本工具、webshell、代码审计
胖鸟博客之勒索病毒紧急处理文件恢复及防范方案
lvm multi-disk mount, combined use