[CMake] Mixed Fortran and C sources with Visual Studio 10

Mark Stijnman mark.stijnman at gmail.com
Mon Oct 28 07:04:43 EDT 2013


On Tue, Oct 22, 2013 at 7:45 PM, Bill Hoffman <bill.hoffman at kitware.com> wrote:
> Hi,
>
> I have thought about CMake doing something automatically here, however, it
> could get tricky pretty fast, and it is pretty easy to fix in the CMake file
> of the project.

You'd have to detect whether the main function will be in the C part
or if the Fortran part contains a PROGRAM subroutine to figure out
which part becomes the lib, and which part the executable. You'd have
to parse all files to find out. And then what should happen if CMake
finds neither, or both? I agree that it's just as easy to sort this
out by hand.

The disadvantage of splitting the C and Fortran parts into two
projects is that sometimes you are likely to get cyclic dependencies
between the projects (when C code calls Fortran, and Fortran code
calls C code) which can lead to link order issues on linux. You may
therefore end up having to split the project into more libraries.
which is another reason CMake will not easily be able to do this
automatically. On the up side, having to remove the cyclic
dependencies may lead to a better code structure, so there's that.

> Your example would be:
> enable_language(Fortran)
> enable_language(C)
> add_library(tst_lib_f tst.F)
> add_executable(tst  cfunc.c)
> target_link_libraries(tst tst_lib_f)

Actually, since the OP mentions that the main is in Fortran, the
solution would be:
add_library(tst_lib_c cfunc.c)
add_executable(tst  tst.F)
target_link_libraries(tst tst_lib_c)


More information about the CMake mailing list