0

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!

Community
  • 1
  • 1
Charles
  • 947
  • 1
  • 15
  • 39

1 Answers1

1

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.

casey
  • 6,855
  • 1
  • 24
  • 37
  • It looks like this gets me halfway there, when I try using this, the object files are compiled, but then I get an error saying "error: user/gridFun2.o: no such file or directory". Is there a way I can fix this also? @casey – Charles Jan 23 '15 at 21:04
  • This, along with OBJS_T = $(patsubst %.f90,%.o,$(SRCS_F)) OBJS_F = $(notdir $(OBJS_T)) produces the result I'm looking for. Thanks again! – Charles Jan 23 '15 at 22:21