Use C functions with Watcom Fortran

In summary, the conversation discusses the use of C functions in Watcom Fortran and the key considerations when using external DLLs in any language. These include the calling convention, structural definition and memory representation, and argument structure. The Windows standard method for dealing with DLLs is also mentioned, using LoadLibrary and GetProcAddress. The book provided covers these topics in detail but also cautions about potential differences in data structure between C and Fortran.
  • #1
eddyq
8
0
Is there a way to use C functions from Watcom Fortran?
 
Technology news on Phys.org
  • #2
Take a look at this:

ftp://openwatcom.mirrors.pair.com/archive/11.0c/docs/f77progguide.pdf

The key things just for future notice about using external DLL's written in any language are as follows:

1) Calling convention (for function)
2) Structural definition and memory representation
3) Argument structure (representation of pointers, and machine words like chars, ints, doubles, floats, longs, etc)

The windows standard way of dealing with DLL's is with LoadLibrary for loading the DLL and GetProcAddress to get the function pointer of the DLL.

I had a quick look at the book and it seems to cover the various calling conventions pretty well, but make sure you know how to convert between structures if say a matrix in C is not the same as a matrix in FORTRAN.
 

1. How do I use C functions with Watcom Fortran?

To use C functions with Watcom Fortran, you must first declare the C functions in your Fortran code using the EXTERNAL statement. Then, you can use the CALL statement to call the C functions within your Fortran code.

2. Can I pass Fortran variables to C functions?

Yes, you can pass Fortran variables to C functions by using the VALUE or REF attribute in the EXTERNAL statement. The VALUE attribute passes the value of the variable, while the REF attribute passes the address of the variable.

3. How do I handle C function return values in Fortran?

To handle C function return values in Fortran, you can use the FUNCTION statement to declare the C function as a function in your Fortran code. Then, you can use the RETURN statement to assign the return value of the C function to a Fortran variable.

4. Are there any limitations to using C functions with Watcom Fortran?

Yes, there are some limitations to using C functions with Watcom Fortran. For example, you cannot pass Fortran arrays directly to C functions. Instead, you must use the TRANSFER statement to convert the array to a temporary C array.

5. Can I mix C and Fortran code in the same file?

Yes, you can mix C and Fortran code in the same file as long as you use the appropriate syntax for each language. For example, C functions must be enclosed in extern "C" blocks, and Fortran code must be enclosed in PROGRAM or SUBROUTINE blocks.

Similar threads

  • Programming and Computer Science
2
Replies
62
Views
4K
  • Programming and Computer Science
Replies
5
Views
3K
  • Programming and Computer Science
Replies
3
Views
325
  • Programming and Computer Science
Replies
2
Views
913
  • Programming and Computer Science
Replies
6
Views
9K
  • Programming and Computer Science
Replies
2
Views
940
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
20
Views
3K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
13
Views
2K
Back
Top