MantisBT - CMake
View Issue Details
0013579CMakeModulespublic2012-10-09 11:162013-03-04 08:38
Jordan Lefebvre 
Brad King 
highmajoralways
closedfixed 
Windows7 64bit
CMake 2.8.9 
CMake 2.8.10CMake 2.8.10 
0013579: FortranCInterface.cmake does not pass CMAKE_BUILD_TYPE flags
In attempting to compile a mixed language static Windows executable, FortranCInterface.cmake breaks the cmake configuration.

Windows MSVC and Intel compilers toggle between static and dynamic system/compiler libraries using /MT and /MD compiler flags respectively. /libs:static /threads and /libs:dll /threads are equivalent to /MT and /MD respectively.

The issues stems from a difference in initialization across C/CXX flags and Fortran flags. The static/dynamic flags are initialized in the base CMAKE_Fortran_FLAGS while they are initialized in the CMAKE_[C|CXX}_FLAGS_<CMAKE_BUILD_TYPE> flags.

FortranCInterface.cmake does not pass the BUILD_TYPE flags through to the subproject. This results in mixed dynamic and static libraries and multiply defined symbols at linking.
1) Create cmake configuration which utilizes FortranCInterface.
2) Use Intel compilers on Windows
3) Add these additional configuration options to a configuration script.bat:
 cmake ^
-D CMAKE_C_FLAGS:STRING="/DWIN32 /D_WINDOWS /W3 /Zm1000" ^
-D CMAKE_CXX_FLAGS:STRING="/DWIN32 /D_WINDOWS /W3 /Zm1000 /EHsc /GR" ^
-D CMAKE_Fortran_FLAGS:STRING="/W1 /nologo /fpp" ^
-D CMAKE_C_FLAGS_RELEASE:STRING="/D_NDEBUG /O2 /MT" ^
-D CMAKE_CXX_FLAGS_RELEASE:STRING="/DNDEBUG /O2 /MT" ^
-D CMAKE_Fortran_FLAGS_RELEASE:STRING="/D_NDEBUG /O2 /MT" ^
-D CMAKE_Fortran_COMPILER:STRING=ifort ^
-D CMAKE_CXX_COMPILER:STRING=icl ^
-D CMAKE_C_COMPILER:STRING=icl ^
-G "NMake Makefiles" ^
%*

4) initialize Intel development environment
5) run script.bat path/to/src
This could be fixed by standardizing the initialization of compiler flags across all languages or passing all compiler flags through to the FortranCInterface subproject.

This prevents a static Windows build for executable deployment.
It would be convenient to have this fixed as soon as possible.
No tags attached.
Issue History
2012-10-09 11:16Jordan LefebvreNew Issue
2012-10-09 13:08Brad KingNote Added: 0031196
2012-10-09 13:11Bill HoffmanNote Added: 0031197
2012-10-10 07:46Jordan LefebvreNote Added: 0031201
2012-10-10 14:31Bill HoffmanNote Added: 0031205
2012-10-10 15:27Brad KingNote Added: 0031207
2012-10-10 15:28Brad KingNote Edited: 0031207bug_revision_view_page.php?bugnote_id=31207#r853
2012-10-10 15:38Brad KingAssigned To => Brad King
2012-10-10 15:38Brad KingStatusnew => assigned
2012-10-10 16:05Brad KingNote Added: 0031210
2012-10-10 16:05Brad KingNote Added: 0031211
2012-10-11 10:43Brad KingStatusassigned => resolved
2012-10-11 10:43Brad KingResolutionopen => fixed
2012-10-11 10:43Brad KingTarget Version => CMake 2.8.11
2012-10-17 17:00David ColeFixed in Version => CMake 2.8.10
2012-10-17 17:00David ColeTarget VersionCMake 2.8.11 => CMake 2.8.10
2013-03-04 08:38Robert MaynardNote Added: 0032475
2013-03-04 08:38Robert MaynardStatusresolved => closed

Notes
(0031196)
Brad King   
2012-10-09 13:08   
Please attach a complete example in a tarball.
(0031197)
Bill Hoffman   
2012-10-09 13:11   
When do you get an error? Does the try-compile fail and you don't get the .h files?
(0031201)
Jordan Lefebvre   
2012-10-10 07:46   
Yes. The try compile fails so the configuration halts. I will try to create a small example tarball.
(0031205)
Bill Hoffman   
2012-10-10 14:31   
I was able to reproduce using the test case. To reproduce you start a Intel command line prompt.

Then run
cmake -D CMAKE_C_FLAGS:STRING="/DWIN32 /D_WINDOWS /W3 /Zm1000" -D CMAKE_CXX_FLAGS:STRING="/DWIN32 /D_WINDOWS /W3 /Zm1000 /EHsc /GR" -D CMAKE_Fortran_FLAGS:STRING="/W1 /nologo /fpp" -D CMAKE_C_FLAGS_RELEASE:STRING="/D_NDEBUG /O2 /MT" -D CMAKE_CXX_FLAGS_RELEASE:STRING="/DNDEBUG /O2 /MT" -D CMAKE_Fortran_FLAGS_RELEASE:STRING="/D_NDEBUG /O2 /MT" -D CMAKE_Fortran_COMPILER:STRING=ifort -D CMAKE_CXX_COMPILER:STRING=icl -D CMAKE_C_COMPILER:STRING=icl -G "NMake Makefiles"


Which gives errors like this:

    /MANIFESTFILE:VerifyFortranC.exe.intermediate.manifest
    VerifyFortranC.exe.embed.manifest.res
    CMakeFiles/VerifyFortranC.dir/main.c.obj
    CMakeFiles/VerifyFortranC.dir/VerifyC.c.obj
    LIBCMT.lib(invarg.obj) : error LNK2005: __invoke_watson already defined in M
SVCRT.lib(MSVCR90.dll)
    LIBCMT.lib(crt0dat.obj) : error LNK2005: __amsg_exit already defined in MSVC
RT.lib(MSVCR90.dll)
    LIBCMT.lib(crt0dat.obj) : error LNK2005: __initterm_e already defined in MSV
CRT.lib(MSVCR90.dll)
    LIBCMT.lib(crt0dat.obj) : error LNK2005: _exit already defined in MSVCRT.lib
(MSVCR90.dll)
(0031207)
Brad King   
2012-10-10 15:27   
(edited on: 2012-10-10 15:28)
Minimum test case:

>type ..\CMakeLists.txt
cmake_minimum_required(VERSION 2.8.9)
project(Issue13579 C Fortran)
string(REPLACE "/MD" "/MT" CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}")
string(REPLACE "/libs:dll /threads" "" CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS}")
include(FortranCInterface)
FortranCInterface_VERIFY()
>cmake .. -G"NMake Makefiles" -DCMAKE_C_COMPILER:STRING=icl -DCMAKE_Fortran_COMPILER:STRING=ifort
    LIBCMT.lib(crt0dat.obj) : error LNK2005: _amsg_exit already defined in MSVCRT.lib(MSVCR90.dll)
    ...

(0031210)
Brad King   
2012-10-10 16:05   
As a work around, simply comment out the FortranCInterface_VERIFY() calls in your project for your deployment build. You know that the compilers are compatible.
(0031211)
Brad King   
2012-10-10 16:05   
This should fix it:

 http://cmake.org/gitweb?p=cmake.git;a=commitdiff;h=71491bbe [^]
(0032475)
Robert Maynard   
2013-03-04 08:38   
Closing resolved issues that have not been updated in more than 4 months.