gmajal
gmajal

Reputation: 71

Makefile confusion with Fortran and C

This is my first time making a make file. I'm a little confused since my Fortran code uses some functions defined in C source files. This is what I've written so far:

      CC = icc
      FC = ifort
      FCFLAGS = -O3 -xHost -fno-alias$(INCLUDES)
      CFLAGS = -O3 -xHost -fno-alias$(INCLUDES)
      LIBS =
      INCLUDES =
      TARGET = run
      OBJS: pi.o\
      timing.o
      timing.o: timing.c timing.h 
    $(CC) -c $(CFLAGS) timing.c
             pi.o:  pi.f90 timing.c timing.h 
         $(FC) -c $(FCFLAGS) timing.o pi.f90
            .PHONY : clean
             clean: rm -f *.o

Am I on the right track?

Upvotes: 1

Views: 1714

Answers (2)

MadScientist
MadScientist

Reputation: 100836

You have a number of problems with your makefile, plus you cannot include a .o file when compiling a different .o file. .o files are only sent to the linker, but using the -c flag to ifort tells it to create an object and not link it. BTW, it will help us understand your question if you format the example properly: your whitespace is very odd making things hard to read.

I think you want something like this; this builds a program named "run".

CC = icc
FC = ifort
FCFLAGS = -O3 -xHost -fno-alias $(INCLUDES)
CFLAGS = -O3 -xHost -fno-alias $(INCLUDES)
LIBS =
INCLUDES =
TARGET = run
OBJS = pi.o timing.o

$(TARGET) : $(OBJS)
        $(FC) -o $(TARGET) $(OBJS)
timing.o: timing.c timing.h 
        $(CC) -c $(CFLAGS) timing.c
pi.o: pi.f90 
        $(FC) -c $(FCFLAGS) pi.f90
.PHONY : clean
clean:
        rm -f *.o

Or, if you want to use some more fancy features of make and avoid typing, you can use:

CC = icc
FC = ifort
FCFLAGS = -O3 -xHost -fno-alias $(INCLUDES)
CFLAGS = -O3 -xHost -fno-alias $(INCLUDES)
LIBS =
INCLUDES =
TARGET = run
SRCS = pi.f90 timing.c

timing.o: timing.h

OBJS = $(addsuffix .o,$(basename $(SRCS))

$(TARGET) : $(OBJS)
        $(FC) -o $@ $^
%.o: %.c
        $(CC) -c $(CFLAGS) -o $@ $<
%.o: %.f90 
        $(FC) -c $(FCFLAGS) -o $@ $<

.PHONY : clean
clean:
        rm -f *.o

This looks longer, but if/when you need to add more source files all you have to do is put them into the SRCS variable (and declare any extra prerequisites).

Upvotes: 2

Greg
Greg

Reputation: 19104

pi.o requires timing.o, not timing.c timing.h, swap the two in your fortran recipe. Should look like this:

pi.o: pi.f90 timing.o 
        $(FC) -c $(FCFLAGS) timing.o pi.f90

Upvotes: 0

Related Questions