[CMake] CMake thinks Fortran is C++ when linking statically

Erlend Pedersen erlend.pedersen at holberger.com
Tue May 10 11:34:30 EDT 2011

On Tue, 2011-05-10 at 14:42 +0200, Michael Hertling wrote:
> On 05/10/2011 12:19 PM, Erlend Pedersen wrote:
> > I am having problems with Fortran linking that doesn't work when
> > building static libraries, only when building shared libraries. I am
> > using cmake from Ubuntu 10.04 (2.8.0-5ubuntu1).
> > 
> > I use CMake to build a project, including project-internal shared
> > libraries. All executables are Fortran, but one of the internal
> > libraries contain minimal sprinklings of C and C++. All works well when
> > building shared libraries, but when I switch to building static
> > libraries, CMake decides to try to link all Fortran executables as if
> > they were C++. Unfortunately, this means that the main function is not
> > properly located, and linking fails.
> > 
> > I tried forcing Fortran linkage on libraries only (hoping this would
> > propagate into the executables that link to them), using 
> > 
> > 
> > and when building the libraries, output shows that they are indeed
> > treated as Fortran static libraries. But the executables (typically
> > conisting of a single .f90 file, and linked to one or more of the
> > internal libraries) are still treated as C++.
> > 
> > Only when I force the LINKER_LANGUAGE by using SET_TARGET_PROPERTIES on
> > both libraries and executables does things build properly. But that
> > seems needlessly redundant and noisy and redundant.
> > 
> > Is there some easier way I can convince CMake to link the executables
> > correctly? Is this a bug?
> No, this isn't a bug. The behaviour you observe occurs because of
> i.e. when a static library with C++ code is mixed with Fortran code,
> the C++ linker is taken into account along with the Fortran linker,
> and C++ will win; this does not apply to _shared_ libraries. Hence,
> the appropriate solution in your case, i.e. Fortran code with one,
> say, C++-contaminated _static_ library. Alternatively, you might
> raise the Fortran linker preference above the C++ linker's one.
> 'hope that helps.

It helped. Thank you very much.

Erlend Pedersen
Holberg Research AS

More information about the CMake mailing list