请选择 进入手机版 | 继续访问电脑版

因斯福论坛

搜索
查看: 12344|回复: 4

Fortran Programming with Mixed Languages 混合语言编程

[复制链接]
发表于 2011-10-10 15:52:10 | 显示全部楼层 |阅读模式
本帖最后由 Zhong_Shengjun 于 2011-10-10 21:07 编辑

附件是 Intel Fortran Compiler User and Reference Guides 的第13章。 CH-13 Programming with Mixed Languages.pdf (402.19 KB, 下载次数: 6447)
回复

使用道具 举报

 楼主| 发表于 2011-10-10 16:33:05 | 显示全部楼层

Mixed-Language Programming Considerations 混合编程考虑的事项

本帖最后由 Zhong_Shengjun 于 2011-10-11 11:39 编辑

Mixed-Language Programming Considerations

There are other important differences in languages; for instance, argument passing, naming
conventions, and other interface issues must be thoughtfully and consistently reconciled between
any two languages to prevent program failure and indeterminate results. However, the
advantages of mixed-language programming often make the extra effort worthwhile. The
remainder of this section provides an explanation of the techniques you can use to reconcile
differences between Fortran and other languages.

在编程语言之间还有其它的重要差别,例如,两种语言之间的参数传递、命名约定和其它的接口问题应深思熟虑地、始终如一地使之一致,以避免程序失败或得不到预期的结果。

Adjusting calling conventions, adjusting naming conventions and writing interface procedures
are discussed in the following topics:

调用约定处理、命名约定处理或在Fortran中写接口过程等主题在以下部分讨论:

• Adjusting Calling Conventions in Mixed-Language Programming
• Adjusting Naming Conventions in Mixed-Language Programming
• Prototyping a Procedure in Fortran

• 处理调用约定
• 处理命名约定
• 在Fortran中定义接口原型

After establishing a consistent interface between mixed-language procedures, you then need
to reconcile any differences in the treatment of individual data types (strings, arrays, and so
on). This is discussed in Exchanging and Accessing Data in Mixed-Language Programming. You
also need to be concerned with data types, because each language handles them differently.
This is discussed in Handling Data Types in Mixed-Language Programming.

在不同语言的过程之间建立起一致的接口之后,再考虑一些特定数据类型(字符串、数组等等)处理的不同。这部分内容在“混合编程的数据访问和数据交换”一节中讨论。你还要考虑数据类型的不同,因为每种语言以不同的方式处理数据类型。这部分内容在“混合编程数据类型的处理”一节讨论。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2011-10-10 17:04:46 | 显示全部楼层

Adjusting Calling Conventions in Mixed-Language Programming

本帖最后由 Zhong_Shengjun 于 2011-10-12 19:18 编辑

Adjusting Calling Conventions in Mixed-Language Programming

混合语言编程中的调用约定的处理

The calling convention determines how a program makes a call to a routine, how the arguments
are passed, and how the routines are named.

调用约定决定了如何调用一个过程,如何传递参数,如何命名过程。

A calling convention includes:

• Stack considerations
   • Does a routine receive a varying or fixed number of arguments?
   • Which routine clears the stack after a call?
• Naming conventions
   • Is lowercase or uppercase significant or not significant?
   • Are external names altered?
• Argument passing protocol
   • Are arguments passed by value or by reference?
   • What are the equivalent data types and data structures among languages?

调用约定包括:

• 栈的考虑
   • 过程接受可变数量的还是固定数量的参数?
   • 调用之后调用者和被调用者谁负责清理栈?
• 命名约定
   • 大小写重要吗?
   • 外部名称是否改变?
• 参数传递协议
   • 参数是按值传递还是按引用传递(传递参数的地址)?
   • 不同语言的数据类型和数据结构是如何等同的?

The ATTRIBUTES properties (also known as options) C, STDCALL (Windows* OS only),
REFERENCE, VALUE, and VARYING affect the calling convention of routines. You can specify:

ATTRIBUTES 属性(也称选项)C, STDCALL(仅用于Windows操作系统), Reference, Value 和 VARYING 影响调用约定。

• The C, STDCALL, REFERENCE, and VARYING properties for an entire routine.
• The VALUE and REFERENCE properties for individual arguments.

• C, STDCALL, REFERENCE, 和 VARYING 属性适用于整个过程(函数和子程序);
• VALUE and REFERENCE 适用于独立的参数 。

By default, Fortran passes all data by reference (except the hidden length argument of strings,
which is passed by value). If the C (or, for Windows OS, STDCALL) option is used, the default
changes to passing almost all data except arrays by value. However, in addition to the
calling-convention options C and STDCALL, you can specify argument options, VALUE and
REFERENCE, to pass arguments by value or by reference, regardless of the calling convention
option. Arrays can only be passed by reference.

默认情况下,Fortran 采用 Reference 方式传递参数数据(除了隐式传递的参数字符串的长度是以值传递的),如果采用C属性(对于Windows操作系统,或者STDCALL),则所有参数默认按值传递(数组例外,它总是按引用传递)。对于参数,可以采用 Value 或者 Reference 属性,而不论过程的调用约定属性。 数组只能按 Reference 传递。

Different Fortran calling conventions can be specified by declaring the Fortran procedure to
have certain attributes.

通过声明 Fortran 过程的属性设置调用约定。

It is advisable to use the DECORATE option in combination with the ALIAS option to ensure
appropriate name decoration regardless of operating system or architecture. The DECORATE
option indicates that the external name specified in ALIAS should have the correct prefix and
postfix decorations for the calling mechanism in effect.

一个例子:

Example:

Fortran Function and Subroutine:

SUBROUTINE FSUB (A,B)
!DEC$ ATTRIBUTES DLLEXPORT :: FSUB
INTEGER :: A,B
END SUBROUTINE
FUNCTION FOO(A)
!DEC$ ATTRIBUTES DLLEXPORT :: FOO
INTEGER :: A
INTEGER :: FOO
END FUNCTION

C# Wrapper Class:

public class FortranDllWrap
{
// CallingConvention.Cdecl must be used since the stack is
// cleaned up by the caller in Intel Fortran by default
//
[DllImport("FDll.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern void FSUB(ref int A, ref int B);
[DllImport("FDll.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int FOO(ref int A);
}


回复 支持 反对

使用道具 举报

 楼主| 发表于 2011-10-11 20:34:29 | 显示全部楼层

混合语言编程--C#调用Fortran动态链接库

本帖最后由 Zhong_Shengjun 于 2011-10-11 20:44 编辑

附件为C#调用Fortran动态链接库示例1。 CS_FDLL_Demo1.rar (13.7 KB, 下载次数: 13513)
回复 支持 反对

使用道具 举报

 楼主| 发表于 2011-10-11 21:05:30 | 显示全部楼层

混合语言编程--C#调用Fortran动态链接库DLL总结

本帖最后由 Zhong_Shengjun 于 2011-10-12 22:35 编辑


• Fortran过程(包括函数和子程序)的属性有:DllExport,Alias,C,StdCall,Reference,Varying。

• Fortran的参数属性有Reference和Value。

• C属性时,调用者清理内存栈,支持可变参数数目,参数传递方式默认为值传递。StdCall属性时,被调用者清理内存栈,不支持可变参数数目,参数传递方式默认为值传递。如果没有C或StdCall属性,调用者清理内存栈,参数传递方式默认为引用传递。

• 如果过程属性采用StdCall或C,并同时采用了Reference属性,则参数传递方式默认为引用传递。

• 如果参数的属性进行了单独的声明,则单独的参数属性声明优先(过程的参数传递默认属性被覆盖)。

• 在C#中写接口函数时,如果Fortran过程有StdCall属性,则在C#中CallingConvention=CallingConvention.StdCall(默认,可以不写)。如果Fortran过程为C属性,或者未显式指明StdCall/C属性,则在C#中CallingConvention=CallingConvention.Cdecl。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|因斯福论坛  

GMT+8, 2024-4-25 09:06

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表