Home Ask Login Register

Developers Planet

Your answer is one click away!

AD0AE February 2016

How to properly link gfortran and gcc?

I am trying to write a C function that calls a piece of fortran code. I thought it would be easier to try to link the fortran code directly versus trying to rewrite the fortran into C. I am using gcc/g++/gfortran on a mac where I directly downloaded the binaries and installed them.

The fortran code is at https://www.ngdc.noaa.gov/IAGA/vmod/igrf12.f. The specific subroutine I want to call is subroutine igrf12syn (isv,date,itype,alt,colat,elong,x,y,z,f)

The wrapper I wrote is as follows:

 #include <stdio.h>

 extern void igrf12syn( int *isv, double *year, double *itype, double *alt,
 double *colat, double *elong, double* x,double* y, double* z, double* f );

int main(){
    double B[4], BabsDervs[3], BrDervs[3], BthDervs[3], BphDervs[3];
    double r, th, ph,year,alt2,lat,colat,elong,x,y,z,f;
    int isv;

    // igrf12syn (isv,date,itype,alt,colat,elong,x,y,z,f)
    r = 6371e3;
    th = 0.57;
    ph = 0;

    year = 2015;
    alt2 = 200.0;
    lat = 33.25;
    colat = 90.0-lat;
    elong = 0.0;
    isv =1;
    igrf12syn_(&isv,&year,&isv,&r, &th, &ph,x,y,z,f);

    printf("%0.4e",f);
 return 0;
 }

The commands I am using to compile and call this are:

/usr/local/bin/gcc -lgfortran -c testigrf.c igrf12.f
/usr/local/bin/gcc -lgfortran -o b.out testigrf.o igrf12.o 

The error I get is:

duplicate symbol _main in:
testigrf.o
igrf12.o
ld: 1 duplicate symbol for architecture x86_64
collect2: error: ld returned 1 exit status
make: *** [igrf] Error 1

I understand what this error means and I can produce the nm files for the .o files which shows both of the main functions. However, I am not sure how to change the Fortran code to get rid of this error.

So my questions are the following: 1. How do

Answers


Rob11311 February 2016

Long time since I called Fortran routines, but your work looks plausible.

Now write a short fortran subroutine which you compile in an independant file, testing your call by reference subroutine parameters.

      subroutine igrf12syn (isv,date,itype,alt,colat,elong,x,y,z,f)

Of course you might try compiling the fortran file, simply deleting the program lines, up to that subroutine definition. From what I see it looks independant but the compiler will do a better job of checking source code.


Mike Kinghan February 2016

Firstly, your C program testigrf.c is defective. As it is, you have the warning:

testigrf.c: In function ‘main’:
testigrf.c:23:5: warning: implicit declaration of function ‘igrf12syn_’ [-Wimplicit-function-declaration]
     igrf12syn_(&isv,&year,&isv,&r, &th, &ph,x,y,z,f);
     ^

which occurs because your have declared a function called igrf12syn and invoked one called igrf12syn_, about which the compiler knows nothing.

Change igrf12syn_ to igrf12syn, recompile, and then the errors revealed are:

testigrf.c: In function ‘main’:
testigrf.c:23:26: warning: passing argument 3 of ‘igrf12syn’ from incompatible pointer type [-Wincompatible-pointer-types]
     igrf12syn(&isv,&year,&isv,&r, &th, &ph,x,y,z,f);
                          ^
testigrf.c:3:14: note: expected ‘double *’ but argument is of type ‘int *’
  extern void igrf12syn( int *isv, double *year, double *itype, double *alt,
              ^
testigrf.c:23:44: error: incompatible type for argument 7 of ‘igrf12syn’
     igrf12syn(&isv,&year,&isv,&r, &th, &ph,x,y,z,f);
                                            ^
testigrf.c:3:14: note: expected ‘double *’ but argument is of type ‘double’
  extern void igrf12syn( int *isv, double *year, double *itype, double *alt,
              ^
testigrf.c:23:46: error: incompatible type for argument 8 of ‘igrf12syn’
     igrf12syn(&isv,&year,&isv,&r, &th, &ph,x,y,z,f);
                                              ^
testigrf.c:3:14: note: expected ‘double *’ but argument is of type ‘double’
  extern void igrf12syn( int *isv, double *year, double *itype, double *alt,
              ^
testigrf.c:23:48: error: incompatible type for argument 9 of ‘igrf12syn’
     igrf12syn(&isv,&year,&isv,&r, &th 

Post Status

Asked in February 2016
Viewed 2,980 times
Voted 11
Answered 2 times

Search




Leave an answer


Quote of the day: live life