Today's Page Hits: 201
This page validates as XHTML 1.0, and will look much better in a browser that supports web standards, but it is accessible to any browser or Internet device. It was created using techniques detailed at glish.com/css/.
mixing C library in Fortran
recently LK send me an query from customer about calling C library in Fortran
question is the following
We have a f90 code, which uses rand, irand, srand fnctions a lot. That code works fine on other platform, e.g., SP, but on the Sun workstation, the code cannot be compiled because of no_found srand. Enclosed below please find a small example that I created just for demonstrating the problem. f90 -xpp=cpp use_srand.f90 -lc -lm gives this error: Undefined first referenced symbol in file srand_ use_srand.o ld: fatal: Symbol referencing errors. No output written to a.out Is there any tricky stuff in calling C library?
PROGRAM use_srand
INTEGER*4 :: SEED(2),time,current_time,irand
EXTERNAL srand, irand
current_time=time()
print*,' Current_time ', current_time
call srand(current_time)
print*,' Current_time ', current_time
SEED(1)=irand(current_time)
SEED(2)=irand(current_time+1)
print*,' SEED(1) ', SEED(1), ' SEED(2) ', SEED(2)
stop
end
Since I donot work with codes everyday so I look it up on http://docs.sun.com/ Sun have many different version of compiler, but in each version there are instruction about calling C library in Fortran
I also have two internal machines to test the codes, one has FD7 compiler one has SS9
FD7 Fortran reference manul chapter11
11.1.4 Underscores in Routine Names
The Fortran compiler normally appends an underscore (_) to the names of subprograms appearing both at entry point definition and in calls. This convention differs from C procedures or external variables with the same user-assigned name. Almost all Fortran library procedure names have double leading underscores to reduce clashes with user-assigned subroutine names.
There are three usual solutions to the underscore problem:
The examples in this chapter could use the C() compiler pragma to avoid underscores. The C() pragma directive takes the names of external functions as arguments. It specifies that these functions are written in the C language, so the Fortran compiler does not append an underscore as it ordinarily does with external names. The C()directive for a particular function must appear before the first reference to that function. It must also appear in each subprogram that contains such a reference. The conventional usage is:
EXTERNAL ABC, XYZ !$PRAGMA C( ABC, XYZ )
If you use this pragma, the C function does not need an underscore appended to the function name. (Pragma directives are described in the Fortran User's Guide
I make the following change
PROGRAM use_srand
INTEGER*4 :: SEED(2),time,current_time,irand
EXTERNAL srand , irand
!$PRAGMA C(srand)
current_time=time()
print*,' Current_time ', current_time
call srand(current_time)
print*,' Current_time ', current_time
SEED(1)=irand(current_time)
SEED(2)=irand(current_time+1)
print*,' SEED(1) ', SEED(1), ' SEED(2) ', SEED(2)
stop
end
For SS9 , the docs said the followings:
There are three usual solutions to the underscore problem:
Posted at 09:54AM Dec 23, 2004 by hstsao in Sun Studio | Comments[1]
Posted by 窃听器 on March 25, 2007 at 07:57 PM EDT #