[英]How can I link a fortran program with a module dependency for use in c++?
fortranModule.f90 fortranModule.f90
module fortranModule
use iso_c_binding
implicit none
contains
subroutine print_f90()
implicit none
write(*,*) "This is a message from Fortran 90."
end subroutine print_f90
end module fortranModule
fortranProgram.f90 fortran程序.f90
program fortranProgram
use iso_c_binding
use fortranModule
implicit none
!Do some work as input for print_f90()
call print_f90()
end program fortranProgram
main.cpp主文件
extern "C" {
extern void fortranProgram(void);
}
int main(void) {
fortranProgram();
return 0;
}
Attempting to compile with the following steps:尝试使用以下步骤进行编译:
gfortran -c fortranModule.f90 fortranProgram.f90
g++ -c main.cpp
g++ main.o fortranModule.o fortraProgram.o -o main -lgfortran
gives给
duplicate symbol _main in:
main.o
fortranProgram.o
ld: 1 duplicate symbol for architecture x86_64
How can I link a fortran program with module dependencies to a c++ program?如何将具有模块依赖关系的 fortran 程序链接到 c++ 程序? Is this possible?
这可能吗? Do I need to rewrite fortranProgram.f90 as a module or subroutine?
我需要将 fortranProgram.f90 重写为模块或子程序吗? Maybe there is a naming scheme similar to __modulename_MOD_subroutinename ?
也许有一个类似于__modulename_MOD_subroutinename的命名方案?
Changing fortranProgram.f90 to将 fortranProgram.f90 更改为
subroutine call_print_f90(num) bind(c, name='call_print_f90')
use iso_c_binding
use fortranModule
implicit none
call print_f90(num)
end subroutine call_print_f90
and changing main.cpp to并将 main.cpp 更改为
extern "C" {
extern void call_print_f90(void);
}
int main(void) {
call_print_f90();
return 0;
}
resolves the problem of having two conflicting "mains".解决了两个相互冲突的“主电源”的问题。
Below is a solution in which fortranProgram.f90 is refactored as a module.下面是一个将 fortranProgram.f90 重构为模块的解决方案。
fortranProgram.f90 fortran程序.f90
module fortranProgram
use iso_c_binding
use fortranModule
implicit none
contains
subroutine call_print_f90() bind(c, name='call_print_f90')
call print_f90(num)
end subroutine call_print_f90
end module fortranProgram
main.cpp主文件
extern "C" {
extern void call_print_f90(void);
}
int main(void) {
call_print_f90();
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.