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:

  • In the C function, change the name of the function by appending an underscore to that name
  • Use the C() pragma to tell the Fortran compiler to omit those trailing underscores.
  • Use the f95 -ext_names option to compile references to external names without underscores.

Use only one of these solutions.

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

  • compile f90 linda.f90
  • run ./a.out
    Current_time 1103814443
    Current_time 1103814443
    SEED(1) 340799264 SEED(2) 2018393530

For SS9 , the docs said the followings:
There are three usual solutions to the underscore problem:

  • In the C function, change the name of the function by appending an underscore to that name.
  • Use the BIND(C) attribute declaration to indicate that an external function is a C language function.
  • Use the f95 -ext_names option to compile references to external names without underscores.

Use only one of these solutions.
The examples in this chapter could use the BIND(C) attribute declaration to avoid underscores. BIND(C) declares the C external functions that can be called from Fortran, and the Fortran routines that can be called from C as arguments. The Fortran compiler does not append an underscore as it ordinarily does with external names. The BIND(C) must appear in each subprogram that contains such a reference. The conventional usage is:
FUNCTION ABC EXTERNAL XYZ
BIND(C) ABC, XYZ
Here the user has specified not only that XYZ is an external C function, but that the Fortran caller, ABC, should be callable from a C function. If you use BIND(C), the C function does not need an underscore appended to the function name
so I make the similar change and it compile and run, just like the FD7 case.
PROGRAM use_srand
INTEGER*4 :: SEED(2),time,current_time,irand
EXTERNAL srand , irand
BIND(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

About laotsao 老曹

HopBit GridComputing LLC Rockscluster Gridengine Solaris Zone, Solaris Cluster, OVM SPARC/Ldom Exadata, SPARC SuperCluster
This entry was posted in Sun Studio. Bookmark the permalink.

One Response to mixing C library in Fortran

  1. 窃听器 says:

    提供<a title=”窃听器” href=”http://www.peian.net”>窃听器,非法买卖/使用手机窃听器等器材去窃听别人隐私是违法行为. 日前,一度闹得沸沸扬扬的“有中国军方背景的公司携美国华商暗助萨达姆案”终于有了结果——美国法庭 … 他们在黄姓商人的家里安装了窃听器,对他实施跟踪,并且在他家附近的社区安装了监视摄像头。
    提供<a title=”手机窃听器” href=”http://www.peian.net”>手机窃听器,2007年1月【福特电子】最新推出美国原装F-6超薄翻盖旋转手写全能手机窃听器以及F-8超小型笔记本式手机窃听器等,我公司在全国个地都有销售网点,大多数地区5个小时以内就可以【免费送货】快速将商品送达顾客手中,购买任一款手机窃听器,赠送无线隐型耳机
    提供<a title=”窃听器” href=”http://www.peian.net/index4.htm”>窃听器,在一家专门卖手机窃听器的网站上,记者发现一个名称为福尔摩斯王600E的最新款式的窃听器,由国外生产,价格是9800元。其他款式型号的窃听器价格从几百到几千元不等。
    提供<a title=”手机窃听器” href=”http://www.peian.net/index4.htm”>手机窃听器,在网上搜索“手机窃听器”,居然得到了47.1万个搜索结果,其受欢迎程度可见一斑。在一家名为“深圳神探电子监控科技开发有限公司”的网页 … 记者发现,在网上叫卖手机窃听器的公司大多还销售其他窃听器材,其产品大部分来自美国,使用的是卫星定位系统。

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s