Tinchu
Tinchu

Reputation: 99

Makefile non-recursive method issues

I am trying to build using Makefile below code structure using non-recursive method as per the guidance I received in

link description

work
├── code
|     | 
|     └── main.h and test.h files here
│     └── main.c and test.c files here
|     └── subdir.mk
|
├── _Build/
│     └── Makefile here

Below is the Makefile

-include ../code/subdir.mk

all : target_file

target_file : ../code/main.o ../code/test.o
    @echo Building ...
    @echo Linking files ...
    gcc -Llib ../code/main.o ../code/test.o -lm -o target_file  

clean:
    rm -rv ../code/*.o

Below is the subdir.mk file

../code/test.o : ../code/test.c ../code/test.h
    @echo Building test.c ...
    gcc -Werror -Wall -c ../code/test.c -o ../code/test.o

../code/main.o : ../code/main.c ../code/main.h ../code/test.h
    @echo Building main.c ...
    gcc -Werror -Wall -c ../code/main.c -o ../code/main.o

The output I am getting while running make command is below

Building test.c ...
gcc -Werror -Wall -c ../code/test.c -o ../code/test.o

There is no error I am getting nor main.o is getting generated. Also in Makefile the linking command is not executed.

Upvotes: 0

Views: 72

Answers (1)

Jens
Jens

Reputation: 72667

That's likely because make by default builds the first target it encounters. Since you include the subdir.mk before your all : target_file line, the first target named in subdir.mk is built, and nothing else. Solution: place the subdir.mk inclusion last, e.g. near the end.

Upvotes: 1

Related Questions