C++コードをFortranに適用するには、次の例(http://fortranwiki.org/fortran/show/Fortran+and+Cpp+objects)に従っています。まず、Fortranを学習し、FortranとC++間の接続を試みています。FortranからC++への文字列の受け渡し
私は、私は、FortranからC++
#include <iostream>
#include <string>
using namespace std;
class CWLiDAR {
string name;
public:
CWLiDAR(string);
};
CWLiDAR::CWLiDAR(string name)
{
this->name = name;
}
/*
*/
/* C wrapper interfaces to C++ routines */
#ifdef __cplusplus
extern "C" {
#endif
CWLiDAR* CWLiDAR__new(string name)
{
return new CWLiDAR(name);
}
void CWLiDAR__delete(CWLiDAR* This)
{
delete This;
}
#ifdef __cplusplus
}
#endif
Fortranのラッパー
module CWLiDAR_module
use, intrinsic :: ISO_C_Binding!, only: C_int, C_ptr, C_NULL_ptr
implicit none
private
type CWLiDAR_type
private
type(C_ptr) :: object = C_NULL_ptr
end type CWLiDAR_type
interface
function C_CWLiDAR__new (name, name_len) result(this) bind(C, name = "CWLiDAR__new")
import
type(C_ptr) :: this
character(name_len, kind=C_CHAR), intent(IN) :: name
integer :: name_len
end function C_CWLiDAR__new
subroutine C_CWLiDAR__delete (this) bind(C, name = "CWLiDAR__delete")
import
type(C_ptr), value :: this
end subroutine C_CWLiDAR__delete
end interface
interface new
module procedure CWLiDAR__new
end interface new
interface delete
module procedure CWLiDAR__delete
end interface delete
public :: new, delete, CWLiDAR_type
contains
! Fortran wrapper routines to interface C wrappers
subroutine CWLiDAR__new(this, name, name_len)
type(CWLiDAR_type), intent(out) :: this
character(*) :: name
integer :: name_len
this%object = C_CWLiDAR__new(name, name_len)
end subroutine CWLiDAR__new
subroutine CWLiDAR__delete(this)
type(CWLiDAR_type), intent(inout) :: this
call C_CWLiDAR__delete(this%object)
this%object = C_NULL_ptr
end subroutine CWLiDAR__delete
end module CWLiDAR_module
メイン
program main
use cwlidar_module
type(CWLiDAR_type) :: lidar
call new(lidar, "Test", 4)
call delete(lidar)
end program main
どのように修正する必要がありますに文字列を渡したいコードを修正FortranからC++に文字列を渡すためのFortranラッパー?
変更するFortranラッパーはどれですか?どのように?彼らは正しいと思われる。彼らは正しいのですか?どのように? –
[FortranからCへの文字列を返す](https://stackoverflow.com/a/44424572/7038689)を参照してください。 –
なぜこれが重複としてマークされているのか分かりません - この質問はFortranからC++に渡すことに関するものであり、リンクされた疑わしい重複はC++からFortranに渡すことです。 – carthurs