Question

I'm trying to invoke a function in C from Fortran.
I don't understand well how to translate C pointer to Fortran types.
Maybe this is the source of the segmentation fault.
Below is my code.

Is my Fortran interface correct?
Is my Fortran program using the right data types?

The C function prototype (I don't have the code)

int __stdcall GRBnewmodel(
    GRBenv *env, 
    GRBmodel **modelP, 
    const char *Pname, 
    int numvars,
    double *obj, 
    double *lb, 
    double *ub, 
    char *vtype, 
    char **varnames
);

The Fortran interface

function GRBnewmodel(env, modelP, Pname, numvars,obj, lb, ub, vtype, varnames) bind(C, name = 'GRBnewmodel' )
    use iso_c_binding
    integer(c_int) :: GRBnewmodel
    type(c_ptr), value :: env
    type(c_ptr) :: modelP
    character(c_char), dimension(*)  :: Pname
    integer(c_int), value :: numvars
    real(8), dimension(*):: obj
    real(8), dimension(*) :: lb
    real(8), dimension(*) :: ub
    character(c_char), dimension(*)  :: vtype
    type(c_ptr) :: varnames ! character**
end function

The Fortran program

type(c_ptr) :: env = c_null_ptr
type(c_ptr) :: modelP = c_nul_ptr
integer :: error
real(8), dimension(10) :: obj
real(8), dimension(10) :: lb
real(8), dimension(10) :: ub
character(c_char), dimension(10) :: vtype
integer :: i

error = GRBloadenv(env, 'gurobi.log'//c_null_char)

obj = 0
lb = 0
ub = 1
vtype = [ ('C', i=1,10) ]

! segmentation fault is on this line
error = GRBnewmodel( &
    env, &
    modelP, &
    c_null_char, &
    10, &
    obj, &
    lb, &
    ub, &
    vtype, &
    c_null_ptr &
)

I'm working on a MAC Maverick with gfortran.

Was it helpful?

Solution

I found the right calling convention.
I posted it in this github repository.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top