Using IMSL Libraries in a Mixed-Language Environment

This section explains how to use the IMSL Libraries in a mixed-language development environment with Visual Fortran and Microsoft Visual C++.

Messages that IMSL routines write to standard output or to error output in a mixed-language application or an application for Windows can be awkward if they are written to the screen. You can avoid this by calling UMACH from a Fortran routine to remap the output and error units to a file instead of to the screen. For example, the following free-form program writes the standard output from VHSTP to the file STD.TXT, and the error message from AMACH to the file ERR.TXT:

  PROGRAM fileout
!       This program demonstrates how to use the UMACH routine to
!       redirect the standard output and error output from IMSL
!       routines to files instead of to the screen. The routines
!       AMACH and UMACH are declared in the numerical_libraries module
!
   USE numerical_libraries
   INTEGER STDU, ERRU
   REAL x, frq(10)/3.0,1.0,4.0,1.0,5.0,9.0,2.0,6.0,5.0,3.0/
!
!       Redirect IMSL standard output to STD.TXT at unit 8
!
   CALL umach(-2, STDU)
   OPEN (unit=STDU, file='std.txt')
   CALL vhstp(10,frq,1,'Histogram Plot')
   CLOSE(8)
!
!       Redirect IMSL error output to ERR.TXT at unit 9
!
   CALL umach(-3, ERRU)
   OPEN (unit=ERRU, file='err.txt')
   x = amach(0)   ! Illegal parameter error
   CLOSE(9)
   END

The standard output from IMSL routine VHSTP written to STD.TXT is:

1
           Histogram Plot
 Frequency-------------------------
    9  *            I           *
    8  *            I           *
    7  *            I           *
    6  *            I   I       *
    5  *          I I   I I     *
    4  *      I   I I   I I     *
    3  *  I   I   I I   I I I   *
    2  *  I   I   I I I I I I   *
    1  *  I I I I I I I I I I   *
 ----------------------------------
 Class            5        10

The error output from IMSL routine AMACH written to ERR.TXT is:

 *** TERMINAL ERROR 5 from AMACH. The argument must be between 1 and 8
 ***          inclusive. N = 0

Consider the following simple Fortran example that uses the IMSL library:

  USE numerical_libraries
  real rinfp
  rinfp = AMACH(7)
  write(*,*) 'Real positive machine infinity = ',rinfp
  end

The output is:

 Real positive machine infinity = Infinity

The corresponding C example is:

 /* FILE CSAMP0.C */
 #include <stdio.h>
 #include <stdlib.h>

 extern float _stdcall AMACH(long *);

 main()
 {
    long n;
    float rinfp;

    n = 7;
    rinfp = AMACH(&n);
    printf("Real positive machine infinity = %16E\n", rinfp);

    fflush(stdout);
    _exit(0);
 }

This C language example demonstrates the use of:

The C example can be compiled by the cl command to create an object file that can be linked using the DF command.

For more information on mixed-language programming, see Programming with Mixed Languages.