2

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).

Cœur
  • 37,241
  • 25
  • 195
  • 267
user2732944
  • 75
  • 2
  • 4
  • 10

1 Answers1

1

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?

Community
  • 1
  • 1
yegorich
  • 4,653
  • 3
  • 31
  • 37
  • No i don't know CMake. I have used, but i don't know to create it by own. – user2732944 Sep 04 '13 at 08:48
  • If i make SRCS = $(wildcard .c) $(wildcard */*.c), it's compiling, but,if header files which includes in the file of one sub directory,which is not refrencing to other subdirectory. – user2732944 Sep 04 '13 at 09:22
  • Which means dir/sub1/file1.c(In this file #include"header.h"), this file is present in the dir/sub3/header.h. I have change dir/sub1/file1.c(In this file #include"../sub3/header.h"). It's not working. – user2732944 Sep 04 '13 at 09:27
  • That's strange. It must work. Could you place all your header files in the central include folder (dir/include)? Add -Iinclude at the beginning of your CFLAGS . – yegorich Sep 04 '13 at 10:48