linking fortran and C++: underscore problem

22 views
Skip to first unread message

Jean-Claude Passy

unread,
Oct 30, 2015, 7:26:19 AM10/30/15
to enzo...@googlegroups.com
Hi all,

I apologize in advance as my question is not Enzo related, but given the expertise some people here have and that it will eventually involve Enzo, I thought I would just ask.

My goal is to use the tabulated EoS from the stellar evolution code MESA into Enzo. The difficulty is MESA is written in fortran77, so I need to do some funky C++/F77 linking.

I am really at the first step, where I use a fortran module from MESA (module_eos.f), which I try to call in a C++ program (main_cpp.C). As a test, I also called the module in a Fortran program (main_fortran.f) to check that everything was working correctly before involving C++.

For the moment, I use the GNU distribution provided by the MESA SDK (http://www.astro.wisc.edu/~townsend/static.php?ref=mesasdk) which is the one I used to build the MPI compilers and build Enzo. 

When I build everything, I get the following error at linking:

#------------------------------------------------------------------------------------------------------------------------------------------------------
[12:18:07] Obiwan:$ make
/Applications/mesasdk/bin/gfortran -ffixed-form -ffixed-line-length-132 -x f77-cpp-input -o module_eos.o -I/Users/jcpassy/Work/MESA/mesa_7503/include -c module_eos.f
/Applications/mesasdk/bin/g++ -c -fopenmp -L/Users/jcpassy/Work/MESA/mesa_7503/lib -lstar -lionization -latm -lcolors -lmlt -lnse -lnet -leos -lkap -lrates -lneu -lchem -linterp_2d -linterp_1d -lnum -lf2crlibm -lcrlibm -lmtx -lconst -lutils -lmesaklu `mesasdk_lapack_link` `mesasdk_blas_link` `mesasdk_pgplot_link` `mesasdk_se_link`  -lgfortran main_cpp.C
g++.exec: warning: /Applications/mesasdk/lib/liblapack.a: linker input file unused because linking not done
g++.exec: warning: /Applications/mesasdk/lib/libxblas.a: linker input file unused because linking not done
g++.exec: warning: /Applications/mesasdk/lib/libblas.a: linker input file unused because linking not done
g++.exec: warning: /Applications/mesasdk/lib/libblas.a: linker input file unused because linking not done
/Applications/mesasdk/bin/g++ -o bin/main_cpp.exe -fopenmp -L/Users/jcpassy/Work/MESA/mesa_7503/lib -lstar -lionization -latm -lcolors -lmlt -lnse -lnet -leos -lkap -lrates -lneu -lchem -linterp_2d -linterp_1d -lnum -lf2crlibm -lcrlibm -lmtx -lconst -lutils -lmesaklu `mesasdk_lapack_link` `mesasdk_blas_link` `mesasdk_pgplot_link` `mesasdk_se_link`  -lgfortran \
main_cpp.o module_eos.o
Undefined symbols for architecture x86_64:
  "_Sample", referenced from:
      _main in main_cpp.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
make: *** [all] Error 1
#------------------------------------------------------------------------------------------------------------------------------------------------------

The problem seems to be in adding an extra underscore to the function name. I know this is a common issue, and Enzo deals with that with the macros FORTRAN_NAME and PFORTRAN_NAME. However, I tried for almost a day, but could not figure it out. 

I would be happy if someone could help me to solve this problem. Once it is done, I will try to compile these files with the MPI compilers, and include them in Enzo.

Thanks a ton,

JC



PS: I am on OSX 10.10, GNU compilers v. 4.9.3.


main_cpp.C
main_fortran.f
module_eos.f
Makefile

Jean-Claude Passy

unread,
Nov 2, 2015, 4:46:26 AM11/2/15
to Jean-Claude Passy, enzo-...@googlegroups.com, enzo...@googlegroups.com
Hi all,

for posterity, I should say that the problem was that the Fortran subroutine “sample” was part of the module “sample_eos”. As shown in the object file, the C++ program should therefore call “sample_eos_MOD_sample” instead of simply “sample”:

#--------------------------------------------------------------
[10:42:49] Obiwan:$ nm module_eos.o
00000000000007d2 T ___sample_eos_MOD_sample
#--------------------------------------------------------------

Cheers,

JC


<main_cpp.C>
<main_fortran.f>
<module_eos.f>
<Makefile>




Reply all
Reply to author
Forward
0 new messages