Simply Fortran doesn't need to provide calling convention information for 64-bit DLLs as it conforms to the Windows standard.Īssuming, though, that we're building a 32-bit DLL, our function definition in its entirety has become: c ![]() The addition of STDCALL on 64-bit Windows is useless as only one calling convention is used on that operating system as a general rule. In order to clear up any confusion surrounding the calling convention on 32-bit Windows, we can explicitly declare it: function mean(xc, n) bind(c)ĬGCC$ ATTRIBUTES STDCALL, DLLEXPORT :: mean Generally speaking DLLs normally provide functions in the stdcall format, but there are countless exceptions. ![]() On 32-bit Windows (or any 32-bit process on 64-bit Windows), DLLs can provide functions using a whole assortment of calling conventions, an extremely technical topic that is nonetheless important. This step can be essential if you're working with, for example, Microsoft Visual C++, as we'll see shortly. The addition of the cGCC$ ATTRIBUTES DLLEXPORT line simply instructs the compiler to export this function so that external programs can call it. In this case, we'll add: function mean(xc, n) bind(c) First, we need to declare that this function is to be exported using a special type of comment that acts as a compile directive. We need one minor addition to this function so that we can ensure its interoperability with other langugages. The above routine should work beautifully if called from a C routine on any platform, but we're trying to move towards a complete DLL for Windows. 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). 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. Simply Fortran for Windows can easily produce dynamic link libraries, or DLLs, using the appropriate "New Project" entries on its Start screen.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |