Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28632 Discussions

Calling C procedures from Fortran

nuno_pinhao
Beginner
796 Views
I am trying to call C procedures on the GNU Scientific Library (gsl) from a Fortran code but without success: the linker always complains about the missing trailing underscore on the routine name.
Is there any way to modify the default compiler/linker behavior of expecting a trailing underscore on those procedures?
I've tried to define an interface for the gsl procedures ( with:
!DIR$ ATTRIBUTES C, ALIAS: 'name_without_underscore' ::gsl_routine _name) but without success.

Thank you
0 Kudos
6 Replies
newguy
Beginner
796 Views
I am having a similar problem. I am not able to call the c procedures compiled with gcc compiler. I am guessing this is because of the incompatibility of the binary/oject files produced by GNU c compiler and ifc compiler. Do I make sense? have you figured out what ur problem was??
0 Kudos
Steven_L_Intel1
Employee
796 Views
Try the directive with !DEC$ instead of !DIR$. I am told that !DIR$ is not recognized for ATTRIBUTES.

Steve
0 Kudos
newguy
Beginner
796 Views
Sorry this may have nothing to do with the earlier posts.
This is the problem I am having.
I have one c file and a lot of Fortran files. I compile the c file using the gcc Ver2.96 and the Fortran file using ifc. When I go to link the files, all the c procedure I am calling in Fortran show up as an error "undefined reference to __".

Is there a flag I can use or am I doing something wrong here? I have refereed to the user guide about this issue and I am following the guidelines for calling C procedures.
0 Kudos
Martyn_C_Intel
Employee
796 Views
You might try using the nm utility to check the symbols being generated on both C and Fortran sides. If they don't match, you can either change the function names in C, or use the !DEC$ ATTRIBUTES ALIAS directive in Fortran as discussed above. You also need to be aware of the different conventions for passing arguments in C and Fortran, as discussed in the User's Guide.

Martyn
0 Kudos
ithinkergoiam
Beginner
796 Views
try compiling the gnu stuff with -fno_second_underscore
0 Kudos
a_p_willis
Beginner
796 Views
I think that even if you sort out the underscore issues, calling GSL directly from fortran is unlikely to work due to the way the arguments to the functions are passed. In fortran they are always passed by reference (pointers). The usual way of merging a C function with another language is to write a small 'wrapper' function in C. This includes the underscore in the name and pointer types for the arguments. The wrapper is then called from fortran (without the underscore). For example
Fortran: INTEGER I,J ... CALL WRAP(I,J)
C: void wrap_(integer* i, integer* j){ *j = gsl_something(*i); }
More info can be found at
http://earth.leeds.ac.uk/~ash/aco/freesoftware.html
0 Kudos
Reply