Linking errors while recompiling

I have a directory containing about 400 .f90 files out of which about 30 are actually modules. So when I create the executable I create the 400 .o files (and the corresponding 30 .mod files get generated) and link them all up to produce the executable. I have been doing this without any issues so far.

Many times I need to change the source code (removal of files, addition of files and change of .F90) and recompile the executable. Instead of recreating all the .o files I decided to do the following:

  • For files which get removed or changed, I remove the .o and .mod file (if exist)
  • For newer files dont do anything

After this I recompile the whole executable. This way I get to use a lot of the previous run’s .o files.

I cannot rely on timestamps of .f90 files (they are actually links to the real .f90 files) but the .o and .mod and regular files and so I can rely on their timestamps.

This has been working well for a couple of weeks, when I found one example where I get linking errors (when I do the above mentioned procedure). If I delete all the .o and .mod files and recompile, then things work. If I delete all the .mod files and the corresponding .o files, then also the things work.

If I just delete the .o and .mod files of changed/removed files, the recompiling produces a linker warning:

“alignment 8 of symbol ‘X’ in file1.o is smaller than 16 in file2.o”

If I run the executable anyway, I get array bounds errors.

Can somebody explain why these errors occur (something to do with .mod files I guess)? More specifically, is there any way I cam improve my logic so that I only require to recompile new files and files which have changed?

Any pointers will be appreciated.

Hi gmkrishn,

What’s most likely happening is when you modify one of your modules not all of the objects that use this module are being rebuilt. To fix, either rebuilt from scratch or use dependancies in your makefile (I’m assuming your using make), which will force the rebuild of the dependent objects.

file1.o:  mod_file1.o $(SRC)/file2.f90
     $(FC) $(FFLAGS) -o file1.o $(SRC)/file1.f90

file2.o:  mod_file1.o $(SRC)/file2.f90
     $(FC) $(FFLAGS) -o file2.o $(SRC)/file2.f90

mod_file1.o:  $(SRC)/mod_file1.f90
     $(FC) $(FFLAGS) -o mod_file1.o $(SRC)/mod_file1.f90

In this example, if mod_file1 gets re-built, so will file1 and file2. Also, consider setting “SRC” to directory which contains the actual files and remove the links. This way you’ll be able to rely on the source file’s timestamp and don’t need to worry about which objects need to be removed when you make a change.

Hope this helps,