1 回答

TA貢獻1856條經驗 獲得超5個贊
// cplusplus source: cmain.cpp
#include <iostream>
extern "C" void get(int& tmp) {
std::cin >> tmp;
if (std::cin.good())
std::cout << "function \"get\" in C++:\t" << tmp << std::endl;
else {
std::cerr << "ERROR: get a value failure" << std::endl;
tmp = 0;
}
}
extern "C" void put(const int& tmp) {
// output numeric: 0xnnn formatting
std::cout.setf(std::ios_base::hex, std::ios_base::basefield);
std::cout.setf(std::ios_base::showbase);
std::cout << "function \"put\" in C++:\t" << tmp << std::endl;
}
!!! Fortran source: fmain.f08
module croutine
interface
!names difference between C++ and Fortran routine here
subroutine rcget(item) bind(C, name='get')
use, intrinsic :: iso_c_binding
integer(c_int), intent(inout) :: item
end subroutine rcget
subroutine put(item) bind(C)
use, intrinsic :: iso_c_binding
integer(c_int), intent(in) :: item
end subroutine put
end interface
end module croutine;
program main
use croutine
use, intrinsic :: iso_c_binding
integer(c_int) :: dat = 0
print *, "value in Fortran: ", dat
call rcget(dat)
print *, "value after called C++ function", dat
call put(dat)
print *, "Finish, has returned to Fortran"
end program
- 1 回答
- 0 關注
- 138 瀏覽
添加回答
舉報