Interoperability with C in Fortran 2003
A. Donev
{"title":"Interoperability with C in Fortran 2003","authors":"A. Donev","doi":"10.1145/1124708.1124710","DOIUrl":null,"url":null,"abstract":"One of the major new features in the Fortran 2003 is features for interoperability with C © Interop). The intrinsic module ISO_C_BINDING provides:• constants, mostly type parameters, C_NULL_CHAR, C_NULL_PTR, and others,• types, and in particular, TYPE(C_PTR) and TYPE(C_FUNPTR),• procedures, such as C_LOC, C_FUNLOC, C_F_POINTER, C_F_PROCPOINTER and C_ASSOCIATED.A Fortran interface can be specified for a C function with external linkage and used to invoke such a function. The interface has the characteristic BIND(C) label, and must also satisfy some additional restrictions.C Interop can be used to portably use multi-language codes in Fortran. Since most languages interoperate with C, the feature can actually be used to interoperate with other programming languages as well. C Interop can also be used to give access to Fortran programmers to the many standard libraries with widely-used and implemented C interfaces. This includes lower-level tasks such as interfacing with the OS on UNIX-based systems, or using special libraries like OpenGL.For simple API's, developing Fortran interfaces is practically trivial once one gets some experience. For more complicated API's whose full functionality/power is not needed, such as for example TCP/IP sockets or shared-memory segments on UNIX systems, it is often easier to develop a condensed C API/library that does the actual work, and is simpler to interface to from Fortran. However, for libraries like OpenGL, one should provide a full Fortran interface so that the whole API can be accessed. Doing this manually is not easy and is also error-prone due to the size of the OpenGL/GLU/GLUT interfaces. For certain libraries like MPI, a special Fortran interface may be defined for the purposes of efficiency, portability, ease-of-use, or to accommodate for language semantic differences.In this first paper, we will show how to develop a Fortran interface for a simple C API/library. In a second paper, we consider automating the process so that large and more complex API's, and in particular, OpenGL, can be handled. The source codes can be obtained at http://atom.princeton.edu/donev/F2x.Along the way, we identify some problems with the design of C Interop in Fortran 2003.","PeriodicalId":379614,"journal":{"name":"ACM SIGPLAN Fortran Forum","volume":"194 1","pages":"0"},"PeriodicalIF":0.0000,"publicationDate":"2006-04-01","publicationTypes":"Journal Article","fieldsOfStudy":null,"isOpenAccess":false,"openAccessPdf":"","citationCount":"2","resultStr":null,"platform":"Semanticscholar","paperid":null,"PeriodicalName":"ACM SIGPLAN Fortran Forum","FirstCategoryId":"1085","ListUrlMain":"https://doi.org/10.1145/1124708.1124710","RegionNum":0,"RegionCategory":null,"ArticlePicture":[],"TitleCN":null,"AbstractTextCN":null,"PMCID":null,"EPubDate":"","PubModel":"","JCR":"","JCRName":"","Score":null,"Total":0}
引用次数: 2
Abstract
One of the major new features in the Fortran 2003 is features for interoperability with C © Interop). The intrinsic module ISO_C_BINDING provides:• constants, mostly type parameters, C_NULL_CHAR, C_NULL_PTR, and others,• types, and in particular, TYPE(C_PTR) and TYPE(C_FUNPTR),• procedures, such as C_LOC, C_FUNLOC, C_F_POINTER, C_F_PROCPOINTER and C_ASSOCIATED.A Fortran interface can be specified for a C function with external linkage and used to invoke such a function. The interface has the characteristic BIND(C) label, and must also satisfy some additional restrictions.C Interop can be used to portably use multi-language codes in Fortran. Since most languages interoperate with C, the feature can actually be used to interoperate with other programming languages as well. C Interop can also be used to give access to Fortran programmers to the many standard libraries with widely-used and implemented C interfaces. This includes lower-level tasks such as interfacing with the OS on UNIX-based systems, or using special libraries like OpenGL.For simple API's, developing Fortran interfaces is practically trivial once one gets some experience. For more complicated API's whose full functionality/power is not needed, such as for example TCP/IP sockets or shared-memory segments on UNIX systems, it is often easier to develop a condensed C API/library that does the actual work, and is simpler to interface to from Fortran. However, for libraries like OpenGL, one should provide a full Fortran interface so that the whole API can be accessed. Doing this manually is not easy and is also error-prone due to the size of the OpenGL/GLU/GLUT interfaces. For certain libraries like MPI, a special Fortran interface may be defined for the purposes of efficiency, portability, ease-of-use, or to accommodate for language semantic differences.In this first paper, we will show how to develop a Fortran interface for a simple C API/library. In a second paper, we consider automating the process so that large and more complex API's, and in particular, OpenGL, can be handled. The source codes can be obtained at http://atom.princeton.edu/donev/F2x.Along the way, we identify some problems with the design of C Interop in Fortran 2003.
在Fortran 2003中与C语言的互操作性
Fortran 2003的主要新特性之一是与C语言(©Interop)的互操作性。内部模块ISO_C_BINDING提供:•常量,主要是类型参数,C_NULL_CHAR, C_NULL_PTR等;•类型,特别是type (C_PTR)和type (C_FUNPTR);•过程,如C_LOC, C_FUNLOC, C_F_POINTER, C_F_PROCPOINTER和C_ASSOCIATED。Fortran接口可以为带有外部链接的C函数指定,并用于调用这样的函数。该接口具有BIND(C)标签的特征,并且还必须满足一些附加的限制。C互操作可用于在Fortran中移植使用多语言代码。由于大多数语言都与C语言互操作,因此该特性实际上也可以用于与其他编程语言互操作。C互操作还可以用于为Fortran程序员提供访问许多具有广泛使用和实现的C接口的标准库的权限。这包括较低级别的任务,如在基于unix的系统上与操作系统进行接口,或使用像OpenGL这样的特殊库。对于简单的API,一旦有了一些经验,开发Fortran接口实际上是微不足道的。对于不需要完整功能/功能的更复杂的API,例如UNIX系统上的TCP/IP套接字或共享内存段,通常更容易开发一个浓缩的C API/库来完成实际工作,并且更容易从Fortran进行接口。然而,对于像OpenGL这样的库,应该提供一个完整的Fortran接口,以便可以访问整个API。手动执行此操作并不容易,而且由于OpenGL/GLU/GLUT接口的大小,也容易出错。对于某些库(如MPI),可能会为了效率、可移植性、易用性或适应语言语义差异而定义特殊的Fortran接口。在第一篇文章中,我们将展示如何为一个简单的C API/库开发Fortran接口。在第二篇论文中,我们考虑自动化这个过程,这样就可以处理大型和更复杂的API,特别是OpenGL。源代码可以在http://atom.princeton.edu/donev/F2x.Along上获得,我们找出了在Fortran 2003中设计C互操作时存在的一些问题。
本文章由计算机程序翻译,如有差异,请以英文原文为准。