linking problem_an ambiguous reference


green_ray
11-02-2009, 02:30 PM
Hi there,

I am trying to compile very simple code from xzroots but have got the error like this,

"Error: Name 'zroots' at (1) is an ambiguous reference to 'zroots' from module 'nr' "

Does someboby have any idea why it happens?
The example code for zroots from Numerical Recipes and I don't see any problem with it..
Reply With Quote

davekw7x
11-03-2009, 05:12 PM
...I am trying to compile...
1. What compiler are you using? What Operating System? How, exactly, are you compiling it? (Command line?)

2. Have you compiled any other programs that use the nr module?

3. What, exactly, are you trying to compile? Is it the (unmodified) program xzroots.f90 that is supplied with the NR distribution? If it is not, then can you post the exact code that you are using?


Regards,

Dave

green_ray
11-04-2009, 01:07 PM
Hi Dave,

[QUOTE=davekw7x;4168]1. What compiler are you using? What Operating System? How, exactly, are you compiling it? (Command line?)

I am using gfortran in Suse Linux but this problem happens in another systems like a Unix..I compile it by makefile so could you have a look the attacehd file?

2. Have you compiled any other programs that use the nr module?

I have had no problem using nr module at all.

3. What, exactly, are you trying to compile? Is it the (unmodified) program xzroots.f90 that is supplied with the NR distribution? If it is not, then can you post the exact code that you are using?

I did compile the xzroots.f90, however, only a bit modified such as using a separeted module, Drive4zroots.f90 which is for subroutines, zroots and laguer. I also attached this file..

All files are uploaded as text files.

Thank you in advance:)

Rock

davekw7x
11-04-2009, 02:59 PM
Hi Dave
Hi Rock.

The "ambiguous reference" message happens because there is a subroutine named zroots in the nr module and there is a subroutine named zroots in your Drive4roots module.

One possible fix is to rename your subroutine to something else.

Another possibility would be to make sure the linker doesn't see the zroots subroutine in the nr module. I don't see anything in your xzroots.f90 that needs anything in nr, so you can just delete the use nr statement (or comment it out). If you change xzroots so that it does use something from the nr module, you can always put the ONLY stuff in the use nr statement as you did in your Drive4roots.f90 file.

Regards,

Dave

Footnote: Instead of including laguer.f90 in Drive4roots.f90, I would probably put explicit dependencies in the Makefile. So, if you comment out the include 'laguer.f90' line the Makefile could look something like:

FC = gfortran
FCFLAGS = -Wall -W -pedantic

# Your directories
#PROGDIR = /home/rockhee
#NRDIR = $(PROGDIR)/Numerical_Recipes/recipes_f-90/recipes

# My directories
PROGDIR = /home/dave/nr3_legacy/nr2/f90_210/xzroots
NRDIR = /home/dave/nr3_legacy/nr2/f90_210/recipes
#
OBJECTS = nrtype.o \
nr.o \
nrutil.o \
laguer.o \
indexx.o \
Drive4roots.o \
xzroots.o
xzroots : $(OBJECTS)
$(FC) -o xzroots $(OBJECTS)

nrtype.o : $(NRDIR)/nrtype.f90
$(FC) -c $(NRDIR)/nrtype.f90

nr.o : $(NRDIR)/nr.f90 nrtype.o
$(FC) -c $(NRDIR)/nr.f90

nrutil.o : $(NRDIR)/nrutil.f90 nrtype.o
$(FC) -c $(NRDIR)/nrutil.f90

laguer.o : $(NRDIR)/laguer.f90
$(FC) -c $(NRDIR)/laguer.f90

indexx.o : $(NRDIR)/indexx.f90
$(FC) -c $(NRDIR)/indexx.f90


Drive4roots.o: Drive4roots.f90 nrtype.o nr.o nrutil.o
$(FC) $(FCFLAGS) -c Drive4roots.f90

xzroots.o : xzroots.f90 nrtype.o nr.o nrutil.o Drive4roots.o
$(FC) $(FCFLAGS) -c xzroots.f90

clean:
rm -f *.mod *.o xzroots


or some such thing.

green_ray
11-05-2009, 11:45 AM
Hi Dave,

thank you for your kind reply and it really helps me!

One possible fix is to rename your subroutine to something else.

Now, I understand the reason for "ambiguous reference" thing and I gave different name for zroots, however, it gives me the other error such like "Segmentation fault":(
I am using same example which Numerical Recipes supplies and I don't see any problem from input initial conditions..Do you have any idea where the problem comes from?

And i am also using the makefile as your advice.

Rock

davekw7x
11-05-2009, 02:07 PM
...
Now, I understand the reason for "ambiguous reference" thing and I gave different name for zroots,...

I went into your Drive4roots.f90 and changed "zroots" to "myzroots" everywhere in that file.

I used the following xzroots.f90:

PROGRAM xmyzroots
! driver for routine myzroots
USE nrtype
USE nr
USE Drive4roots
IMPLICIT NONE
INTEGER(I4B), PARAMETER :: M=4,M1=M+1
INTEGER(I4B) :: i
COMPLEX(SP), DIMENSION(M) :: roots
COMPLEX(SP), DIMENSION(M1) :: a = (/ (0.0_sp,2.0_sp),(0.0_sp,0.0_sp),&
(-1.0_sp,-2.0_sp),(0.0_sp,0.0_sp),(1.0_sp,0.0_sp) /)
LOGICAL(LGT) :: polish
write(*,'(/1x,a)') 'Roots of the polynomial x^4-(1+2i)*x^2+2i'
polish=.false.
call myzroots(a,roots,polish)
write(*,'(1x,t10,a,t25,a,t37,a)') 'Root #','Real','Imag.'
do i=1,M
write(*,'(1x,i11,5x,2f12.6)') i,roots(i)
end do

END PROGRAM xmyzroots


Then, using the Makefile from my previous post, I executed make clean && make && ./xzroots from a command line and here is the result:


rm -f *.mod *.o xzroots
gfortran -c /home/dave/nr3_legacy/nr2/f90_210/recipes/nrtype.f90
gfortran -c /home/dave/nr3_legacy/nr2/f90_210/recipes/nr.f90
gfortran -c /home/dave/nr3_legacy/nr2/f90_210/recipes/nrutil.f90
gfortran -c /home/dave/nr3_legacy/nr2/f90_210/recipes/laguer.f90
gfortran -c /home/dave/nr3_legacy/nr2/f90_210/recipes/indexx.f90
gfortran -Wall -W -pedantic -c Drive4roots.f90
gfortran -Wall -W -pedantic -c xzroots.f90
gfortran -o xzroots nrtype.o nr.o nrutil.o laguer.o indexx.o Drive4roots.o xzroots.o

Roots of the polynomial x^4-(1+2i)*x^2+2i
Root # Real Imag.
1 -1.000000 -1.000000
2 -1.000000 0.000000
3 1.000000 1.000000
4 1.000000 0.000000


Regards,

Dave

Footnote: My system: gfortran version 4.1.2 on Centos 5.4

green_ray
11-07-2009, 11:41 AM
Hi Dave,

I appreciate all of your comments and it does work now!
However, one strange thing is it works well in Unix server but not in Linux machine. I compiled same files by same makefile.. Do you have any idea why it is like this?

Cheers,
Rock

davekw7x
11-07-2009, 01:17 PM
...but not in Linux machine. ...

What do you mean?

Did it not compile (were there any compiler messages)? Did it not run? Did it crash? Did it run without crashing but it gave the wrong output? What?

What Linux distribution are you using?


I will recapitulate my experience with Centos version 5.4 Linux (with GNU gfortran version 4.1.2):

I used nrtype.f90, nr.f90, nrutil.90, laguer.f90, and indexx.f90 from the NR3 CD distribution (in the legacy/nr2/f90_210/recipes directory). I used your Drive4roots.f90 (modified by changing zroots to myzroots everywhere in that file), and I used the xzroots.f90 file and Makefile from my previous post.

Was your "make" output exactly the same as I showed?


If the "make" was successful, show us what happened when you executed "./xzroots"


Regards,

Dave

green_ray
11-08-2009, 10:21 AM
It does work in Unix system, Sun Os which shows like this:
rm -f *.mod *.o xzroots
argo spxrs2 57: make
gfortran -c /home/user/spxrs2/Rockhee/recipes/nrtype.f90
gfortran -c /home/user/spxrs2/Rockhee/recipes/nr.f90
gfortran -c /home/user/spxrs2/Rockhee/recipes/nrutil.f90
gfortran -c /home/user/spxrs2/Rockhee/recipes/laguer.f90
gfortran -c /home/user/spxrs2/Rockhee/recipes/indexx.f90
gfortran -Wall -W -pedantic -c Drive4roots.f90
gfortran -Wall -W -pedantic -c xzroots.f90
gfortran -o xzroots nrtype.o nr.o nrutil.o laguer.o indexx.o Drive4roots.o xzroots.o
argo spxrs2 58: !.
./xzroots

Roots of the polynomial x^4-(1+2i)*x^2+2i
Root # Real Imag.
1 -1.000000 -1.000000
2 -1.000000 0.000000
3 1.000000 1.000000
4 1.000000 0.000000

------------------------------------------------
However, in Linux, Suse Linux 10.0 with GNU (gcc version 4.0.2) it shows the error such as:
----------------------------------------------
./xzroots

Roots of the polynomial x^4-(1+2i)*x^2+2i
Segmentation fault
------------------------------------------------

I am using same files but in different system..

Regards,
Rock

davekw7x
11-08-2009, 10:56 AM
...
I am using same files but in different system..So, did the "make" output look the same?

Since it seems to be crashing after calling myzroots, why not put some print messages inside the subprogram to see how far it is getting?

For example, near the beginning of the myzroots subprogram:

ad(:)=a(:)

write(*,'(" In myzroots: size(a) = ",I1)')size(a)
do j=1,size(a)
write(*,'(" a(",I1,") = ",2(1pe14.6)))')j, a(j)
enddo

do j=m,1,-1


Or some such thing.

Here's what it showed me (gfortran version 4.1.2):


Roots of the polynomial x^4-(1+2i)*x^2+2i
In myzroots: size(a) = 5
a(1) = 0.000000E+00 2.000000E+00
a(2) = 0.000000E+00 0.000000E+00
a(3) = -1.000000E+00 -2.000000E+00
a(4) = 0.000000E+00 0.000000E+00
a(5) = 1.000000E+00 0.000000E+00
.
.
.


Now if it gets this far and the output is correct, but it still crashes, the put print statements wherever it calls another subprogram:


do j=m,1,-1
x=cmplx(0.0_sp,kind=spc)
write(*,'(" 1: Calling laguer.")')
call laguer(ad(1:j+1),x,its)
.
.
.
roots(j)=x
write(*,'(" 2: Calling poly_term.")')
ad(j:1:-1)=poly_term(ad(j+1:2:-1),x)
.
.
.
if (polish) then
do j=1,m
write(*,'(" 3: Calling laguer.")')
call laguer(a(:),roots(j),its)
.
.
.
write(*,'(" 4: Calling indexx.")')
call indexx(real(roots),indx)
write(*,'(" 5: Calling roots.")')
roots=roots(indx)
write(*,'(" 6: Returning from myroots.")')
END SUBROUTINE myzroots

Or some such thing.

Here's what I got from the run:

Roots of the polynomial x^4-(1+2i)*x^2+2i
In myzroots: size(a) = 5
a(1) = 0.000000E+00 2.000000E+00
a(2) = 0.000000E+00 0.000000E+00
a(3) = -1.000000E+00 -2.000000E+00
a(4) = 0.000000E+00 0.000000E+00
a(5) = 1.000000E+00 0.000000E+00
1: Calling laguer.
2: Calling poly_term.
1: Calling laguer.
2: Calling poly_term.
1: Calling laguer.
2: Calling poly_term.
1: Calling laguer.
2: Calling poly_term.
4: Calling indexx.
5: Calling roots.
6: Returning from myroots.
Root # Real Imag.
1 -1.000000 -1.000000
2 -1.000000 0.000000
3 1.000000 1.000000
4 1.000000 0.000000

It's a brute-force technique, but I can't duplicate your setup and your version of Fortran and libraries. Maybe it will give you some clues. If you find it is crashing inside one of the subprograms, you can drill deeper.

Regards,

Dave

green_ray
11-14-2009, 01:15 PM
Thank you very much, Dave.

I think I found out what problem it was!

Rock