从c ++调用fortran子例程

时间:2013-12-26 17:56:01

标签: c++ linker makefile cmake fortran

我想从C ++调用Fortran子例程。由于我的程序需要包含来自deal.ii(微分方程分析库)的一些库,我使用CMake来创建makefile。我现在的问题如下:

  • CMakeLists.txt需要哪些更改才能链接Fortran文件?
  • 我是否必须在C ++文件中明确包含已使用的Fortran文件,或者是否足以在C ++代码的开头提供相应的函数原型?

提前多多感谢! SEB

PS .: 这是当前的CMakeLists.txt:

##
#  CMake script for the SIMP program:
##

# Set the name of the project and target:
SET(TARGET "SIMP")

# Declare all source files the target consists of:
SET(TARGET_SRC
  ${TARGET}.cc
  # You can specify additional files here!
  )

# Usually, you will not need to modify anything beyond this point...

CMAKE_MINIMUM_REQUIRED(VERSION 2.8.8)

FIND_PACKAGE(deal.II 8.0 QUIET
  HINTS ${deal.II_DIR} ${DEAL_II_DIR} ../ ../../ $ENV{DEAL_II_DIR}
  )
IF(NOT ${deal.II_FOUND})
  MESSAGE(FATAL_ERROR "\n"
    "*** Could not locate deal.II. ***\n\n"
    "You may want to either pass a flag -DDEAL_II_DIR=/path/to/deal.II to cmake\n"
    "or set an environment variable \"DEAL_II_DIR\" that contains this path."
    )
ENDIF()

DEAL_II_INITIALIZE_CACHED_VARIABLES()
PROJECT(${TARGET})
DEAL_II_INVOKE_AUTOPILOT()

2 个答案:

答案 0 :(得分:0)

查看http://www.cmake.org/Wiki/CMakeForFortranExample

如果您正在使用gfortran,那么您唯一需要的就是

enable_language(Fortran)

get_filename_component部分检查您是否存在编译器。非常有用的知道它正在挑选哪一个,特别是如果安装了gfortran,g95和ifort。

答案 1 :(得分:0)

现在我使用以下CMakeLists.txt文件,它可以正常工作。

##
#  CMake script for the SIMP program:
##

# Set the name of the project and target:
set (TARGET "SIMP")
set (CMAKE_Fortran_COMPILER "gfortran")
set (CMAKE_Fortran_FLAGS_RELEASE "-O2 -m32")
set (CMAKE_Fortran_FLAGS_DEBUG   "-O0 -g -m32")
set (CMAKE_CXX_FLAGS_DEBUG       "-O0 -g -m32")


# Declare all source files the target consists of:

SET(TARGET_SRC
  ${TARGET}.cc
# You can specify additional files here!
  fortran_subroutine.f
  )

# Usually, you will not need to modify anything beyond this point...

CMAKE_MINIMUM_REQUIRED(VERSION 2.8.8)

FIND_PACKAGE(deal.II 8.0 QUIET
  HINTS ${deal.II_DIR} ${DEAL_II_DIR} ../ ../../ $ENV{DEAL_II_DIR}
  )

IF(NOT ${deal.II_FOUND})

  MESSAGE(FATAL_ERROR "\n"
    "*** Could not locate deal.II. ***\n\n"
    "You may want to either pass a flag -DDEAL_II_DIR=/path/to/deal.II to cmake\n"
    "or set an environment variable \"DEAL_II_DIR\" that contains this path."
    )

ENDIF()

DEAL_II_INITIALIZE_CACHED_VARIABLES()

PROJECT(${TARGET} C CXX Fortran)

DEAL_II_INVOKE_AUTOPILOT()