0

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))
%:
    @:
Martim Correia
  • 483
  • 5
  • 16
  • What make run command are you using? Did you try `MAKECMDGOALS="-n tejo.tecnico.ulisboa.pt -p 58011" make run` ? – Galik Jan 09 '22 at 16:41
  • That's an extremely obtuse way of passing arguments. Just do `args :=` `run: user` `./user $(args)`, then run `make run args='foo bar'` to run with args, or just `make run` to run without args. – HolyBlackCat Jan 09 '22 at 16:45
  • You definitely don't want to set `MAKECMDGOALS` on the command line or in the environment: that's a GNU make variable. I agree that there's no way to answer this question since it doesn't contain any example of the actual invocation of `make`, or the output or errors received. – MadScientist Jan 09 '22 at 16:48
  • 3
    In general I recommend against using a make target to _run_ your program. Make is intended for _building_ your program. Maybe if you have a specific test case you want to invoke and have some hardcoded options for that test case, it makes sense. But why is it better to type `make run args="foo bar"`, than to just type `./user foo bar`?? – MadScientist Jan 09 '22 at 16:49

1 Answers1

4

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"
Andreas
  • 5,086
  • 3
  • 16
  • 36