当前位置:网站首页>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,正式项目,还是算了吧

原网站

版权声明
本文为[Thousands of copies]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/223/202208110516571098.html