Makefile变量,而不是int [关闭]

问题描述 投票:-1回答:1
你好,我需要makefile变量的帮助。

make run PORT=something //(run server on port something)

这是我的Makefile

SERVER=server CLIENT=client SERVER.c=src/server.c CLIENT.c=src/client.c CFLAGS=-std=gnu99 -Wall -Wextra -pedantic DFLAGS=-DPORT=$(PORT) CC=gcc build: $(CC) $(CFLAGS) $(DFLAGS) -o $(SERVER) $(SERVER.c) $(CC) $(CFLAGS) $(DFLAGS) -o $(CLIENT) $(CLIENT.c) run: $(CLIENT) $(SERVER) ./$(SERVER) "$(PORT)" clean: $(RM) *.o $(CLIENT) $(SERVER)

和C程序

int() { int post_number; post_number = PORT; printf("%d\n", post_number); }

我需要将此变量保存为int并发布到server.c和client.c。
c makefile
1个回答
0
投票
因此,您的main()函数需要看起来像:

main(int argc, char *argv[]) { int post_number; if (argc == 2) { //check there is exactly one argument (plus command name) post_number = atoi(argv[1]); //atoi converts ascii to integer. //argv[1] is a pointer to the first parameter printf("%d\n", post_number); } }

当然,您可以将此数字保存为全局数字,或将其作为参数传递给另一个函数。

© www.soinside.com 2019 - 2024. All rights reserved.