[CMake] Fortran MinGW on Windows

Brad King brad.king at kitware.com
Fri May 31 14:43:17 EDT 2013


On 05/31/2013 01:35 PM, Alain Leblanc wrote:
> What happens when enable_language(Fortran) is invoked and CMake
> can not find the gfortran shared libraries?

CMake does not actually search for the libraries.  It runs the Fortran
compiler front-end to build a test executable and passes options to
ask the compiler to print out the link line it generates to invoke the
linker.  From that CMake extract the -L and -l options needed for
Fortran.  It never actually searches for the library files.

The automatic use of the cross-language linking feature only works
when CMake knows there is Fortran in one of the libraries because
it is a library built within the same project (or imported from
a CMake-built project that uses install(EXPORT)).

> I'm asking because I'm using a couple of Fortran programs in a big
> C++ project, and I don't use enable_language() but just use
> find_library(GFORTRAN gfortran ...)
> ...
> target_link_library(myProj $(GFORTRAN) ...)

If you're not actually compiling any Fortran then this is a fine
approach for a platform-specific dependency.  As far as CMake knows
gfortran is just another library in this case.

> create a symbolic link from libgfortran.so.3 to libgfortran.so
> in order for the find_library() line to work.

The .so.3 version will not be picked up by the linker with "-lgfortran".
This is a common convention for libraries on Linux, and many packages
have a separate -dev version that adds the .so symlink.  However, in
the case of the Fortran runtime library the .so may never be directly
published in a system library directory but rather in an internal
compiler directory.  CMake would only know about that directory after
enabling Fortran in the project.

-Brad


More information about the CMake mailing list