【发布时间】:2021-10-13 10:32:37
【问题描述】:
我正在尝试从以下 Fortran 代码 (util.f90) 构建一个简单的示例共享库(使用 cmake):
module util
implicit none
type dummy
integer :: n
real, allocatable :: buff(:)
end type
contains
subroutine allocate_dummy(x, n)
implicit none
type(dummy), intent(out) :: x
integer, intent(in) :: n
x%n = n
allocate(x%buff(n))
end subroutine
subroutine print_dummy(x)
implicit none
type(dummy) :: x
integer :: i
if (allocated(x%buff)) then
write(*, *) x%n, x%buff, 'Sum:', sum(x%buff)
else
write(*, *) 'not allocated'
end if
end subroutine
end module
示例主程序 (main.f90) 在哪里
program main
use util
implicit none
type(dummy) :: x
call allocate_dummy(x, 3)
x%buff = 3.14
call print_dummy(x)
end program
因此库只分配x 并打印它。
我使用以下CMakeLists.txt 来构建库和可执行文件
cmake_minimum_required(VERSION 3.10)
project(DUMMY)
enable_language(Fortran)
add_library(dumlib SHARED util.f90)
target_include_directories(dumlib INTERFACE ${CMAKE_CURRENT_BINARY_DIR})
add_executable(a.out main.f90)
target_link_libraries(a.out dumlib)
但是./a.out 打印出来
3 Sum: 9.42000008
应该在哪里打印
3 3.14000010 3.14000010 3.14000010 Sum: 9.42000008
直接编译(没有共享库),或者如果我手动构建库(没有 cmake),行为正确。但是使用 cmake 共享库,x%buff 不会打印出来。由于Sum: 已正确计算和打印,因此代码可以正常工作。
我想知道可能是什么问题?
编辑:
如果(在write 语句中)我使用(x%buff(j), j=1, n) 而不是x%buff,则问题已得到解决。但是,我仍然对这种行为的原因感到好奇。
编辑 2:
我的 Fortran 编译器是 gfortran(gcc 版本 9.3.0)。
【问题讨论】:
-
这确实很奇怪。我无法复制它。
-
可能是编译器问题。我使用了 gcc 9.3.0 中的 gfortran。 @弗拉基米尔F
-
这看起来像是编译器或编译器配置的差异。
标签: cmake fortran shared-libraries gfortran