I am trying to built two different libraries from fortran source code, one with OMP support, the other without. Thus %.o files from the same source differ dependent on the compiler flags. When changing/recompiling one source file, ar still needs all o-files to rebuild the library. To avoid recompiling all source files I want to store the o-files from un-changed source files in two different directories, one with o-files including omp support, the other with out. I am happy to have to makefiles for that in that directory where the libraries are finally located.

However, I cannot get the library rebuild in a single command because of the vpath variable behaviour. Here is the make file:

   SRC :=
   FORTRAN  = ifort
   OPTSSEQ = -mkl=sequential -DThreadUnSafe -warn nounused -warn declarations -O3 -DTIMEDETAIL
   NOOPT    =
   LOADER   = ifort
   kernel=$(shell uname -r)
   ARCH     = ar
   RANLIB   = ranlib
  .SUFFIXES: .f90 .o
   include Moduls.mk
   vpath %.f90 src/
   vpath %.o NoOMP/
   OBJS = $(patsubst %.f90,%.o,$(SRC))
   $(LibName): $(OBJS)
      $(ARCH) $(ARCHFLAGS) $@ $?
      $(RANLIB) $@
   %.o : %.f90
      $(FORTRAN) $(OPTSSEQ) -c $? -o $(addprefix NoOMP/,$@)
     -rm *.mod
     -rm NoOMP/*.o
     -rm *.smod
     -rm $(LibName)

This results in successful building all %.o files after "make clean", since all %.f90 files are read from src/ and all %.o files are written to NoOMP/. However, building the archive fails because the "NoOMP" prefix on the %.o files is dropped. Thus, ar complains that it cannot find the %.o files. Only if I run "make" again, the archive is build because then %.o files have the NoOMP prefix. Adding a prefix command to the archive build line works fine if it is build from scratch. However, if only a single file has changed, the unchanged files have a "NoOMP/NoOMP/" prefix, resulting in an abort again.

While this is terribly annoying, as far as I understood the manual that is the default behaviour of GNU make. If I am wrong, how can I repair the makefile, if I am right how can I circumvent this problem.

1 Answer 1



the examples work when changing the line

    $(ARCH) $(ARCHFLAGS) $@ $?


    $(ARCH) $(ARCHFLAGS) $@ $(addprefix NoOMP/,$?)

Above I had ruled out this solution, but my trial where based on $^ instead of $?. While the first will rebuilt the whole archive from old and new %.o files, the last will only update the existing archive with new %.o files. When rebuilding the whole archive from old and new %.o files, old %.o files will have the wrong prefix, new the right. Thus excluding old %.o files by updating the archive with newly compiled %.o files circumvents that problem.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .