[CMake] Fortran MinGW on Windows

Leek, Jim leek2 at llnl.gov
Fri May 31 03:51:34 EDT 2013


Hi, I recently rebuilt an old build system with cmake.  It works well on Linux, but part of the idea of using cmake was to make it more portable.  Now I'm working on the Windows port, but I'm having issues with the Fortran libraries.  The program itself is C++, but it has a number of optional third party libraries, a few of which are written in Fortran.  The weird thing is, when I do the final link, I get a bunch of "undefined references" for stuff that's in the gfortran library.

Here are the details, using the optional MARS Fortran library as an example:

In the top level CMakeLists.txt:
option (USE_MARS
        "Build and use the MARS library." OFF)
if (USE_MARS)
  include_directories ("${PROJECT_SOURCE_DIR}/External/MARS")
  add_subdirectory (External/MARS)
  set (EXTRA_LIBS ${EXTRA_LIBS} mars)
  add_definitions(-DHAVE_MARS)
endif (USE_MARS)

and the External/MARS/CMakeLists.txt:
enable_language(Fortran)
add_library(mars ${LIBRARY_TYPE} mars36_fort.f)
install (TARGETS mars DESTINATION lib)

At build time, it all goes fine until the final link of the main executable (psuade):
Linking CXX executable psuade.exe
External\MARS\libmars.a(mars36_fort.f.obj):mars36_fort.f:(.text+0x21fce): undefined reference to `_gfortran_transfer_integer_write'

However, if I throw -lgfortran on the end, it works fine:
C:\MinGW\bin\g++.exe  -mfpmath=sse -msse2    -Wl,--whole-archive CMakeFiles\psuade-bin.dir/objects.a -Wl,--no-whole-archive  -o psuade.exe -Wl,--out-implib,libpsuade.dll.a -Wl,--major-image-version,0,--minor-image-version,0  libpsuade.a C:\MinGW\bin\liblapack.dll C:\MinGW\bin\libblas.dll External\Bose\libbose.a External\MARS\libmars.a -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 -lgfortran

What's going on here?  I guess I could have an "if(windows) add_library(gfortran)" or some such, but that seems a little hacky, and this works properly on Linux, even though Linux doesn't explicitly link to libgfortran either.
I'm using gfortran 4.7.2 on both Windows and Linux.

Oh, I read about CMakeAddFortranSubdirectory, but the example linked to at http://www.cmake.org/files/lapack_test.tar.gz seems to have disappeared.

Thanks,
Jim
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.cmake.org/pipermail/cmake/attachments/20130531/0f9ec8ac/attachment-0001.htm>


More information about the CMake mailing list