- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试编写 CMakeLists.txt 以耦合一个简单的 FORTRAN 程序,该程序使用 iso_c_binding 调用 C++ 函数。当我使用 gfortran 作为 FORTRAN 编译器时,代码运行良好,但是当我切换到 openMPI (mpif90) 而不是 gfortran 时,我收到错误:
CMakeFiles/test.exe.dir/main.f90.o: In function `MAIN__':
main.f90:(.text+0x2d): undefined reference to `mpi_init_'
main.f90:(.text+0x47): undefined reference to `mpi_comm_size_'
main.f90:(.text+0x61): undefined reference to `mpi_comm_rank_'
main.f90:(.text+0x1c3): undefined reference to `mpi_finalize_'
collect2: ld returned 1 exit status
make[2]: *** [test.exe] Error 1
make[1]: *** [CMakeFiles/test.exe.dir/all] Error 2
make: *** [all] Error 2
我附上 gfortran 版本(工作)。为了切换到 mpif90,我只是取消注释 main.f90 中的注释行:
program main
use iso_c_binding
implicit none
! include 'mpif.h'
interface
function func (n,x) bind (C, name="func")
import
integer(c_int):: func
integer(c_int):: n
real(c_double), dimension(1:n), intent(in):: x
end function func
end interface
integer(c_int):: n
real(c_double), dimension(:), allocatable:: x
integer(c_int):: result
! integer, parameter:: master = 0
! integer:: numtasks, taskid, ierr, dest
! integer status(mpi_status_size)
! call mpi_init(ierr)
! call mpi_comm_size(mpi_comm_world, numtasks, ierr)
! call mpi_comm_rank(mpi_comm_world, taskid, ierr)
! if (taskid .eq. master) then
n = 3
allocate(x(1:n))
x = (/1., 2., 3./)
result = func(n,x)
deallocate(x)
! end if
! call mpi_finalize(ierr)
end program main
函数.c:
#include <iostream>
using namespace std;
#ifdef __cplusplus
extern"C" {
#endif
int func(int& n, double x[]) {
std::cout << x[0] << " " << x[1] << " " << x[2] << std::endl;
return 0;
}
#ifdef __cplusplus
}
#endif
CMakeLists.txt:
PROJECT(test)
cmake_minimum_required(VERSION 2.6)
enable_language(C Fortran)
# -------------------------
# Setting the compilers
# -------------------------
#set (CMAKE_Fortran_COMPILER /usr/local/openmpi/bin/mpif90)
set (CMAKE_Fortran_COMPILER /usr/bin/gfortran)
set (CMAKE_C_COMPILER /usr/bin/g++)
# -------------------------
# Setting the flags
# -------------------------
set_source_files_properties(main.f90 func.cpp PROPERTIES COMPILE_FLAGS "-c -lstdc++")
# -------------------------
# Making the executable
# -------------------------
ADD_EXECUTABLE(test.exe main.f90 func.cpp)
如果能就此问题提供任何帮助,我将不胜感激。
最佳答案
问题是您没有在任何地方链接到 MPI。此外,手动设置 CMAKE_Fortran_compiler 是错误的方法,因为它根本不可移植。这是我在我的一个项目中拥有的简化版本。请注意,我使用的是最新版本的 cmake 附带的 FindMPI 模块。我不知道 cmake 2.6 版是否有该模块,但您可以轻松地在网上找到它。
cmake_minimum_required (VERSION 2.8)
project(DYNAMO Fortran C)
find_package(MPI REQUIRED)
set(DYNAMO_LINK_LIBRARIES ${MPI_Fortran_LIBRARIES})
set(DYNAMO_SRCS main.f90)
add_executable(dynamo ${DYNAMO_SRCS})
set_target_properties(dynamo PROPERTIES COMPILE_FLAGS "${MPI_Fortran_COMPILE_FLAGS}")
set_target_properties(dynamo PROPERTIES LINK_FLAGS "${MPI_Fortran_LINK_FLAGS}")
target_link_libraries(dynamo ${DYNAMO_LINK_LIBRARIES})
install(TARGETS dynamo RUNTIME DESTINATION bin)
关于c++ - 使用 cmake 将 C++ 和 FORTRAN (mpif90) 与 iso_c_binding 耦合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25187754/
我正在编写一些调用 fortran 的 C 代码,但不清楚 iso_c_binding 模块的使用。 我的 Fortran 和 C 接口(interface)在没有 iso_c_binding 的情况
在混合 C/Fortran 应用程序的上下文中,有没有一种方法可以检查编译器是否知道“iso_c_binding”(例如 GCC 4.1.2 不知道,而 4.3.4 知道),就像预编译器一样-处理指令
我正在写 iso_c_binding 在 Fortran 中调用具有以下原型(prototype)的 C 函数 int zmat_run( const size_t inputsize,
我有一个使用 OpenMP 并行化的 Fortran 程序。主函数调用一个 Fortran 函数,该函数包含具有常微分方程的系统的定义。当拆分到不同的线程时,这可以正常运行。我还想要链接到具有类似 O
我正在编写代码,使用 Fortran 的 C 互操作性机制(在 Fortran 2003 中引入并在较新版本的 gfortran 和 ifort 中实现)从 Fortran 调用 C 函数。 This
我正在使用两个 Fortran 模块。第一个包含一个子程序 foo : module fmod1 contains subroutine foo(i) implicit none
我正在尝试获取 Fortran 代码库以使用 ISO_C_BINDING 调用 C 库调用,但我在传递 Fortran 数组时遇到了问题。 我创建了一个简单示例来说明我的问题。 主.f90 progr
如何将 C 字符串数组 (char* cstrings[]) 传递给 Fortran子程序? 问题 Arrays of strings in fortran-C bridges using iso_c
C/C++ 编译器供应商是否也支持 Fortran ISO_C_BINDING 模块的概念?例如,C/C++ int 的大小可能因不同供应商的编译器而异。因此,通过 ISO_C_BINDING 模块,
我想从 Fortran 调用 C 函数并接收在 C 函数中定义的字符串。我搜索过,但到目前为止我找不到一个有效的、直接的答案。 实际上我找到了一个解决方法:接收一个字符数组,然后使用内部函数 tran
我有一个旧的 Fortran 代码,我想将其与新的 C/C++ 程序混合。 Fortran 子例程动态分配一些我想传递给 C 程序的数组。我只会在运行 Fortran 代码后获得这些数组的大小。 在这
我有以下 Fortran 函数 create_pointer,它调用名为 create_pointer 的 C 函数,该函数创建一个指针并返回其地址: FUNCTION create_pointer(
是否可以使用内部 iso_c_bindings 模块为单个 C 函数/过程生成多个 Fortran 2003 接口(interface)? 对于 C 类型参数与 Fortran 参数不完全匹配的情况,
我正在尝试将一个并行 MPI Fortran 程序链接到一个也使用 MPI 的并行 C 库。软件体系结构以 Fortran 为中心,因此我尽量保留 Fortran 方面的内容。 所以我需要将 C 例程
几周前我发布了一个类似的问题 ( iso_c_binding calling C routine with pointers from Fortran with arrays ),我找到了解决问题的方
我正在尝试编写 CMakeLists.txt 以耦合一个简单的 FORTRAN 程序,该程序使用 iso_c_binding 调用 C++ 函数。当我使用 gfortran 作为 FORTRAN 编译
是否有一种工具可以使用 Fortran 2003 标准中的内在 iso_c_bindings 模块,自动从 C 库头生成 Fortan 绑定(bind)? 我对将 C 语言转换为 Fortran 语言
我是一名优秀的程序员,十分优秀!