1. VC and Fortran mixed programming current VC and VF are Windows-based IDE development environments, and automatically integrated into VS development environments when installing the same version, this will give us three times when calling Fortran. Different methods. 1.1 The compiler of file integration of different languages under the same work can select the compilation mode according to different extensions, generate the target code file .Obj, then link according to the specified call mode, generate executable files .exe. In 2 we mention the calling rules, call the Fortran function or module in the C language, must be crown in the front of the function name to be called _stdcall keyword as the call mode, so the compiler will handle the sequence of parameters, parameter transmission Ways, stack processing and naming modifications. In the C language, although it is considering the C language, it is considering the C , that is, the C call rules and c are consistent, but C is inconsistent with the C language on naming conversion. Refer to the following to the following declaration: 1 C language calls the declaration of the Fortran module: Extern void _stdcall Subroutine (argtype arg1, ...); extern float _stdcall function (argtype arg1, ...); 2 C language call Fortran module statement: Extern "C" {void _stdcall subs ";} extern" c "{float _stdcall function (argtype arg1, ...);} External function declaration only provides functions to the calling process, the purpose is In order to ensure compilation pass. When the link generates an executable, the main call process is called the module according to the function name link. If the module is found, the link is successful, with the modifier of the call rule and the naming rule to convert, to ensure normal operation. 1.2 Call the DLL module generated by the Fortra language: The algorithm in the DLL module has made modifications, as long as the function or sub-routine name and the entrance parameter are unchanged, the main call process does not need to re-modify or compile. The benefits of the DLL file are a lot, and interested readers can refer to the related computer books. The process of making the DLL module is as follows: First, use the wizard to create an empty Visual Fortran DLL project, you can see the function declared with the CDEC $ Attributes DLlexPort Directive, which is the subroutine output for the DLL output. Users can call it in an external program to complete certain functions. You can add a CDEC $ Attributes DLlexPort directive to a DLL file declare multiple functions, procedures, or data to be output.
For example, declare the output function Factinteger * 4 function fact (n)! Dec $ attributes dllexport :: Factinteger * 4 n ... End Function Fact! ARRAYTESTSUBROUTINE ARRAYTESTSUBROUTINETESTEST (ARR)! DEC $ Attributes Dllexport :: ArrayTestreal * 4 Arr (3, 7) ... End Subroutine Arraytest1.3 VC Call IMSL Mathematics Library Function This is a mixed programming comparison, the author discovered the above change call rules and naming during use The conversion method still cannot call the VF math library directly in the VC, compile the error: Unresolved External Call, that is, an external call that cannot be resolution. Even if the compilation instruction of the VC #pragma Comment (lib, "***.") Adds the three lib files required to call IMSL, there is still an unresolved external call error, which is unknown for the internal calling process. To study the online help of Compaq Visual Fortran, you can find that in the Fortran program, you need to use the IMSL library, you also need to add compiling instructions Use Numeric_libraries to include the exact header files and libraries so you can use the functions in the IMSL math library. Based on the above analysis, we can customize the DLL, including the math functions to be used in your module, then use in the VC, refer to the following example: Real * 4 Function Fcbrt (a)! Expose Function Fcbrt To Users of this DLL ! DEC $ attributes dllexport :: Fcbrtuse Numeric_librariesreal * 4 A [Value]! VARIABLES! BODY OF THE FUNCTIONFCBRT = CBRT (a) End function FCBRT Note Output Function Name Don't be the same name in the IMSL library, otherwise generates DLL Library function name conflict! Of course, the user wants to make a custom and packaging of library functions in order to simply call several mathematics functions during actual use. The following is provided with a simple way to call the IMSL library function in the VC, refer to Table 1: Static or DLL multi-thread? Whether to use the FORTRAN link inventory used by the C debug library, the C link inventory is used in stock. LIB DLL No No DFORDLL.LIB (Dforrt.dll) MSVCRT.LIB (MSVCRT.DLL) DLL No is DFORDLL.LIB (DFORRT.DLL) MSVCRTD.LIB (MSVCRTD.DLL) DLL Yes No DFORMD.LIB (DFORMD.DLL) MSVCRT.LIB (MVCRT.DLL) DLL is DFORMD.LIB (DFORMD.DLL) MSVCRTD.LIB (MSVCRTD.DLL) Table 1
In programming practice, according to the call dependencies of the table, the author summarizes the method of calling the IMSL math library directly in the VC project. First, call the IMSL library function in the console application, you must include DFOR.LIB, DFCONSOL.LIB, call the IMSL library function in the MFC application, must contain dFormd.lib, of course, the above two cases Project To call the Mathematics library provided by Fortran, the following three library files are basic: 1 IMSL.LIB IMSL static library, including all functions and sub-routines for Fortran77 and Fortran90; 2 IMSLS_ERR.LIB IMSL error handling library; 3 IMSLMPistub .LIB Based on multi-CPU parallel processing library (available for single CPU PC users) readers can be modified in the setting items of the Project menu in the project in the project. 2 Provides a simple example in computer graphics about curves, curved, and a large number of solution linear equations will be used. If the B-spline is interpolation problem, you must first perform the reverse control vertex, then in accordance with the B-spline base, we use the VC to complete the interface, and use the math function provided by the IMSL to complete the calculation. Example: known value point sequence pi, i = 0, 1, 2 ..., 7: p [0] p [1] p [2] p [3] p [4] p [5] p [6] P [7] Abscissae x 8.125 8.400 9.000 9.485 9.600 9.959 10.166 10.200 Ordinates Y 0.0774 0.099 0.28 0.6 0.708 1.20 1.80 2.177
Seeking control vertex sequences. The mathematical problem of the crowd of B pocket curve control is: D represents the DE BOOR control vertex, P known value point column and boundary condition. The function LSASF / DLSASF to be called the IMSL solution linear equation group in the VC, and the complete code is as follows:
#include
#pragma comment (lib, "dfor") # Pragma Comment (lib, "dfconsol") # Pragma Comment (Lib, "IMSL") # Pragma Comment (Lib, "IMSLS_ERR")
// Solve a real symmetric system of linear equmentions with itrestive refinement.extern "c" {void _stdcall lsasf (int *, float *, int *, float *, float *);} extern "c" {void _stdcall DLSASF (int *, Double *, int *, double *, double *);}
INT Main (int Argc, char * argv []) {INT i, J; int LDA = 8; int N = 8; double a [8] [8]; double px [8] = {6 * 8.125, 6 * 8.400, 6 * 9.000, 6 * 9.485, 6 * 9.600, 6 * 9.959, 6 * 10.166, 6 * 10.200}; Double Py [8] = {6 * 0.0774, 6 * 0.099, 6 * 0.28, 6 * 0.6, 6 * 0.708, 6 * 1.20, 6 * 1.80, 6 * 2.177}; // Store Double DX [8], DY [8];
For (i = 0; i <8; i ) {for (j = 0; j <8; j ) {if (i == j) a [i] [j] = 4.0; Else IF (ABS (Ij) == 1) a [i] [j] = 1.0; ELSE A [I] [j] = 0.0;}} a [0] [0] = a [7] [7] = 5.0; DLSASF (& n, & a [0], & LDA, PX, DX); DLSASF (& N, & A [0] [0], & LDA, PY, DY);
For (i = 0; i RETURN 0;} calculation results Di, i = 0, 1, ..., 9 D [0] = D [1] d [2] d [3] d [4] d [5] d [6] p [7] D [8] = D [9] Abscissae x 8.08523 8.32384 9.01941 9.5984 9.4944 10.0157 10.1944 10.0157 10.1944 10.0157 10.1948 10.201 Ordinates Y 0.07852 0.07183 0.22819 0.69544 0.59008 1.24422 It is worth noting that when the external function is declared, the parameter list in the FORTRAN function must declare the reference of the C / C parameter type, namely the pointer type, the data type correspondence reference table 2: Fortran Type Integer * 1 Integer * 2 INTEGER * 4 REAL * 4 Real * 8 Chratio * 1 C / C Type Char Short Int, long float double unsigned char Table 2 There are many ways to introduce a static link library or a dynamic link library. There are many examples in the routine, and readers can refer to related programming materials.