[CMake] Fortran - Can’t find *.mod

Alan W. Irwin irwin at beluga.phys.uvic.ca
Mon Dec 21 15:44:33 EST 2009


On 2009-12-21 20:52+0100 C. Meissa wrote:

> Hello List,
>
> I’m moving my fortran project to cmake…
>
> I have a src directory containing 3 dirs:
> main
> mod1
> mod2
>
> The main program in “main” is to be linked against the libs in “mod1”
> and “mod2”.
>
> Additionally “mod2” does need “mod1”, too. (use mod1).
>
> When compiling gfortran says:
> Fatal Error: Can't open module file 'mod1.mod' for reading at (1): File not
> found

Find out the directory where that mod1.mod file has been generated by the
gfortran build of your mod1 library. I am virtually positive that location
must be the ${CMAKE_BINARY_DIR}/mod1 directory in the build tree. Then use
the correct -I option when building your main programme so that gfortran can
find that location.  That is done with 
"include_directories(${CMAKE_BINARY_DIR}/mod1)" in your top-level
CMakeLists.txt file.

Of course, to be sure your main application is built and linked properly
_after_ your mod1 library is built, you must use the appropriate
target_link_libraries command.  And similarly for the mod1 library
to make sure it is built and linked properly after your mod2 library
is built.

I believe that in order for target_link_libraries to work correctly, CMake must
already have configured the build of the dependent targets with add_library.
So here is the order of processing I would recommend.

# configure the build of mod2 library.
add_subdirectory(mod2)

# configure the build of mod1 library including its dependency on the mod2
# library.
add_subdirectory(mod1)

# configure the build of the main executable, and make sure it depends
# properly on the mod1 library.
include_directories(${CMAKE_BINARY_DIR}/mod1)
add_executable(main_target ....)
target_link_libraries(main_target mod1library_target)

Anyhow, I always use that order rule (configure a target before it is
referred to by other CMake commands), and it works fine.

Alan
__________________________
Alan W. Irwin

Astronomical research affiliation with Department of Physics and Astronomy,
University of Victoria (astrowww.phys.uvic.ca).

Programming affiliations with the FreeEOS equation-of-state implementation
for stellar interiors (freeeos.sf.net); PLplot scientific plotting software
package (plplot.org); the libLASi project (unifont.org/lasi); the Loads of
Linux Links project (loll.sf.net); and the Linux Brochure Project
(lbproject.sf.net).
__________________________

Linux-powered Science
__________________________


More information about the CMake mailing list