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。
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);
}
}
当然,您可以将此数字保存为全局数字,或将其作为参数传递给另一个函数。