使用cmake通过iso_c_binding来耦合c++和FORTRAN (mpif90)
Using cmake for coupling C++ and FORTRAN (mpif90) with iso_c_binding
我正试图编写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
func.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)
相关文章:
- 使用 VS2015 在 Windows 10 上构建 Fortran .lib x64 位并将其链接到 C++
- 将 OR 逻辑运算符从 C++ 转换为 Fortran
- Fortran 和 C++ 的混合编程:Fortran 不能调用C++子程序
- 如何在C++中获取真实值精度 与Fortran相同(Pararel Studio XE编译器)
- 读取 C++ 中的 Fortran 数据类型
- 重新编译我们的FORTRAN代码并在C++中使用后,我们的system()或C_str()命令无法正常工作
- 使用C++和fortran构建R包,Mac
- 如何将C++17 stdc++fs库链接到混合语言(C++和Fortran)CMake项目
- 混合编程 - 包括C++标头到 Fortran
- 编译 Fortran 和C++程序并从英特尔编译器链接
- 与 MSVC 一起使用的 MinGW 的 Fortran 模块导致未解析的符号"__exmod_MOD_value"
- 将指向 1D Fortran 数组的指针传递给使用 C_LOC() 进行C++会导致垃圾值
- 将分配给C++数组传递给 Fortran 子例程
- 在 Fortran 中泛化特定声明类型的操作
- CPP -D 选项,用于预处理 Fortran 代码
- 通过矢量下标访问 Fortran 数组,相当于 cpp
- 如何将矢量从C 传递到Fortran
- 将字符串从 Fortran 传递到 c++
- 是否可以从C 调用FORTRAN接口
- C /FORTRAN项目由Visual Studio 2010/Intel 2013创建,将不会在Visual St