Search code examples
fortrannamespacesshared-librariesintel-fortran

Compile different files where functions and common blocks have the same name


I am currently trying to implement some 3rd party code (program A) within another 3rd party program (program B). Unfortunately, it seems like some COMMON blocks and subroutines share names between the two codes. This is not detected by the compiler (I suspect because the compilation process involves many different files and creating a shared object), but the program crashes when accessing certain common blocks / subroutines with very general names (e.g. BASIS, JACOBIAN), and renaming them alleviates the problem. However, renaming all common blocks and subroutines within program A is not feasible because of its size.

At the moment, I have two seperate directories of code. I compile both seperately with the intel compiler into .o files and then create a shared object from both:

ifort -c -fPIC -fp-model precise codeA.f
ifort -c -fPIC -fp-model precise codeB.f
ifort -c -fPIC -fp-model precise code_coupling.F90
ld -shared -o library.so codeA.o codeB.o code_coupling.o

The code in code_coupling.F90 is for coupling both codes and it is called within codeB.f, which I cannot change.

  • Is there a possibility to compile codeA.f with some additional compiler flags so that the names of the COMMON blocks and subroutines don't interfere with each other?
  • Is there some other way I can prevent the names from interfering with each other?

Solution

  • One (a little bit hacky) solution I have discovered is to compile codeA.f with the flag -assume nounderscore, and rename the functions that need to be called in code_coupling.F90 manually with a trailing underscore:

    ifort -c -fPIC -fp-model precise -assume nounderscore codeA.f
    ifort -c -fPIC -fp-model precise codeB.f
    ifort -c -fPIC -fp-model precise code_coupling.F90
    ld -shared -o library.so codeA.o codeB.o code_coupling.o
    

    Rename the subroutine codeA_subroutine within codeA.f to codeA_subroutine_.