<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<meta name="Generator" content="Microsoft Word 14 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
        {font-family:"Malgun Gothic";
        panose-1:2 11 5 3 2 0 0 2 0 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:"\@Malgun Gothic";
        panose-1:2 11 5 3 2 0 0 2 0 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri","sans-serif";}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
span.EmailStyle17
        {mso-style-type:personal-compose;
        font-family:"Calibri","sans-serif";
        color:windowtext;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-family:"Calibri","sans-serif";}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-US" link="blue" vlink="purple">
<div class="WordSection1">
<p class="MsoNormal">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.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Here are the details, using the optional MARS Fortran library as an example:<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">In the top level CMakeLists.txt:<o:p></o:p></p>
<p class="MsoNormal">option (USE_MARS<o:p></o:p></p>
<p class="MsoNormal"> "Build and use the MARS library." OFF)<o:p></o:p></p>
<p class="MsoNormal">if (USE_MARS)<o:p></o:p></p>
<p class="MsoNormal"> include_directories ("${PROJECT_SOURCE_DIR}/External/MARS")<o:p></o:p></p>
<p class="MsoNormal"> add_subdirectory (External/MARS) <o:p></o:p></p>
<p class="MsoNormal"> set (EXTRA_LIBS ${EXTRA_LIBS} mars)<o:p></o:p></p>
<p class="MsoNormal"> add_definitions(-DHAVE_MARS)<o:p></o:p></p>
<p class="MsoNormal">endif (USE_MARS)<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">and the External/MARS/CMakeLists.txt:<o:p></o:p></p>
<p class="MsoNormal">enable_language(Fortran)<o:p></o:p></p>
<p class="MsoNormal">add_library(mars ${LIBRARY_TYPE} mars36_fort.f)<o:p></o:p></p>
<p class="MsoNormal">install (TARGETS mars DESTINATION lib)<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">At build time, it all goes fine until the final link of the main executable (psuade):<o:p></o:p></p>
<p class="MsoNormal">Linking CXX executable psuade.exe<o:p></o:p></p>
<p class="MsoNormal">External\MARS\libmars.a(mars36_fort.f.obj):mars36_fort.f:(.text+0x21fce): undefined reference to `_gfortran_transfer_integer_write'<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">However, if I throw –lgfortran on the end, it works fine:<o:p></o:p></p>
<p class="MsoNormal">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<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">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.<o:p></o:p></p>
<p class="MsoNormal">I’m using gfortran 4.7.2 on both Windows and Linux.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Oh, I read about CMakeAddFortranSubdirectory, but the example linked to at
<a href="http://www.cmake.org/files/lapack_test.tar.gz">http://www.cmake.org/files/lapack_test.tar.gz</a> seems to have disappeared.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Thanks,<o:p></o:p></p>
<p class="MsoNormal">Jim<o:p></o:p></p>
</div>
</body>
</html>