Martim Correia
Martim Correia

Reputation: 505

passing optional arguments to a makefile

I have a program and i have this make file and im trying to run my program with this makefile, and it compiles well the problem is when i run the program i what to run it like this ./user -n tejo.tecnico.ulisboa.pt -p 58011 with this -n tejo.tecnico.ulisboa.pt or this -p 58011 being optional.

I saw this post Passing arguments to "make run" and im not understanding what im doing wrong in the make run command

So can anyone tell me whats wrong in my makefile?

btw im fairly new at making makefiles and using the command line.

Program:

# 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))
%:
    @:

Upvotes: 2

Views: 5741

Answers (1)

Andreas
Andreas

Reputation: 5301

What you should do is to declare a variable (possibly with default):

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

run: user
    ./user $(USER_OPTIONS)

Then invoke make setting the option from the command line:

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

Upvotes: 6

Related Questions