Portability of WSTP Programs

The Wolfram Language side of a Wolfram Symbolic Transfer Protocol (WSTP) connection is set up to work exactly the same on all computer systems. But inevitably there are differences between external programs on different computer systems.

For a start, different computer systems almost always require different executable binaries. When you call (RefLink[Install, paclet:ref/Install])["prog"], therefore, you must be sure that prog corresponds to a program that can be executed on your particular computer system.

Installing programs on different computer systems.

The Wolfram Language follows the convention that if prog is an ordinary file, then (RefLink[Install, paclet:ref/Install])["prog"] will just try to execute it. But if prog is a directory, then the Wolfram Language will look for a subdirectory of that directory whose name agrees with the current value of (RefLink[$SystemID, paclet:ref/$SystemID]) and will then try to execute a file named prog within that subdirectory.

Typical Unix commands for compiling external programs.

Even though the executable binary of an external program is inevitably different on different computer systems, it can still be the case that the source code in a language such as C from which this binary is obtained can be essentially the same.

But to achieve portability in your C source code, there are several points that you need to watch.

For a start, you should never make use of extra features of the C language or C runtime libraries that happen to be provided on a particular system, but are not part of standard C. In addition, you should try to avoid dealing with segmented or otherwise special memory models.

The include file (RefLink[wstp.h, paclet:ref/file/wstp.h]) contains standard C prototypes for all the functions in the WSTP library.

WSTP functions that use specific C types.

If you are going to call WSTP library functions in a portable way, it is essential that you use the same types as they do.

If your programs correctly match the argument types for the WSTP library functions, you do not have to worry about C type differences between computer systems. WSTP automatically converts the C types to the appropriate sizes for each platform. WSTP also swaps bytes as needed to correctly transfer numbers across platforms and it converts between floating-point number formats with the smallest possible loss of precision.

Manipulating general strings.

In simple C programs, it is typical to use strings that contain only ordinary ASCII characters. But in the Wolfram Language, it is possible to have strings containing all sorts of special characters. These characters are specified within the Wolfram Language using Unicode character codes, as discussed in "Raw Character Encodings".

C language char* strings typically use only 8 bits to store the code for each character. UCS-2 encoded strings, however, require 16 bits. As a result, the functions (NonWLLink[WSPutUnicodeString(), paclet:ref/c/WSPutUnicodeString]) and (NonWLLink[WSGetUnicodeString(), paclet:ref/c/WSGetUnicodeString]) work with arrays of unsignedshort integers. The same is true of UTF-16 encoded strings and the corresponding functions (NonWLLink[WSPutUTF16String(), paclet:ref/c/WSPutUTF16String]) and (NonWLLink[WSGetUTF16String(), paclet:ref/c/WSGetUTF16String]).

UTF-32 encoded strings require 32 bits for each character, and the corresponding functions (NonWLLink[WSPutUTF32String(), paclet:ref/c/WSPutUTF32String]) and (NonWLLink[WSGetUTF32String(), paclet:ref/c/WSGetUTF32String]) work with arrays of unsigned int integers.

If you know that your program will not have to handle special characters, then you may find it convenient to use (NonWLLink[WSPutByteString(), paclet:ref/c/WSPutByteString]) and (NonWLLink[WSGetByteString(), paclet:ref/c/WSGetByteString]). These functions represent all characters directly using 8-bit character codes. If a special character is sent from the Wolfram Language, then it will be converted by (NonWLLink[WSGetByteString(), paclet:ref/c/WSGetByteString]) to a fixed code that you specify.

A point to watch in creating portable WSTP programs.

Computer systems and compilers that have C runtime libraries based on the Unix model allow WSTP programs to have a main program of the form main(argc,argv) that simply calls (NonWLLink[WSMain, paclet:ref/c/WSMain])(argc,argv).

Some computer systems or compilers may, however, require main programs of a different form. You should realize that you can do whatever initialization you want inside main() before calling (NonWLLink[WSMain(), paclet:ref/c/WSMain]). Once you have called (NonWLLink[WSMain(), paclet:ref/c/WSMain]), however, your program will effectively go into an infinite loop, responding to requests from the Wolfram Language until the link to it is closed.