[CMake] mixing c/fortran - missing gfortran sometimes

Clinton Stimpson clinton at elemtech.com
Fri Sep 24 00:40:52 EDT 2010


  On 09/23/2010 10:56 AM, Brad King wrote:
> On 09/23/2010 12:20 PM, Clinton Stimpson wrote:
>> I've got a source files for a mixed C/fortran going into one library.
>> If the library is shared, -lgfortran is added to the link line, and I'm good
>> to go.
>> If it is static, -lgfortran is not added to any executable or shared library
>> that links with this static library.
> Are the executables built in the same project as the library?
In the same source/build tree, yes.  But not under the same project() call.
>> Should I add
>> target_link_libraries(my_mixed_c_fortran_lib
>> ${CMAKE_Fortran_IMPLICIT_LINK_LIBRARIES})
>> to fix this?  Or should CMake do this for me?
> This should be automatic.
>
> What version of CMake are you using?
Today's git.

If I have these two cmake files
=== CMakeLists.txt
project(prj)
add_subdirectory(sub)
add_executable(main main.cpp)
target_link_libraries(main sub)
===

=== sub/CMakeLists.txt
project(sub C Fortran)
add_library(sub hello.f hello2.c)
===

I get link errors.  If I change the first to have
project(prj C CXX Fortran)
or add an enable_language(Fortran)
then the problem goes away.

So does this basically mean the parent cmake files have to know what 
languages are being used?

Clint



More information about the CMake mailing list