Problem to call a Fortran subroutine in C++

Tim Prince timothyprince@sbcglobal.net
Wed Sep 13 12:12:00 GMT 2006


Americo Barbosa da Cunha Junior wrote:
> I've trying to call a Fortran subroutine in a C++ code.
> 
> Below you can see the declaration of the subroutine and the part of C++ 
> code that call it.
> 
> 
> extern "C" {
> extern void F_(int *n, double *tn, double *y, double *ftem, double 
> *rpar, int *ipar);
> }
> 
> // Jacobian
> void jacobian(double **Jac, double *savf, double *y, double dt, int n)
> {
>    int i,j;
>    double *ftem = malloc_vector(n);       // Vetor com Reaction Mapping 
> Perturbado     double *rpar = malloc_vector(n);       // Vetor de 
> trabalho double
>    int    *ipar = malloc_ivector(n);          // Vetor de trabalho inteiro
>    double *EWT = vector_ewt(y,n);      // Error Weight Vector
>    double u = uround();                         // Roundoff
>    double R, R0, FAC, tn;
>  
>    FAC = vector_wnorm(savf,EWT,n);
>    R0 = 1000*fabs(dt)*u*FAC*(double)n;
>    if( R0 == 0.0)
>        R0 = 1.0;
> 
>    for(i = 0; i < n; i++)
>    {
>        R = max(sqrt(u)*fabs(y[i]),R0/EWT[i]);      // Perturba??o
>        y[i] += R;                                                    // 
> Perturbando o vetor composicao
>        F_(&n,&tn,y,ftem,rpar,ipar);                      // Preenche o 
> ftem com o Reaction Mapping perturbado
>        FAC = 1.0/R;
>        for(j = 0; j < n; j++)
>            Jac[i][j] = (ftem[j]-savf[j])*FAC;
>    } }
> 
> 
> Whem I try to link both languages the compiler returns the following 
> error message: undefined reference to 'F_'
> 
> I did all procedures described in : 
> http://www.physics.utah.edu/~detar/phycs6720/handouts/fortran_binding.html
> 
Did you notice they used lower case names?  A pity they didn't advice 
you to run nm or the like on the individual .o files to help you resolve 
any such discrepancies.  Fortran compilers, by default, collapse the 
linkage names to all lower (in the case of GNU fortran) or all upper case.



More information about the Gcc-help mailing list