Even Summary of FortranCC ++ mixed programming, everyone is interested in seeing it.

xiaoxiao2021-03-06  16

Sender: JXBKING (Arcane), News District: Fortran

Title: Even Summary Fortran / C / C Mixed Programming, everyone is interested, look at it.

Sending station: Nanjing University Small Lily Station (Fri Jun 4 13:21:50 2004)

C / C / Fortran mixed programming

Mixed programming is often encountered in software programming, especially mixed programming of C / C / fortran, which mainly shows the problems that are often encountered in the above three language mixing program, and also illustrate mixed programming under different platforms. The problem should be paid attention to.

The problem that the mixed language programming should be considered mainly in: Function call and data structure storage.

1 Windows Platform

Function: Because the Fortran programming language has no casement, the mixed language programming under the Windows platform should be considerably, the main case is case. Considering the difference in the compiler, the following can be declared in the following manner. (C / C compiler uses Microsoft Visual C 6.0, the Fortran compiler uses Digital Visual Fortran 6.0).

Suppose a function of a C is void cfunction (); then, only need to be defined below its header file:

#ifdef __cplusplus

Extern "c" void {

#ENDIF

EXTERN VOID __STDCALL CFUNCTION ();

#Define cfunction cfunction

#ifdef __cplusplus

}

#ENDIF

In this way, it can be called directly in the process of Fortran or C .

Suppose is a FortRan function subroutine ffunction (); then, in the C header file, you can use the following definition:

#ifdef __cplusplus

Extern "c" void {

#ENDIF

EXTERN VOID __STDCALL FFUNCTION ();

#Define ffunction ffunction

#ifdef __cplusplus

}

#ENDIF

In this way, you can call directly in the C program. Due to the C compiler, there is no define __cplusplus this environment variable, so this header can also be used directly.

If it is a C function, such as: void cplusPlusFunction (); and C function, make the following definition:

#ifdef __cplusplus

Extern "c" void {

#ENDIF

Extern void __stdcall cplusplusfunction ();

#Define CPlusPlusFunction CPlusPlusFunction

#ifdef __cplusplus

}

#ENDIF

After the above definition, all functions can be freely called in three languages.

In the mixed programming of three languages, pay attention to the parameters of the function: the transfer problem of the string.

In a mixed language programming of the Fortran and C / C on the Windows platform, the processing of strings requires special attention. A character variable of Fortran is a string of the fixed length, no special termination symbol, which is not like C / C . Regarding how to represent characters, how to store their lengths without a fixed agreement. Some compilers are transmitted to a program as a pair of parameters, one is to save the address of this string, and the other is the length of the string. The end of the string of the Fortran is determined by the length of the string. For functions containing strings, you can do this:

For example, the function void ccharfunction (char * msg) needs to be defined: Void CcharFunction (CHAR * MSG, INT LEN); after the above Define, only CcharFunction (MSG) is called in Fortran. Since the Fortran program does not have a clear string end flag, so if the two strings are connected together, the C's program will take this over the string, so it is best to be in the program of c, This string obtained by the Fortran program is processed, because, from the variable of the Len, the string length can be obtained, and the front LEN character taken as the MSG as this string should have.

And if it is in the Fortran program, such as the function: Subroutine Fcharfunction (Fchar); after the corresponding statement, the following definition is performed:

#Define fchaarfunction (fchar), Fcharfunction (Fchar, Strlen (fchar))

In this way, you can call directly in the C / C program.

In these three hybrid programming, there is a small problem that is the problem of pointers. All variables in Fortran are equivalent to pointers in C / C , so in programs inside C / C , the parameters of the function should be declared in the form of a pointer (except for the length of the string parameter).

Data: In mixed programming, the difference in data must also cause sufficient attention. This is reflected in two aspects, arrays and structures.

Array: The array in the Fortran language, the arrays in the array and C / C are somewhat different, which is in two aspects, one is the ranks, and the second is the array start value.

The Fortran language is different from the C / C row priority, and the use of the column priority. Suppose an A array, M rows N columns, then the data storage format when using row priority is:

A11, A12, ..., A1N, A21, A22, ..., A2N, ..., AM1, AM2, ..., AMN

The data storage format using the column is:

A11, A21, ..., AM1, A12, A22, ..., AM2, ..., A1N, A2N, ..., AMN

The priority order is extended to the multidimensional array, which is stated as the first right subscript; the column priority is promoted to the multidimensional array, which is specified as the first left subscript. In this way, when the data is called in the mixed language program, you must pay attention to the difference in the ranks, and accurate calls.

Another difference in array is different from the starting date. Inside, the default array subscript starts at 1, while the C / C starts from 0, so you should pay attention to add or reduce one in the call to ensure call to the correct data.

Structure: After the structure in the Fortran language is declared, it is assigned space, and it is also necessary to declare it in C / C .

Use the following manner:

FORTRAN:

Common / color7 / c_red, c_green, c_blue

Common / NDAT / NID (Nasize), XN (3, Nasize)

C / C :

#ifdef __cplusplus

Extern "C" {

#ENDIF

#define color7 color7

#define nddat nddat

Extern struct {float c_red; float c_green; float c_blue;} color7;

EXTERN STRUCT {Int Nid [Nasize]; float xn [Nasize] [3];

#ifdef __cplusplus

}

#ENDIF

2 linux platform

There is no difference in the mixed language programming of the Linux platform and the substantive difference on the Windows platform, mainly in Define. Considering the difference in the compiler, on the function declaration, the following manner can be declared across platform programming. (C / C compiler uses the GNU GCC, the Fortran compiler uses PGI Fortran).

Assume a function of a CFunction (); then, only need to define in its header file:

#ifdef __cplusplus

Extern "c" void {

#ENDIF

EXTERN VOID CFUNCTION ();

#Define cfunction cfunction_

#ifdef __cplusplus

}

#ENDIF

In this way, it can be called directly in the process of Fortran or C .

Note: The function name should not be more than 31 characters. (Ie the cfuntion_ character length is not more than 32 characters .pgi & linux)

Similarly, for the function in C and Fortran, as long as it is changed, it can be changed to lowercase.

For arrays, changes and windows are consistent. They are all different in the order of priority. For strings, there is no need for additional attention, and the GCC compiler handles this problem, that is, do not need additional changes.

The definition of the data structure is also changed to lowercase, such as:

FORTRAN:

Common / color7 / c_red, c_green, c_blue

Common / NDAT / NID (Nasize), XN (3, Nasize)

C / C :

#ifdef __cplusplus

Extern "C" {

#ENDIF

#define color7 color7_

#define nddat nddat_

Extern struct {float c_red; float c_green; float c_blue;} color7;

EXTERN STRUCT {Int Nid [Nasize]; float xn [Nasize] [3];

#ifdef __cplusplus

}

#ENDIF

3 other platforms

For the Solaris platform, basically consistent with the Linux platform, however, considering that Solaris is running on the SPARC CPU, it uses Big Endian, and the basic Windows and Linux run in Intel or AMD X86 platform, all using Little Endian, this requires special attention, which should give sufficient attention when reading and writing data files. Other UNIX platforms such as HP UNIX, ULTRIX, IRIS, etc., generally only the small differences on Define, basically in the string processing, structure and array, the same as Linux, for them, considering more central processors Differences from different brought. (Such as alignment, large end and small end). Win32 platform Define a a

Ultrix || sparc || Iris || Linux platform define a a_

HPUX || AIX platform Do not deffine

4 C / C / Fortran hybrid programming string processing

The case where the string often needs to be transmitted, and the passage of the string is a more troublesome thing. In Fortran, the string is not ending, but there is a length concept, that is, the compiler will Give each string one length to control the length of the string. However, this length parameter is in different platforms, and its location is also different (some of which follows the string, some follow the last side of the function parameters), for common platforms such as Windows, Linux, Solaris, HP UNIX , IRIS, can be defined as follows:

For example C function

Void Messag (char * msg1, int * where1, char * msg2, int * where2)

{

Printf ("...% s she% d, while% s she% d / n", msg1, * where1, msg2, where2);

}

If you want to call in Fortran, you need to deffine below:

#if Defined Ultrix || SPARC || Iris || Linux || Win32

#if defined ultrix || sparc || iris || Linux

EXTERN VOID __STDCALL Messag (char *, int *, char *, int *, int, int, int)

#define Messag (S1, I1, S2, I2) Messag_ (S1, I1, S2, I2, STRLEN (S1), Strlen (S2))

#ELSE / * WIN32 Platform * /

Extern void __stdcall messag (char *, int, int *, char *, int, int in)

#define Messag (S1, I1, S2, I2) Message (S1, Strlen (S1), I1, S2, STRLEN (S2), I2)

#ENDIF

#ELSE / * Other platform * /

EXTERN VOID __STDCALL Messag (char *, int *, char *, int *, int, int, int)

#define Messag (S1, I1, S2, I2) Messag (S1, I1, S2, I2, STRLEN (S1), Strlen (S2))

#ENDIF

If used in C , add the corresponding

#ifdef __cplusplus

Extern "C" {

#ENDIF

/ * Your extern code * / # ifdef __cplusplus

}

#ENDIF

It can be called directly in Fortra, such as:

Call Messag (Char1, I1, CHAR2, I2)

Similarly, the string processing function written in Fortran, after using the above define and extern, can also be called directly in C.

5 file read and write

The reading and writing of the file is also a very important issue in the mixed, and the usual problem occurs in the mixed compiler of different platforms, as well as different Fortran compiler compiles.

In Fortran, the write is done by the WRITE statement, and each WRITE statement writes multiple data at once, constitutes a data block. Each unformatted data block is composed of the following 3 part as shown in Figure 1: (1) The start flag of the data block, record all the number of bytes of all data; (2) constitutes the data content of the data block. The number of intensive numbers and floating point, accounting for 4 bytes, the low byte is before, high byte behind. There is no time between the data. (3) The end sign of each data block is also the number of bytes of the data block, rather than the return line symbol. There is no separator between each record.

In addition, due to differences in programming languages, different compiler storage formats also differ, such as Visual Fortran and Digital Fortran still differ in storage data blocks. Differences are in a WRITE statement, the start and end flag of the Visual Fortran storage data block is represented by one byte, and in Digital Fortran is in use with a shaping number, that is, four bytes. The Visual Fortran can store up to 2 ^ 7 (128 bytes), if a WRITE statement requires more than 128 bytes of data required, press | 80 | ..DATA .. | 80 | 80 | ... DATA ... | 80 | Cyclic deposit. So when reading, you can convert it to Digital Fortran storage.

form.

Arcane

Mail: jxbking@163.net

2003-9-12 (2003-10 / 30 Update).

-

A: There is no thing than the love value

B: a penny ratio does not have something worth money

This is available, love is worthless.

※ Source:. Nanjing University Small Lily Station http://bbs.nju.edu.cn [from: 218.104.83.243]

转载请注明原文地址:https://www.9cbs.com/read-41942.html

New Post(0)