MantisBT - CMake
View Issue Details
0015872CMakeCMakepublic2015-12-06 23:392016-05-02 08:30
Ben Matthews 
 
normalmajoralways
closedno change required 
x86_64LinuxVarious
CMake 3.3.2 
 
0015872: Programs with Fortran entry point (but calls into C) don't link correctly using Intel or PGI compilers
CMake always seems to link mixed language programs using the C++ compiler. Intel and PGI link libcrt, which wants to have a main(). Programs with a Fortran entry point typically won't have main(). Setting the linker_language property doesn't seem to make a difference.

Using the GNU compilers, CMake still uses the C++ compiler, but it gets the libraries right and everything works as expected.
git clone https://github.com/matthb2/cmake_f90_entry_bug.git [^]
cd cmake_f90_entry_bug
mkdir build
cd build
CC=icc CXX=icpc FC=ifort cmake ../
make

####
make
Scanning dependencies of target testprog
[ 50%] Building Fortran object CMakeFiles/testprog.dir/test.f90.o
[100%] Linking CXX executable testprog
/usr/lib/gcc/x86_64-pc-linux-gnu/4.9.3/../../../../lib64/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
CMakeFiles/testprog.dir/build.make:94: recipe for target 'testprog' failed
make[2]: *** [testprog] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/testprog.dir/all' failed
make[1]: *** [CMakeFiles/testprog.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
Note that the example code is a bit of a simplified/contrived example. It will work fine if you remove the HAS_CXX property, but real cases also incorporate C/C++ modules and require that libc/libstdc++ be present.

icc -V
Intel(R) C Intel(R) 64 Compiler XE for applications running on Intel(R) 64, Version 13.0.0.079 Build 20120731
Copyright (C) 1985-2012 Intel Corporation. All rights reserved.
FOR NON-COMMERCIAL USE ONLY
No tags attached.
Issue History
2015-12-06 23:39Ben MatthewsNew Issue
2015-12-07 05:17tgallagherNote Added: 0039957
2015-12-07 05:19tgallagherNote Edited: 0039957bug_revision_view_page.php?rev_id=1970
2015-12-07 05:25tgallagherNote Deleted: 0039957
2015-12-07 08:41Brad KingNote Added: 0039958
2015-12-07 08:41Brad KingStatusnew => resolved
2015-12-07 08:41Brad KingResolutionopen => no change required
2016-05-02 08:30Robert MaynardNote Added: 0040979
2016-05-02 08:30Robert MaynardStatusresolved => closed

Notes
(0039958)
Brad King   
2015-12-07 08:41   
The HAS_CXX property is documented as obsolete and equivalent to setting LINKER_LANGUAGE to "CXX". Just don't set HAS_CXX. If there are C++ sources in the target CMake will know it needs to add the C++ runtime libraries and such even when using the Fortran linker.
(0040979)
Robert Maynard   
2016-05-02 08:30   
Closing resolved issues that have not been updated in more than 4 months.