[CMake] fortran modules and parallel builds

Brad King brad.king at kitware.com
Wed Feb 25 16:22:56 EST 2015


On 02/25/2015 01:58 PM, Zaak Beekman wrote:
> When multiple executables or libraries depend on the same
> fortran source file that contains a module, parallel (Makefile)
> builds are failing for me because the .mod file is getting
> moved/renamed/written by more than one process at a time. Is
> there a way to have the same module containing sourcefile
> listed as one of the sources for multiple executable and/or
> library targets without this happening? Right now my work
> around/hack is change the Fortran_MODULE_DIRECTORY property to
> something unique for each target, but this is a pain, and very
> sloppy.

If a source is listed in multiple targets then it is compiled
separately for each target.  If that source provides a module
then one *must* use a distinct Fortran_MODULE_DIRECTORY for
each one or it not well-defined what provides the .mod file,
hence your conflict.

If you really only want to compile the source once but use it
from multiple executables then you should put it in a static
library instead:

 add_library(mymod STATIC mymod.F90)
 add_executable(myexe1 myexe1.F90)
 target_link_libraries(myexe1 mymod)
 add_executable(myexe2 myexe2.F90)
 target_link_libraries(myexe1 mymod)

Then there will be only one compilation of the module-providing
source.  CMake will hook up the dependencies so that the exe
sources do not compile before the module is made available
by compiling the library.

-Brad



More information about the CMake mailing list