Stack Exchange
Fortran shared lib .so shows undefined symbol error
I'm compiling a Fortran code to obtain shared library .so. In the code it uses a module. The compiling has no problem while it reports undefined symbol error when opening the .so library.
Code mesh_map.F are as follows:
#include "cfx5ext.h"
dllexport(mesh_map)
      SUBROUTINE mesh_map (CZ, DZ, IZ, LZ, RZ)
      USE EXTRA_FLUID2
      USE ISO_C_BINDING
      USE IFPORT
      IMPLICIT NONE
      CHARACTER*(1) CZ(*)
      DOUBLE PRECISION DZ(*)
      INTEGER IZ(*)
      LOGICAL LZ(*)
      REAL RZ(*)

      MAP_STATUS = 1
 
      END

extra_fluid2.f90:
Module EXTRA_FLUID2
INTEGER :: map_status = 0
end module EXTRA_FLUID2

Compiling command:
ifort -c extra_fluid2.f90
/home/xxx/intel/oneapi/compiler/2021.4.0/linux/bin/intel64/ifort -fpic -assume 2underscore -check uninit -warn declarations -diag-error 6717 -ftz -O2 -fp-speculation=safe -fp-model=precise -fp-model=source -fimf-arch-consistency=true -qno-opt-dynamic-align -fpe0 -fomit-frame-pointer -real-size 32 -integer-size 32 -I/usr/ansys_inc/v192/CFX/include -o linux-amd64/ifort/mesh_map.o -c mesh_map.F
 -lrt/xxx/intel/oneapi/compiler/2021.4.0/linux/bin/intel64/ifort -shared -o ./linux-amd64/ifort/libmesh_map.so linux-amd64/ifort/mesh_map.o extra_fluid2.o

When I check the .so library using:
ldd -r libmesh_map.so 

The result shows as:
undefined symbol: extra_fluid2_mp_map_status__  (./libmesh_map.so)

How can I fix this? Thanks.

What OS? Is that directory in LD_LIBRARY_PATH?

What is in cfx5ext.h, and why do you USE modules iso_c_binding and ifport but make no reference to any entity from them?

It is Linux(CentOS) system. Reference to map_status is through "use extra_fluid2", so is there any need to add directory in LD_LIBRARY_PATH？

cfx5ext.h, iso_c_bindng and ifport are used for memory mapping file and other purpose. But here I remove those functions because there's no error for those parts.

You must use -assume 2underscore in both compilations, using/not using the option cannot be mixed.
Also extra_fluid2.f90 should be compiled with option -fpic. It's generally a bad idea to use different sets of flags for files that are supposed to go into the same executable or shared object.
