Interface with other languages

If you are programming for Win32, Linux, Solaris or Win16 youshould link to external code via a DLL. This eliminates all of the potential headachesof interfacing with external systems.

The first thing you need when interfacing with another languageis a module which has the TYPE, CONST and PROCEDURE declarations you are going touse from the code in the other language. It is not likely that you will be providedwith an interface native to you language. Modula-2 or Ada95. Therefore you will needa basic working knowledge of C/C++, enough to convert C header files to your nativelanguage.

Some useful things to remember when converting a C header fileto make the C code more Modula-2/Ada95 in nature

  1. Convert C pointer parameters to Modula-2 VAR parameters, or Ada95 'in out' or 'out' parameters.
  2. Remember that in Stony Brook compilers array and record parameter types are always passed by reference. Using this knowledge you can convert C pointer parameters to these types to value or reference parameters of these types in Modula-2 or Ada95. Nearly all C code that passes a parameter of these types will pass it by reference, by address, because this is generally more efficient. The parameter may only be used as an input parameter, or an input/output parameter or just an output parameter. The documentation for the procedure will tell you how the parameter is used. For input parameters you would convert the C pointer parameter to a value parameter. For input/output you would convert to Modula-2 VAR or Ada95 in out parameters. For output you would convert to Modula-2 VAR or Ada95 out parameters.

For directly linking an external OBJ/LIB file or a foreign moduleyou will have no problems if that code has no external dependencies from the file.For a code other than assembly sometimes it is not always obvious that any externaldependencies exist. Any external code will have to be linked to you program via aLIB file. Refer to Using third party libraries supplied inOBJ or LIB form to learn hot to link that. External dependencies might requiresome sort of initialization to function properly. At this point there are no realanswers as to what to do. This is why we say you should link to the code via a DLL.The only other option is the use the CMAIN compiler option.

Using CMAIN

Again we reiterate that you should link to the code via a DLLwhen feasible. Otherwise, our system can make itself look like most C program entrypoints by using the CMAIN compiler directive inthe main program file of your program. In this way any external dependencies of thecode will be properly initialized by the C runtime system and then the runtime systemwill call the Modula-2 as though it is an actual C program.

The default public symbol name of the "main" entrypoint is usually correct for most systems on most target platforms, however it maynot be correct. In this case you will have to use the MAINcompiler directive to set the public symbol name.