Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Naming of symbols in Fortran shared library, intel vs gcc?

Is there a way to control the naming of symbols in shared libraries? Specifically, I've been using GCC for a project where we access shared library through C-Types in Python. This works great, however recently I've been working with a system where the Intel compiler is suggested. I can build the shared object just fine but I find the symbols have a slightly different naming convention as compared to intel. Specifically when I compile with gcc the symbol names look like:

__test_function_MOD_read_a_file

the Intel compiled shared object has the symbol names like:

test_function_mp_read_a_file__

Is there a way to force consistency of the naming or at least change the name of the symbols after the fact?

For example consider the following bit of code test_function.f90

MODULE test_function
   CONTAINS
   SUBROUTINE read_a_file
      PRINT *,'I did a thing!'
   END SUBROUTINE
END

The compile line should look something like

gfortran -fPIC -c test_function.f90
gfortran -shared -o libtest_function.so test_function.o
like image 839
Lazer Avatar asked Oct 25 '25 15:10

Lazer


1 Answers

Both compilers are mangling the names of the subprograms contained in the module. The Fortran standard does not mandate any naming convention. You can prevent the name mangling by using the ISO C Binding feature of Fortran to give the subprogram a specific name. For example,

module bar
   contains
   function fun(x) bind(c, name='foo')
       real fun, x
       fun = x
   end function fun
   function goo(x)
       real goo, x
       goo = x
   end function goo
end module bar

When compiled with gfortran the resulting object file contains

gfortran -c a.f90
nm a.o
00000000 T __bar_MOD_goo
00000013 T foo

Thus, you can reference functionfun as foo in the library. You'll probably also want to use the types defined by the iso_c_binding module.

like image 179
evets Avatar answered Oct 28 '25 04:10

evets



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!