Trying to compile a large program with interdependent modules using the command line
I have a large program comprising of 40 source files. 39 of these source files contain module definitions (only) and 1 of these files contains a main program. The main program references 12 of the module definition source files via `use modulename` syntax. These 12 files in turn reference the other 27 module definitions. All the files are necessary for the functioning of the program.
I have compiled all of the 39 module-definition source files using the following syntax. I also made sure that I compiled lowest level modules first (i.e those that reference no other modules) then higher level modules next (i.e those that reference many other modules).
This resulted in a .mod and .obj file for each source file.
Once all 39 modules were compiled I ensured that corresponding .o and .mod files were generated (which they were). Finally I attempted to compile the main program using the following command:
ifort -free -fpp -DMACRO1 -DMACRO2 MAIN.FOR
Unfortunately this results in a stream of almost 100 errors of the following kind
DVF_UNIT_VS.obj : error LNK2019: unresolved external symbol MODNAME_mp_VARIABLENAME referenced in function MAIN__
DVF_UNIT_VS.exe : fatal error LNK1120: 99 unresolved externals
The command does generate an object file but no executable. The question is why can the compiler not recognize those variables in modules for which the .mod and .obj files were successfully generated?
I also have a version of this program that compiles successfully in Visual Studio. This version generates additional .mod files with a _genmod.mod suffix. I'm not sure what these do but I do not see them generated in my command line variant. I hope this has nothing to do with the problem I am experiencing? In any case it is important for me to be able to compile this using the command line and not Visual Studio.
The compiler recognizes the .mod files just fine. But because you omitted -c, the linker is invoked. Referencing a module does not cause the object files to be automatically included in the link step - you have to do that yourself. The usual way to do this with a lot of modules is to insert the object files into a library and then reference that on the linking command line.
Steve (aka "Doctor Fortran") - https://stevelionel.com/drfortran