给点干货吧
Fortran代码
[Fortran] 纯文本查看 复制代码 module mixcompile
use iso_c_binding
implicit none
contains
subroutine cpp_print(cpp_char, n) bind(c, name="f90print")
use iso_c_binding
implicit none
type(c_ptr), value:: cpp_char
integer(c_int), value:: n
character(len=n), pointer:: f_string
call c_f_pointer(cpp_char, f_string)
write(*, '("Length of string:", 1x, I4)')n
write(*, *)f_string
end subroutine
end module
c++代码
[C++] 纯文本查看 复制代码 #include <string>
extern "C" {void f90print(const char*, int);};
int main(int argc, char *argv[])
{
std::string s{"0123456789"};
f90print(s.c_str(), s.size());
for(int i=0; i<argc; i++){
s = argv[i];
f90print(s.c_str(), s.size());
}
return 0;
}
编译命令
[Bash shell] 纯文本查看 复制代码 g++ a.f90 b.cc -lgfortran -o example |