2. Introductory Example

Making and using a DLL consists of three major steps

1.
Writing the C/C++ or Fortran code
2.
Generating the DLL
3.
Using DLL in XploRe


2.1 The program

C/C++ program.

Let us consider the simple C program vecsum.c which sums up a vector:

#include "dll.h"

EXTERN int EXPORT sum (double *x, double *n, double *s)
{
   long i, nn = (long) *n;
   *s=0.0;
   for (i=0; i<nn; i++)
     *s = *s + *(x+i);

   return (0);
}

Note that the include file dll.h defines the symbols EXTERN and EXPORT in a proper way for the below mentioned compiler.

Fortran 77 program.

We show here the equivalent Fortran 77 vecsum.f program:

C2345.7890123456789012345678901234567890123456789012345678901234567890
C
C
      function sum(x, nn, s)
      double precision nn,s
      double precision x(nn)
      integer i,n,sum
      n=int(nn)
      s=0.0

      do 10 i=0, n
         s=s+x(i)
10    continue

      sum=0
      return
      end


2.2 Generating a DLL

Linux with GNU C compiler.

This function can be compiled to a shared library, for instance with the Gnu C-Compiler

    gcc -shared -o vecsum.so vecsum.c

from the Linux shell prompt.

SUN Solaris with GNU C compiler.

This function can be compiled to a shared library, for instance with the Gnu C-Compiler

    gcc -G -o vecsum.so vecsum.c

from the Unix shell prompt.

Windows 95/NT with Borland C++ 5.02.

As a first step we create a project called vecsum.ide to generate a Windows DLL:

1.
choose from the menu File/New/Project
2.
change ProjectPath to C:\vecsum\vecsum.ide
3.
choose under Target Type the second item Dynamic Library [.dll]
4.
change in the second box under Libraries: the radio button from Dynamic to Static
5.
decheck in the box Frameworks: the check boxes OWL and then Class Library
6.
press OK
7.
you may delete in the Project Window the files vecsum.rc and vecsum.def

The second step consists of generating the code and compiling it:

1.
in the Project window you get the context menu of vecsum.cpp with right mouse button and choose View/Text Edit
2.
in the editor window type now (or copy) the example code given above
3.
make sure that dll.h is in C:\vecsum
4.
save the file via the menu File/Save
5.
compile the DLL via the menu Project/Build all

Windows 95/NT with Visual C++ 5.0.

As a first step we create a project called vecsum project to generate a Windows DLL:

1.
choose from the menu File/New
2.
choose Win 32 Dynamic-Link Library
3.
enter a Project name, e.g. vecsum
4.
enter a Path, e.g. C:\vecsum
5.
press OK

The second step consists of generating the code and compiling it:

1.
open the new dialog from the menu Project/Add to project/New
2.
choose C++ source code file
3.
enter under Filename the name vecsum
4.
in the editor window type now (or copy) the example code given above
5.
make sure that dll.h is in C:\vecsum
6.
save the file via the menu File/Save
7.
compile the DLL via the menu Build/Make vecsum.dll

Windows 95/NT with Symantec C++ 7.0.

As a first step we create a project called vecsum.prj to generate a Windows DLL:

1.
copy to C:\vecsum a file with the example code named vecsum.cpp and the file dll.h
2.
choose from the menu Project/New
3.
type under Project Name: the project name vecsum.prj
4.
change drive and directories to C:\vecsum
5.
press Next
6.
choose under Target Type the radio button Dynamic-Link Library
7.
press Next
8.
mark in the second window under File Name: the file vecsum.cpp
9.
press the button Add
10.
press Finish

The second step consists of compiling the code:

1.
compile the DLL via the menu Project/Rebuild All

Linux with GNU Fortran 77 compiler.

This function can be compiled to a shared library, for instance with the GNU Fortran-Compiler

    g77 -shared -o vecsum.so vecsum.f

from the Linux shell prompt.

SUN Solaris with GNU Fortran 77 compiler.

This function can be compiled to a shared library, for instance with the GNU Fortran-Compiler

    g77 -G -o vecsum.so vecsum.f

from the Unix shell prompt.

SUN Solaris with Fortran 77 compiler.

This function can be compiled to a shared library, for instance with the SUN Fortran-Compiler

    f77 -G -o vecsum.so vecsum.f

from the Unix shell prompt.

Windows 95/NT with Absoft Pro Fortran 6.0.

As a first step we create a project called vecsum.gui to generate a Windows DLL:

1.
copy vecsum.f to C:\vecsum
2.
start the Absoft Compiler Interface
3.
enter under Name the project name vecsum and under Location the directory C:\vecsum
4.
press OK
5.
choose under Target Type the radio button Dynamic-Link Library
6.
check in the registry card DLL the box Export All Routines and Data
7.
press OK
8.
get by the right mouse button the context menu and choose Add/Remove file(s)
9.
mark vecsum.f
10.
press Add and then Close

The second step consists of compiling the code:

1.
get again the context menu
2.
choose Build to compile the DLL


2.3 Using the DLL in XploRe

To use the DLL in XploRe requires the following simple steps.

1.
Define the vector x and initialize the result s
x=1:10
s=0
2.
Open the DLL for use in XploRe
h=509 dlopen ("vecsum.so") for Unix/Linux
h=512 dlopen ("vecsum.dll") for Windows 95/NT
Note that XploRe needs to be able to find this library. From XploRe you can set the path for DLLs by 515 setenv ("xpl4dll","/your_absolute_path_to_the_dll"). Alternatively you can give the absolute path within the 518 dlopen command.
3.
Call the function sum from the DLL for x
521 dlcall ("_sum",x, rows(x),s) for Borland C++, Absoft Fortran
524 dlcall ("sum",x, rows(x),s) for GNU C++, Visual C++, Symantec C++
527 dlcall ("sum_",x, rows(x),s) for GNU Fortran 77, SUN Fortran 77

As a result, you should find s having the value 55, which is the sum for the numbers 1 to 10.



Method and Data Technologies   MD*TECH Method and Data Technologies
  http://www.mdtech.de  mdtech@mdtech.de