Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Makefile - How to save the .o one directory up?

Tags:

makefile

Imagine the following folder structure:

  • project
    • src
      • code.c
      • makefile
    • bin

How can I compile code.c to code.o and directly put it inside bin? I know I could compile it to code.o under src and the do "mv code.o ../bin" but that would yield an error if there were compile errors, right? Even if it works that way, is there a better way to do it?

Thanks.

like image 208
nunos Avatar asked Jan 23 '23 06:01

nunos


1 Answers

The process should or should not "yield an error" depending on what you mean. If there are compiler errors, you'll know it.

That said, there are several ways to do it with make. The best in this case are probably:

  • You could put the Makefile in bin. Make is good at using files there to make files here, but not the other way around.
  • You could specify the target path in the makefile target:
    $(MAIN_DIR)/bin/%.o: %.c
        $(COMPILE)...
    
like image 94
Beta Avatar answered Jan 24 '23 20:01

Beta