C++/Fortran mixing

N

NM

Hi all
I am trying to link C++ and Fortran.I am not very familiar with Fortran.
I have been successful in linking C++ and fortran using g++ and ifc for
simple program. So adding _ to at the end of fortran subroutine names when
calling from C++ is working. Now I want to move to bigger program. I have a
rather big Fortran code which compiles and links fine under ifc. Now I
changed the program main in Fortran into a subroutine main_sub so that I can
call this subroutine main_sub from C++. I am getting error in linking. For
your convenience I am pasting the output of both linking the main program in
ifc and linking the program with g++. Any help will be appreciated regarding
the undefined reference. Thanks

NM

linking the main program in ifc
------------------------------

workstation:~/1D> make
ifc -o a.out ./module/control.o ./module/typed.o ./module/data_structure1D.o
../module/h_refinements.o ./module/typehp.o ./module/hp_refinements.o
../module/frsolmod.o ./module/interpolation.o ./module/tree.o ./main/main.o
../main/main_sub.o ./main/read_control.o ./constr_util/history.o
../constr_util/history2.o ./constr_util/setcnstr.o ./data/exact.o
../data/getc.o ./data/getd.o ./data/getf.o ./data/get_values.o
../data/write_values.o ./data/get_nreles.o ./data/get_nreles2.o ./data/getg.o
../data/getmat.o ./datstrs/dumpin.o ./datstrs/dumpout.o ./datstrs/faminf.o
../datstrs/faminf2.o ./datstrs/findap.o ./datstrs/findap2.o
../datstrs/nelcon.o ./datstrs/nelcon2.o ./datstrs/nodcor.o
../datstrs/nodcor2.o ./datstrs/result.o ./datstrs/solelm2.o
../datstrs/solelm.o ./elem_util/const.o ./elem_util/shape1.o
../meshgen/allocds.o ./meshgen/deallocds.o ./meshgen/meshgen.o
../solver1/preout.o ./solver1/solin1.o ./solver1/solin2.o ./solver1/solout.o
../mysolver/mysolver.o ./solver1/solve1.o ./laplace/elem.o
../laplace/elem_info.o ./laplace/elem_matrix.o ./laplace/elem_ld.o
../laplace/elem_bc.o ./laplace/elem_error.o ./laplace/elem_error2.o
../laplace/elem_load.o ./laplace/error_contr.o ./laplace/error_contr2.o
../laplace/load_contr.o ./meshmods/break.o ./meshmods/break2.o
../meshmods/break_mdle.o ./meshmods/break_mdle2.o ./meshmods/cluster_mdle.o
../meshmods/cluster_mdle2.o ./meshmods/nodmod2.o ./meshmods/nodmod.o
../graph_1D/display_error.o ./graph_1D/graph.o ./graph_1D/graph1D.o
../graph_1D/graph1D_zoom.o ./graph_1D/rates.o ./graph_util/arctan.o
../graph_util/clip_line.o ./graph_util/clip_poly.o ./graph_util/colorset.o
../graph_util/dpbox.o ./graph_util/draw_line.o ./graph_util/finstr.o
../graph_util/initwin.o ./graph_util/mat3prod.o ./graph_util/selwin.o
../graph_interf/closwind.o ./graph_interf/clrwind.o ./graph_interf/drawline.o
../graph_interf/fillpoly.o ./graph_interf/flushx.o ./graph_interf/initcm.o
../graph_interf/openwind.o ./graph_interf/selwind.o ./graph_interf/setpost.o
../graph_interf/symbol.o ./graph_interf/wverr.o ./graph_interf/wvwarn.o
../graph_interf/inout.o ./graph_interf/psgr.o ./graph_interf/xglibf.o
../utilities/findmax.o ./utilities/locate.o ./utilities/opfil.o
../utilities/pause.o ./blas/blas.o ./blas/gausse.o ./blas/rhsub.o
../blas/tri.o ./blas/zrhsub.o ./frontsol/bckwrd.o ./frontsol/complt.o
../frontsol/dest.o ./frontsol/desvec.o ./frontsol/elmrhs.o
../frontsol/elmsol.o ./frontsol/frwcp.o ./frontsol/frwrs.o ./frontsol/locls.o
../frontsol/loclu.o ./frontsol/locr.o ./frontsol/resol.o ./frontsol/semrhs.o
../frontsol/surfsp.o ./frontsol/surfss.o ./frontsol/symasm.o
../frontsol/symelm.o ./frontsol/unsasm.o ./frontsol/unselm.o
../frontsol/zdirio.o ./frontsol/zecond.o ./frontsol/zeror.o
../frontsol/zeros.o ./frontsol/zerou.o ./adapt_hp/compute_error.o
../adapt_hp/print_nodes.o ./adapt_hp/get_values_1_compute_error.o
../adapt_hp/get_values_2_compute_error.o
../adapt_hp/write_values_compute_error.o
../adapt_hp/get_values_1_global_hpref.o
../adapt_hp/get_values_2_global_hpref.o
../adapt_hp/write_values_global_hpref.o ./adapt_hp/get_values_1_hp_elements.o
../adapt_hp/get_values_2_hp_elements.o ./adapt_hp/write_values_hp_elements.o
../adapt_hp/element1D_error.o ./adapt_hp/element_ext.o
../adapt_hp/global_hpref.o ./adapt_hp/hp_elements.o ./adapt_hp/hp_strategy.o
../adapt_hp/increase_order.o ./adapt_hp/optimize.o ./adapt_hp/precompute.o
../adapt_hp/project_element.o ./adapt_h/compute_herror.o
../adapt_h/h_strategy.o -L/usr/X11R6/lib -lX11 -lIEPCF90

No error and a.out is generated.


linking using ifc and g++
------------------------

workstation:~/temp/t/test_1d_fortran/psl_fortran.mpi> make
g++ -DMPI -g -I/u/user1/code2/lib/runtime/unified -I/u/user1/mpi-1.2.5/inc
lude -c c2_main.cpp
g++ -DMPI -g -I/u/user1/code2/lib/runtime/unified -I/u/user1/mpi-1.2.5/inc
lude -c main.cpp
mpiCC c2_main.o main.o /u/user1/ICES/1D/module/control.o
/u/user1/ICES/1D/module/typed.o /u/user1/ICES/1D/module/data_structure1D.o
/u/user1/ICES/1D/module/h_refinements.o /u/user1/ICES/1D/module/typehp.o
/u/user1/ICES/1D/module/hp_refinements.o /u/user1/ICES/1D/module/frsolmod.o
/u/user1/ICES/1D/module/interpolation.o /u/user1/ICES/1D/module/tree.o
/u/user1/ICES/1D/main/main_sub.o /u/user1/ICES/1D/main/read_control.o
/u/user1/ICES/1D/constr_util/history.o
/u/user1/ICES/1D/constr_util/history2.o
/u/user1/ICES/1D/constr_util/setcnstr.o /u/user1/ICES/1D/data/exact.o
/u/user1/ICES/1D/data/getc.o /u/user1/ICES/1D/data/getd.o
/u/user1/ICES/1D/data/getf.o /u/user1/ICES/1D/data/get_values.o
/u/user1/ICES/1D/data/write_values.o /u/user1/ICES/1D/data/get_nreles.o
/u/user1/ICES/1D/data/get_nreles2.o /u/user1/ICES/1D/data/getg.o
/u/user1/ICES/1D/data/getmat.o /u/user1/ICES/1D/datstrs/dumpin.o
/u/user1/ICES/1D/datstrs/dumpout.o /u/user1/ICES/1D/datstrs/faminf.o
/u/user1/ICES/1D/datstrs/faminf2.o /u/user1/ICES/1D/datstrs/findap.o
/u/user1/ICES/1D/datstrs/findap2.o /u/user1/ICES/1D/datstrs/nelcon.o
/u/user1/ICES/1D/datstrs/nelcon2.o /u/user1/ICES/1D/datstrs/nodcor.o
/u/user1/ICES/1D/datstrs/nodcor2.o /u/user1/ICES/1D/datstrs/result.o
/u/user1/ICES/1D/datstrs/solelm2.o /u/user1/ICES/1D/datstrs/solelm.o
/u/user1/ICES/1D/elem_util/const.o /u/user1/ICES/1D/elem_util/shape1.o
/u/user1/ICES/1D/meshgen/allocds.o /u/user1/ICES/1D/meshgen/deallocds.o
/u/user1/ICES/1D/meshgen/meshgen.o /u/user1/ICES/1D/solver1/preout.o
/u/user1/ICES/1D/solver1/solin1.o /u/user1/ICES/1D/solver1/solin2.o
/u/user1/ICES/1D/solver1/solout.o /u/user1/ICES/1D/mysolver/mysolver.o
/u/user1/ICES/1D/solver1/solve1.o /u/user1/ICES/1D/laplace/elem.o
/u/user1/ICES/1D/laplace/elem_info.o /u/user1/ICES/1D/laplace/elem_matrix.o
/u/user1/ICES/1D/laplace/elem_ld.o /u/user1/ICES/1D/laplace/elem_bc.o
/u/user1/ICES/1D/laplace/elem_error.o /u/user1/ICES/1D/laplace/elem_error2.o
/u/user1/ICES/1D/laplace/elem_load.o /u/user1/ICES/1D/laplace/error_contr.o
/u/user1/ICES/1D/laplace/error_contr2.o
/u/user1/ICES/1D/laplace/load_contr.o /u/user1/ICES/1D/meshmods/break.o
/u/user1/ICES/1D/meshmods/break2.o /u/user1/ICES/1D/meshmods/break_mdle.o
/u/user1/ICES/1D/meshmods/break_mdle2.o
/u/user1/ICES/1D/meshmods/cluster_mdle.o
/u/user1/ICES/1D/meshmods/cluster_mdle2.o
/u/user1/ICES/1D/meshmods/nodmod2.o /u/user1/ICES/1D/meshmods/nodmod.o
/u/user1/ICES/1D/graph_1D/display_error.o /u/user1/ICES/1D/graph_1D/graph.o
/u/user1/ICES/1D/graph_1D/graph1D.o /u/user1/ICES/1D/graph_1D/graph1D_zoom.o
/u/user1/ICES/1D/graph_1D/rates.o /u/user1/ICES/1D/graph_util/arctan.o
/u/user1/ICES/1D/graph_util/clip_line.o
/u/user1/ICES/1D/graph_util/clip_poly.o
/u/user1/ICES/1D/graph_util/colorset.o /u/user1/ICES/1D/graph_util/dpbox.o
/u/user1/ICES/1D/graph_util/draw_line.o /u/user1/ICES/1D/graph_util/finstr.o
/u/user1/ICES/1D/graph_util/initwin.o /u/user1/ICES/1D/graph_util/mat3prod.o
/u/user1/ICES/1D/graph_util/selwin.o
/u/user1/ICES/1D/graph_interf/closwind.o
/u/user1/ICES/1D/graph_interf/clrwind.o
/u/user1/ICES/1D/graph_interf/drawline.o
/u/user1/ICES/1D/graph_interf/fillpoly.o
/u/user1/ICES/1D/graph_interf/flushx.o
/u/user1/ICES/1D/graph_interf/initcm.o
/u/user1/ICES/1D/graph_interf/openwind.o
/u/user1/ICES/1D/graph_interf/selwind.o
/u/user1/ICES/1D/graph_interf/setpost.o
/u/user1/ICES/1D/graph_interf/symbol.o /u/user1/ICES/1D/graph_interf/wverr.o
/u/user1/ICES/1D/graph_interf/wvwarn.o /u/user1/ICES/1D/graph_interf/inout.o
/u/user1/ICES/1D/graph_interf/psgr.o /u/user1/ICES/1D/graph_interf/xglibf.o
/u/user1/ICES/1D/utilities/findmax.o /u/user1/ICES/1D/utilities/locate.o
/u/user1/ICES/1D/utilities/opfil.o /u/user1/ICES/1D/utilities/pause.o
/u/user1/ICES/1D/blas/blas.o /u/user1/ICES/1D/blas/gausse.o
/u/user1/ICES/1D/blas/rhsub.o /u/user1/ICES/1D/blas/tri.o
/u/user1/ICES/1D/blas/zrhsub.o /u/user1/ICES/1D/frontsol/bckwrd.o
/u/user1/ICES/1D/frontsol/complt.o /u/user1/ICES/1D/frontsol/dest.o
/u/user1/ICES/1D/frontsol/desvec.o /u/user1/ICES/1D/frontsol/elmrhs.o
/u/user1/ICES/1D/frontsol/elmsol.o /u/user1/ICES/1D/frontsol/frwcp.o
/u/user1/ICES/1D/frontsol/frwrs.o /u/user1/ICES/1D/frontsol/locls.o
/u/user1/ICES/1D/frontsol/loclu.o /u/user1/ICES/1D/frontsol/locr.o
/u/user1/ICES/1D/frontsol/resol.o /u/user1/ICES/1D/frontsol/semrhs.o
/u/user1/ICES/1D/frontsol/surfsp.o /u/user1/ICES/1D/frontsol/surfss.o
/u/user1/ICES/1D/frontsol/symasm.o /u/user1/ICES/1D/frontsol/symelm.o
/u/user1/ICES/1D/frontsol/unsasm.o /u/user1/ICES/1D/frontsol/unselm.o
/u/user1/ICES/1D/frontsol/zdirio.o /u/user1/ICES/1D/frontsol/zecond.o
/u/user1/ICES/1D/frontsol/zeror.o /u/user1/ICES/1D/frontsol/zeros.o
/u/user1/ICES/1D/frontsol/zerou.o /u/user1/ICES/1D/adapt_hp/compute_error.o
/u/user1/ICES/1D/adapt_hp/print_nodes.o
/u/user1/ICES/1D/adapt_hp/get_values_1_compute_error.o
/u/user1/ICES/1D/adapt_hp/get_values_2_compute_error.o
/u/user1/ICES/1D/adapt_hp/write_values_compute_error.o
/u/user1/ICES/1D/adapt_hp/get_values_1_global_hpref.o
/u/user1/ICES/1D/adapt_hp/get_values_2_global_hpref.o
/u/user1/ICES/1D/adapt_hp/write_values_global_hpref.o
/u/user1/ICES/1D/adapt_hp/get_values_1_hp_elements.o
/u/user1/ICES/1D/adapt_hp/get_values_2_hp_elements.o
/u/user1/ICES/1D/adapt_hp/write_values_hp_elements.o
/u/user1/ICES/1D/adapt_hp/element1D_error.o
/u/user1/ICES/1D/adapt_hp/element_ext.o
/u/user1/ICES/1D/adapt_hp/global_hpref.o
/u/user1/ICES/1D/adapt_hp/hp_elements.o
/u/user1/ICES/1D/adapt_hp/hp_strategy.o
/u/user1/ICES/1D/adapt_hp/increase_order.o
/u/user1/ICES/1D/adapt_hp/optimize.o /u/user1/ICES/1D/adapt_hp/precompute.o
/u/user1/ICES/1D/adapt_hp/project_element.o
/u/user1/ICES/1D/adapt_h/compute_herror.o
/u/user1/ICES/1D/adapt_h/h_strategy.o -L/u/user1/code2/lib -L/usr/X11R6/lib
-lX11 -L/lusr/share/software/intel/compiler70/ia32/lib -lIEPCF90 -lcode_uni
fied -o psl_fortran -lm -lpthread
/u/user1/ICES/1D/graph_1D/rates.o: In function `rates':
graph_1D/rates.f:118: undefined reference to `f_powdd'
graph_1D/rates.f:118: undefined reference to `f_powdd'
graph_1D/rates.f:155: undefined reference to `f_powdd'
graph_1D/rates.f:157: undefined reference to `f_powdd'
graph_1D/rates.f:157: undefined reference to `f_powdd'
/u/user1/ICES/1D/graph_1D/rates.o:graph_1D/rates.f:157: more undefined
references to `f_powdd' follow
/u/user1/ICES/1D/frontsol/dest.o: In function `dest':
frontsol/dest.f:126: undefined reference to `f_ldnint_val'
frontsol/dest.f:179: undefined reference to `f_ldnint_val'
frontsol/dest.f:180: undefined reference to `f_ldnint_val'
frontsol/dest.f:181: undefined reference to `f_ldnint_val'
/u/user1/ICES/1D/frontsol/symelm.o: In function `symelm':
frontsol/symelm.f:56: undefined reference to `f_ldnint_val'
/lusr/share/software/intel/compiler70/ia32/lib/libIEPCF90.so.3: undefined
reference to `a_qtoi'
/lusr/share/software/intel/compiler70/ia32/lib/libIEPCF90.so.3: undefined
reference to `a_eqq'
/lusr/share/software/intel/compiler70/ia32/lib/libIEPCF90.so.3: undefined
reference to `a_geq'
/lusr/share/software/intel/compiler70/ia32/lib/libIEPCF90.so.3: undefined
reference to `a_divq'
/lusr/share/software/intel/compiler70/ia32/lib/libIEPCF90.so.3: undefined
reference to `a_addq'
/lusr/share/software/intel/compiler70/ia32/lib/libIEPCF90.so.3: undefined
reference to `a_dtoq'
/lusr/share/software/intel/compiler70/ia32/lib/libIEPCF90.so.3: undefined
reference to `a_ftoq'
/lusr/share/software/intel/compiler70/ia32/lib/libIEPCF90.so.3: undefined
reference to `a_mulq'
/lusr/share/software/intel/compiler70/ia32/lib/libIEPCF90.so.3: undefined
reference to `a_subq'
/lusr/share/software/intel/compiler70/ia32/lib/libIEPCF90.so.3: undefined
reference to `a_neq'
/lusr/share/software/intel/compiler70/ia32/lib/libIEPCF90.so.3: undefined
reference to `a_ltq'
/lusr/share/software/intel/compiler70/ia32/lib/libIEPCF90.so.3: undefined
reference to `a_leq'
/lusr/share/software/intel/compiler70/ia32/lib/libIEPCF90.so.3: undefined
reference to `a_itoq'
/lusr/share/software/intel/compiler70/ia32/lib/libIEPCF90.so.3: undefined
reference to `a_floorq'
collect2: ld returned 1 exit status
make: *** [psl_fortran] Error 1
workstation:~/temp/t/test_1d_fortran/psl_fortran.mpi>
 
N

NM

I forgot to post lines from rates.f and dest.f
rates.f:118 dx = x_max**alpha - x_min**alpha
dest.f:126 l = iabs(nint(Amdest(km-1)))
 
E

E. Robert Tisdale

NM said:
I am trying to link C++ and Fortran.I am not very familiar with Fortran.
I have been successful in linking C++ and fortran using g++ and ifc for
simple program. So adding _ to at the end of fortran subroutine names when
calling from C++ is working. Now I want to move to bigger program. I have a
rather big Fortran code which compiles and links fine under ifc. Now I
changed the program main in Fortran into a subroutine main_sub so that I can
call this subroutine main_sub from C++. I am getting error in linking. For
your convenience I am pasting the output of both linking the main program in
ifc and linking the program with g++. Any help will be appreciated regarding
the undefined reference.

[snip]

Everything looks OK
except that the link editor ld can't resolve some references.
Most likely this is because you have neglected to include
a [Fortran] library which defines these references.
Your Fortran compiler ifc knows which libraries are required
but your C++ compiler mpiCC does not.

cd to a directory containing your Fortran libraries

/lusr/share/software/intel/compiler70/ia32/lib/

for example and type

nm -o *.a *.so *.so.3 | grep a_floorq

You should get a list of references to a_floorq.
The ones labeled U are undefined but those labeled T are defined.
Just include the corresponding library along with

-lIEPCF90 -lcode_unified

in your Makefile.
 
N

NM

Thanks. It is working :)

E. Robert Tisdale said:
NM said:
I am trying to link C++ and Fortran.I am not very familiar with Fortran.
I have been successful in linking C++ and fortran using g++ and ifc for
simple program. So adding _ to at the end of fortran subroutine names when
calling from C++ is working. Now I want to move to bigger program. I have a
rather big Fortran code which compiles and links fine under ifc. Now I
changed the program main in Fortran into a subroutine main_sub so that I can
call this subroutine main_sub from C++. I am getting error in linking. For
your convenience I am pasting the output of both linking the main program in
ifc and linking the program with g++. Any help will be appreciated regarding
the undefined reference.

[snip]

Everything looks OK
except that the link editor ld can't resolve some references.
Most likely this is because you have neglected to include
a [Fortran] library which defines these references.
Your Fortran compiler ifc knows which libraries are required
but your C++ compiler mpiCC does not.

cd to a directory containing your Fortran libraries

/lusr/share/software/intel/compiler70/ia32/lib/

for example and type

nm -o *.a *.so *.so.3 | grep a_floorq

You should get a list of references to a_floorq.
The ones labeled U are undefined but those labeled T are defined.
Just include the corresponding library along with

-lIEPCF90 -lcode_unified

in your Makefile.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top