Charles
Charles

Reputation: 987

gmake compile and link source files in different directories

I'm trying to compile and link several files in different folders using gfortran, and GNU Make 3.81 on a windows machine. I learned how to use wildcards from this reference:

gmake compile all files in a directory

And I want to do something similar to this reference:

Makefiles with source files in different directories

But the difference is that I want to build only one executable in my root directory from the source files in several other directories. I tried reading the make manual:

http://www.gnu.org/software/make/manual/make.html

But it seems primarily directed towards c/c++ programming and non-windows syntax.

My current makefile looks like this:

FC      = gfortran
MOD_DIR = "bin"

FCFLAGS = -O0 -Og -Wall -pedantic -fbacktrace -fcheck=all
FCFLAGS += -J$(MOD_DIR) -fopenmp -fimplicit-none -Wuninitialized

TARGET = test
SRCS_C = $(wildcard *.f90) $(TARGET).f90
OBJS_C = $(patsubst %.f90,%.o,$(SRCS_C))


all: $(TARGET)

$(TARGET): $(OBJS_C)
    $(FC) -o $@ $(FCFLAGS)  $(OBJS_C)

$(OBJS_C): $(SRCS_C)
    $(FC) $(FCFLAGS) -c $(SRCS_C)

clean:
    del *.o $(MOD_DIR)\*.mod

Which works fine when all of my source files are in the root directory. And so I thought this would work:

FC      = gfortran
MOD_DIR = "bin"

FCFLAGS = -O0 -Og -Wall -pedantic -fbacktrace -fcheck=all

# FCFLAGS += -J$(MOD_DIR) -I$(INCLUDE_DIR) -fopenmp -fimplicit-none -Wuninitialized
FCFLAGS += -J$(MOD_DIR) -fopenmp -fimplicit-none -Wuninitialized

TARGET = test

SRCS_C =\
"solvers/"$(wildcard *.f90) \
"user/"$(wildcard *.f90) \
$(wildcard *.f90) $(TARGET).f90

OBJS_C = $(patsubst %.f90,%.o,$(SRCS_C))

all: $(TARGET)

$(TARGET): $(OBJS_C)
    $(FC) -o $@ $(FCFLAGS)  $(OBJS_C)

$(OBJS_C): $(SRCS_C)
    $(FC) $(FCFLAGS) -c $(SRCS_C)

clean:
    del *.o $(MOD_DIR)\*.mod

Where I don't mind just entering the names of the folders where a list of source files can be taken from. I've also tried using -I$(INCLUDE_DIR), but this didn't work either. The error from what I have above is:

gmake: *** No rule to make target `"user/"gridFun.f90', needed by `"user/"gridFu
n.o'.  Stop.

Any help is greatly appreciated!

Upvotes: 0

Views: 299

Answers (1)

casey
casey

Reputation: 6915

To accomplish what you want with SRCS_C, consider using:

SRCS_C =\
$(wildcard solvers/*.f90) \
$(wildcard user/*.f90) \
$(wildcard *.f90) $(TARGET).f90

Also note that (TARGET).f90 will also be matched by $(wildcard *.f90), in your cases causing test.f90 to be included twice in SRCS_C. You can safely omit $(TARGET).f90 in this example.

Upvotes: 1

Related Questions