Makefile - How to save .o one directory up?

Imagine the following folder structure:

  • Project
    • CSI
      • code.c
      • Makefile
    • bin

How can I compile code.c in code.o and directly put it in bin? I know that I can compile it in code.o under src and make "mv code.o ../ bin", but this will lead to an error if there are compilation errors, right? Even if it works this way, is there a better way to do this?

Thank.

+3
source share
4 answers

The process should or should not "give an error" depending on what you mean. If there are compiler errors, you will find out.

However, there are several ways to do this with make. In this case, it is best:

  • Makefile . Make there , .
  • makefile:
    $(MAIN_DIR)/bin/%.o: %.c
        $(COMPILE)...
    
+5

, . , makefile.

$(subst $(notdir $(CURDIR)),,$(CURDIR))

:

~/myProject/
    src/
        Makefile
        #all the .c and .cpp
    bin/
        #where you want to put the binaries.

$(CURDIR) ~/myProject/src

$(subst $(notdir $(CURDIR)), $(CURDIR)) ~/myProject

+2

, , , &&:

code.o: code.c code.h
    g++ -c code.c && mv code.o ../

mv code.o ../ , g++ 0, . , makefile, , , .

0

:

cc -c code.c && mv code.o ../bin

This will not execute the mv part if cc fails.

0
source

Source: https://habr.com/ru/post/1740609/


All Articles