亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

各位大神,請問fortran調用C++函數時變量的傳遞方式是咋樣的?

各位大神,請問fortran調用C++函數時變量的傳遞方式是咋樣的?

C++
一只甜甜圈 2022-08-11 15:11:11
我想將C++函數運行后的變量值返回到Fortran中,請問如何實現?以下是我的代碼:我想得到經過abs()函數作用后的tn值fortran部分:program mainimplicit noneINTERFACESUBROUTINE abs(i) BIND(C)USE,INTRINSIC :: ISO_C_BINDINGINTEGER(C_INT), VALUE,INTENT(IN) :: iEND SUBROUTINE absEND INTERFACEinteger(4)::tncall trans(tn)write(*,*) tn............endC++部分extern "C" void trans(int *tempt){*temp=5;cout<<*tempt;}
查看完整描述

1 回答

?
RISEBY

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


查看完整回答
反對 回復 2022-08-15
  • 1 回答
  • 0 關注
  • 138 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號