

This step would be problematic, though, if we hadn't used the proper kind when declaring our Fortran array. To do so, we've used the C_F_POINTER intrinsic subroutine that accepts a C array and assigns it to a Fortran array such that it may be used as any Fortran-native array would be. Since a pointer to an array is passed in, we actually want to access this region of memory as a standard Fortran array. These kind declarations, provided by the intrinsic ISO_C_BINDING module, ensure that the declarations in Fortran and the calling C routine both match regardless of the underlying platform. As explained, we need to alert the Fortran compiler that these arguments will be passed "by value." We've also changed our declarations of the actual array and the integer argument to be kind=c_float and kind=c_int respectively. Additionally, both the pointer and the integer arguments now have the value attribute in their declarations. Instead, a C pointer, declared as type(c_ptr) is expected as an argument. In this updated definition, you'll notice that the function no longer directly accepts an array as an argument. Real(kind=c_float), dimension(:), pointer::x Our function declaration can now be changed to: c Third, we should employ the features of Fortran's C binding abilities, declaring the function appropriately. Second, C normally passes "by value," as opposed to Fortran's default of "by reference." When a C routine is passing an integer to a function, it is explicitly passing the value of that integer, not a reference to a common memory location like Fortran might use (depending on how we've declared our arguments).
#Project options simply fortran code
To maximize portability, we should change our Fortran code to accept a C pointer. First, we need to understand that C treats arrays simply as pointers. The call is somewhat ambiguous, so we'll need to properly declare this function to conform more to what C expects. This function does include an integer declaring the length of our array because we eventually want to interact with another language the C language does not have arrays that explicitly store their sizes.Īs written, this routine may or may not be callable from C. A simple implementation of a mean routine in (fixed-format) Fortran would be: c

Routines callable from C, of course, are generally callable from any language without much additional effort.įor this example post, we'll create a file averaging.f that contains a number of routines for computing different statsistical "averages," mean, median, and mode to be specific. In this post, we'll look at creating a DLL with some functions that can be seamlessly called from the C language.

Often times, though, users wish to create DLLs that can interact with other languages and programs, which can mildly complicate matters on Windows.
#Project options simply fortran windows
Simply Fortran for Windows can easily produce dynamic link libraries, or DLLs, using the appropriate "New Project" entries on its Start screen.
