I am following the following example (http://fortranwiki.org/fortran/show/Fortran+and+Cpp+objects) to adapt a C++ code to Fortran, first I am trying to learn Fortran and making connection between Fortran and C++.
I modified the code where I would like to pass a string from Fortran to 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
The Fortran wrapper
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
The main
program main
use cwlidar_module
type(CWLiDAR_type) :: lidar
call new(lidar, "Test", 4)
call delete(lidar)
end program main
How should I modify the Fortran wrapper to pass a string from Fortran to C++?