将可选参数传递给 makefile

passing optional arguments to a makefile

我有一个程序,我有这个 make 文件,我正在尝试 运行 我的程序和这个 makefile,它编译得很好问题是当我 运行 这个程序时我要做什么 运行 它像这样 ./user -n tejo.tecnico.ulisboa.pt -p 58011 和这个 -n tejo.tecnico.ulisboa.pt 或这个 -p 58011 是可选的。

我看到了这个 post Passing arguments to "make run" 但我不明白我在 make 运行 命令中做错了什么

谁能告诉我我的 makefile 有什么问题?

顺便说一句,我在制作 makefile 和使用命令行方面相当陌生。

节目:

# Makefile
CC   = g++
LD   = g++

AUXLIB = auxiliar_code/aux_functions.h
SOCKETLIB = socket_operations/socket_functs.h
COMMANDSLIB = commands/commands.h

.PHONY: all clean run

all: client

client: socket_operations/socket.o auxiliar_code/aux.o commands/commands.o commands/client.o main.o
    $(LD) -o user socket_operations/socket.o auxiliar_code/aux.o commands/commands.o commands/client.o main.o

auxiliar_code/aux.o: auxiliar_code/aux_functions.cpp $(AUXLIB) client_constants.h
    $(CC) -o auxiliar_code/aux.o -c auxiliar_code/aux_functions.cpp

commands/client.o: commands/Client.cpp $(COMMANDSLIB) $(SOCKETLIB) $(AUXLIB) client_constants.h
    $(CC) -o commands/client.o -c commands/Client.cpp

commands/commands.o: commands/commands.cpp $(COMMANDSLIB) $(SOCKETLIB) $(AUXLIB) client_constants.h
    $(CC) -o commands/commands.o -c commands/commands.cpp

socket_operations/socket.o: socket_operations/socket_functs.cpp $(SOCKETLIB) $(AUXLIB) client_constants.h
    $(CC) -o socket_operations/socket.o -c socket_operations/socket_functs.cpp

main.o: main.cpp $(COMMANDSLIB) $(AUXLIB) client_constants.h
    $(CC) $(CFLAGS) -o main.o -c main.cpp

clean:
    @echo Cleaning files generated
    rm -f auxiliar_code/*.o commands/*.o socket_operations/*.o *.o user

run: user
    @echo ./user $(filter-out $@,$(MAKECMDGOALS))
%:
    @:

你应该做的是声明一个变量(可能是默认的):

# Fill in your default here, setting from command line will override
USER_OPTIONS ?=

run: user
    ./user $(USER_OPTIONS)

然后从命令行调用 make 设置选项:

make run USER_OPTIONS="-n tejo.tecnico.ulisboa.pt -p 58011"