繁体   English   中英

从C ++调用fortran子例程

[英]calling fortran subroutines from c++

我想从C ++调用Fortran子例程。 由于我的程序需要包括Deal.ii中的某些库(微分方程分析库),因此我使用CMake创建了makefile。 我的问题如下:

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

在此先多谢! 勒布

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()

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

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

enable_language(Fortran)

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

现在,我使用以下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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM