[CMake] Fortran+CXX project with CMake 2.2.2
William A. Hoffman
billlist at nycap.rr.com
Thu Nov 10 12:54:17 EST 2005
>es, here is the CMakeLists.txt that actually works. I also added a test to make sure both .f and .F extensions were handled correctly.
>
># The project must specify all languages that are being used in the project
>PROJECT(add_fortran CXX Fortran)
>ADD_EXECUTABLE(add_fortran main.cpp testf1.F testf2.f)
>
># You must which linker to use for a multiple language project
>SET_TARGET_PROPERTIES(add_fortran PROPERTIES LINKER_LANGUAGE CXX)
>
># You need to find and link with libg2c (I _think_ this applies only if you are using g77)
>FIND_PATH(G2C_DIR libg2c.a /opt/local/lib)
>LINK_DIRECTORIES(${G2C_DIR})
>TARGET_LINK_LIBRARIES(add_fortran g2c)
The reason that you have to add the libg2c is because of the LINKER_LANGUAGE stuff. If g77 is not used as the linker, it will not link in the stuff in needs. However, if c++ is not used as the linker, it will have the same problem, but with missing c++ libs. The following may be a better way to link in the G2C:
FIND_LIBRARY(G2C NAMES g2c PATHS /opt/local/lib)
TARGET_LINK_LIBRARIES(add_fortran ${G2C})
(it should do about the same thing, but have a little better debug info if it
is not found.)
-Bill
More information about the CMake
mailing list