user2732944
user2732944

Reputation: 85

Using vpath and wild card in Makefile

1) I am having directory called dir, and the subdirectories are sub1, sub2, sub3.

2) I want to create a Makefile which will compile all the sources in the subdirectory. These are some lines of my makefile.

CFLAGS = -I/usr/include/ -I./ -ansi -g
GPROF_CFLAGS = -I/usr/include/ -I./
VPATH = ./sub1 ./sub2 ./sub3
SRCS := $(wildcard *.c)
OBJS=$(SRCS:.c=.o)
exe:    $(OBJS)
        $(CC) $(LFLAG) -o exe $(CFLAGS) $(OBJS) -lm 

exe_gprof:
        gcc $(LFLAG) -o exe $(GPROF_SUFFIX) $(GPROF_CFLAGS) $(SRCS)

clean:
        rm -f *.o

clean_all: clean
        rm -f exe exe$(GPROF_SUFFIX) gmon.out Simfir000.stat rm -f *.o.

3) This make file works fine, if i have only sub folder called sub(this directory contains all source files) and no other sub folder. (i.e) in the make file vpath=./sub.

4) If there are sub folders as I mentioned in point 1, the main file is in the subfolder of sub1/src/main.c(i.e sub folder inside the sub folder). If I try to compile. It's giving undefined ref to main.

5) what change I have to do, to compile this successfully. Can we give more than one directory path in vpath?.

Overall map is:

Having directory called dir

sub directory dir/sub1

dir/sub2

dir/sub3

main file is in dir/sub1/src/main.c

My second question is
If I want to execute directory by directory and create different exe's

compiling sub1 only and create sub1exe

copiling sub2 only and create sub2exe.

The make file will be

CFLAGS = -I/usr/include/ -I./ -ansi -g
GPROF_CFLAGS = -I/usr/include/ -I./
VPATH = ./sub1 ./sub2 ./sub3
SRCS1 := $(wildcard *.c)
SRCS2 := $(wildcard *.c)
OBJS=$(SRCS:.c=.o)
exe1:    $(OBJS)
        $(CC) $(LFLAG) -o exe1 $(CFLAGS) $(OBJS) -lm 
exe2:    $(OBJS)
        $(CC) $(LFLAG) -o exe2 $(CFLAGS) $(OBJS) -lm 

exe1_gprof:
        gcc $(LFLAG) -o exe1 $(GPROF_SUFFIX) $(GPROF_CFLAGS) $(SRCS1)
exe2_gprof:
        gcc $(LFLAG) -o exe2 $(GPROF_SUFFIX) $(GPROF_CFLAGS) $(SRCS2)

clean:
        rm -f *.o

clean_all: clean

What changes I have to make instead of $(wildcard *.c).

Upvotes: 2

Views: 3092

Answers (1)

yegorich
yegorich

Reputation: 4849

This post seems to answer your question: Sources from subdirectories in Makefile

Try SRCS = $(wildcard *.c) $(wildcard **/*.c)

I think VPATH and wildcard are not working together, so that your SRCS doesn't have all source files.

By the way, have you already tried CMake?

Upvotes: 1

Related Questions