你好,我需要makefile变量的帮助。
make build //(compiler server)
make run PORT=something //(run server on port something)
我需要保存此变量并将其发布到server.c和client.c,这是我的Makefile
SERVER=server
CLIENT=client
FILES=src/server.c src/client.c
CFLAGS=-std=gnu99 -Wall -Wextra -Werror -pedantic
CC=gcc
build:
$(CC) $(CFLAGS) -o $(SERVER) src/server.c
$(CC) $(CFLAGS) -o $(CLIENT) src/client.c
run:
./server
clean:
$(RM) *.o src/$(CLIENT) src/$(SERVER
SERVER = server
CLIENT = client
SERVER.c = src/server.c
CLIENT.c = src/client.c
CFLAGS = -std=gnu99 -Wall -Wextra -Werror -pedantic
PORT = 9823
DFLAGS = -DPORT=$(PORT)
CC = gcc
all: build
build: $(CLIENT) $(SERVER)
$(CLIENT): $(CLIENT.c)
$(CC) $(CFLAGS) $(DFLAGS) -o $(CLIENT) $(CLIENT.c)
$(SERVER): $(SERVER.c)
$(CC) $(CFLAGS) $(DFLAGS) -o $(SERVER) $(SERVER.c)
run: $(CLIENT) $(SERVER)
./$(SERVER)
clean:
$(RM) *.o $(CLIENT) $(SERVER)
用于客户端和服务器的代码包含这样的代码,最好在公共标头中:
#ifndef PORT #define PORT 1234 #endif
并在需要端口号的地方引用PORT。如果严格来说是运行时决定,那么也许您使用:
SERVER = server CLIENT = client SERVER.c = src/server.c CLIENT.c = src/client.c CFLAGS = -std=gnu99 -Wall -Wextra -Werror -pedantic PORT = 9823 CC = gcc all: build build: $(CLIENT) $(SERVER) $(CLIENT): $(CLIENT.c) $(CC) $(CFLAGS) -o $(CLIENT) $(CLIENT.c) $(SERVER): $(SERVER.c) $(CC) $(CFLAGS) -o $(SERVER) $(SERVER.c) run: $(CLIENT) $(SERVER) ./$(SERVER) -p $(PORT) clean: $(RM) *.o $(CLIENT) $(SERVER)
当然,您还需要告诉客户端连接到给定的端口号。您仍应在某些公共标头中具有客户端和服务器共享的默认端口号。您可能会使用这些解决方案的混合体,您可以在构建过程中定义默认端口号,并在
run
规则中也使用它。