0

I try to compile a project with generated object stored in a sub directory :

myproject:
|
src: .cpp, .cpp ...
  |
  release: .o, .o ...

Here's a part of Makefile:

SRC     ?= src
OBJ_PATH = $(SRC)/Release
vpath %.o $(OBJ_PATH)

...

OBJS := $(addprefix $(OBJ_PATH)/,obj1.o obj2.o )

all: build

obj1.o: $(SRC)/Manager.cpp
$(EXEC) $(CC) $(INCLUDES) $(CCFLAGS) $(GNCD_FLGS) -c $(<) -o $(OBJ_PATH)/$@ #-o $@ -c $<

obj2.o: $(SRC)/Synth.cpp
$(EXEC) $(CC) $(INCLUDES) $(CCFLAGS) $(GNCD_FLGS) $(DEFS) -c $(<) -o $(OBJ_PATH)/$@ #-o $@ -c $<


myApp:  obj1.o obj2.o
$(EXEC) $(CC) $(LDFLAGS) $(GNCD_FLGS) -o $@ $(OBJS) $+ $(LIBS)
$(EXEC) mkdir -p $(OBJ_PATH)/$(TRGT_ARCH)/$(TRGT_OS)/$(BLD_TP)
$(EXEC) cp $@ $(OBJ_PATH)/$(TRGT_ARCH)/$(TRGT_OS)/$(BLD_TP)

$(OBJECTS) : Stt.h

build: myApp

run: build
$(EXEC) ./myApp

..but i got an error link:

Could not open input file 'obj1.o' 
Makefile:86: recipe for target 'myApp' failed

So it seems couldn't find object in src/Release dir;

any ideas ?

thank's

spin0
  • 21
  • 4
  • Is this a duplicate of http://stackoverflow.com/questions/13552575/gnu-make-pattern-to-build-output-in-different-directory-than-src ? – Alexey Semenyuk Feb 14 '17 at 18:33
  • Yes, it's sounds interesting but in my case i need to create objects from project source and also an external lib. So i'm not sure to get this syntax to be able to link both objects .. – spin0 Feb 21 '17 at 03:49

1 Answers1

0

Your recipe for myApp use $+, which list the prerequisites. It expands in obj1.o obj2.o. But you build obj1.o and obj2.o in $(OBJ_PATH). So the linker try to find the objects in the root directory, but cannot find them, since they are in $(OBJ_PATH). Since your recipe explicitely lists them (with $(OBJS)), you do not need the automatic variable.

Sidenote

According to Paul's Second rule of Makefiles, it is best that every rule updates a file corresponding exactly to the target name (including the path part) (in other words, always use $@ in the recipe), in order to always know which is the exact file updated. In your case, if you want to build the objects files in OBJ_PATH, you could use a rule of the form $(OBJ_PATH)/obj.o for each. You could also replace the dependency of myApp by $(OBJS), and use the automatic variable (btw, is there a reason why you prefer $+ over $^ (does the same thing but do not conserv duplicates in the prerequisites list) ?).

Community
  • 1
  • 1
VannTen
  • 441
  • 2
  • 12
  • Hi VannTen, i modified like $(OBJ_PATH)/obj1.o: $(SRC)/Manager.cpp ...and for myApp, $(EXEC) cp $@ $(OBJ_PATH)/$(TRGT_ARCH) become $(EXEC) cp $@ $(TRGT_ARCH)/.. Thank's for your explanations :) – spin0 Feb 21 '17 at 04:38