Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Loglan 05 External Func HC

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

5 External Functions

Contents

Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Loglan Program Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Example 1: Calling External C Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Example 2: Calling External Fortran Function in Windows . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

Geolog® 7 – Paradigm™ 2011 External Functions 5-1


With Epos® 4.1 Data Management
Overview

External functions may be called from within a Loglan program. Because the Loglan program is converted
to C, it is easier if the external function is also written in this language, although Fortran may also be called
if the argument passing mechanism is compatible.

NOTE All parameters and variables are to be in lowercase to prevent compilation failure.

Loglan Program Structure


Because Loglan does not allow unknown (external) functions to be directly called, it is necessary to enter C
mode to call an external function. This is done as follows:
.cc_begin
variable = user_function ( arg1, arg2, arg3... )
.cc_end
All numeric variables in Loglan programs are of type "double"; this may cause difficulties with Fortran
subroutines with "integer" or "real" arguments. In this case, local variables of the appropriate type may
need to be declared (within "C Mode").
Most fortran compilers append an underscore to the name of an external. For example, to call a routine
declared in Fortran as
subroutine exttst ( integer, real, double )
using Loglan variables var1, var2, and var3, you would need:
.cc_begin
{
integer arg1;
float arg2;
arg1 = var1;
arg2 = var2;
exttst_ ( &arg1, &arg2, &var3 );
var1 = arg1; /* Required if the first argument is modified. */
var2 = arg2; /* Required if the second argument is modified. */
}
.cc_end
Include files containing prototype definitions may be specified using Tools > Externals from the menu in
mui_loglan. If no function prototypes have been used, the types of the arguments MUST match those
expected by the external function. Returned arguments need to be passed by address (by preceding the
argument name by "&" in the call).

Compilation and Linking

Files containing compiled external functions are passed to loglan_compile by the OBJS or EXTLIBS
arguments.

Geolog® 7 – Paradigm™ 2011 External Functions 5-2


With Epos® 4.1 Data Management
If these are defined using mui_loglan, they will be automatically passed to loglan_compile when the Loglan
program is compiled. The OBJS parameter ("External Objects" in mui_loglan) is a space separated list of
files to be linked with the Loglan program. Each entry in the list is used as follows:
• If a directory is supplied, the entry (suffixed with ".o" if no extension is supplied) is added to the
list of files to be linked.
• If the entry does not contain a directory, "./loglan" is used, and the extension is checked. If the
extension is non-blank and is not ".c", "C", ".cpp", ".cc" or ".f", the entry will be used as specified.
If the extension is any of the above, the source file will be compiled and the resultant object file
will be used. If the extension is blank, a source file (C or Fortran) will be compiled if either the
source file is newer than the corresponding object file, or the corresponding object file does not
exist; the object file is then added to the list of files to be linked.

NOTE In Windows, to set the Fortran compiler variable use the Windows short name. The
following example gives a guide:

set env(PG_F77) "C:/PROGRA~2/Intel/Compiler/Fortran/10.1.025/em64t/bin/ifort.exe"

Example 1: Calling External C Function


The following is an example of a Loglan program which uses an external C function.

Geolog® 7 – Paradigm™ 2011 External Functions 5-3


With Epos® 4.1 Data Management
Geolog® 7 – Paradigm™ 2011 External Functions 5-4
With Epos® 4.1 Data Management
Example 2: Calling External Fortran Function in Windows
The following is an example of a Loglan program which uses an external Fortran function. The Loglan and
associated external files used in the example are located in <geolog install>/doc/examples/loglan directory.

NOTE This example works on Linux but it may be simpler as it is possible that the Fortran
compiler location will be in the path so there would be no need for the PG_F77 environment
variable to be set.

Geolog® 7 – Paradigm™ 2011 External Functions 5-5


With Epos® 4.1 Data Management
Geolog® 7 – Paradigm™ 2011 External Functions 5-6
With Epos® 4.1 Data Management
Geolog® 7 – Paradigm™ 2011 External Functions 5-7
With Epos® 4.1 Data Management
Geolog® 7 – Paradigm™ 2011 External Functions 5-8
With Epos® 4.1 Data Management
Index

E
environment variable
PG_77 3
external function
Loglan call 2

L
Loglan
difficulties with Fortran subroutines 2
external function call 2
program structure 2

P
PG_F77
environment variable 3
program structure
Loglan 2

Geolog® 7 – Paradigm™ 2011 External Functions 5-9


With Epos® 4.1 Data Management

You might also like