WSTP Development in C (Mac OS X)
Supported Development Platforms | Running WSTP Programs |
Installing the WSTP Components | Troubleshooting |
Building WSTP Programs |
Recommended Installation
CompilerAdditions Installation
WSTPExamples Installation
WSTP Framework Components
CompilerAdditions Directory
wstp.h
libWSTPi4.a
wstp.framework
wsprep
WSTPExamples Directory
PrebuiltExamples Directory
Using WSTP Template Files
wsprep addtwo.tm -o addtwotm.c
addtwotm.c : addtwo.tm
wsprep addtwo.tm -o addtwotm.c
Building WSTP Programs from the Command Line
Using a Makefile
# This makefile can be used to build all or some of the sample
# programs. To build all of them, use the command
# 'make all'. To build one, say addtwo, use the command
# 'make addtwo'.
WSLINKDIR = /Applications/Mathematica.app/Contents/SystemFiles/Links/WSTP/DeveloperKit
SYS = MacOSX-x86-64
CADDSDIR = ${WSLINKDIR}/${SYS}/CompilerAdditions
INCDIR = ${CADDSDIR}
LIBDIR = ${CADDSDIR}
WSPREP = ${CADDSDIR}/wsprep
EXTRA_LIBS = -lc++ -framework Foundation
WSTP_LIB = -lWSTPi4
all : addtwo bitops counter factor factor2 factor3 quotient reverse sumalist
addtwo : addtwotm.o addtwo.o
${CC} -I${INCDIR} addtwotm.o addtwo.o -L${LIBDIR} ${WSTP_LIB} ${EXTRA_LIBS} -o $@
bitops : bitopstm.o bitops.o
${CC} -I${INCDIR} bitopstm.o bitops.o -L${LIBDIR} ${WSTP_LIB} ${EXTRA_LIBS} -o $@
counter : countertm.o
${CC} -I${INCDIR} countertm.o -L${LIBDIR} ${WSTP_LIB} ${EXTRA_LIBS} -o $@
factor : factor.o
${CC} -I${INCDIR} factor.o -L${LIBDIR} ${WSTP_LIB} ${EXTRA_LIBS} -o $@
factor2 : factor2.o
${CC} -I${INCDIR} factor2.o -L${LIBDIR} ${WSTP_LIB} ${EXTRA_LIBS} -o $@
factor3 : factor3.o
${CC} -I${INCDIR} factor3.o -L${LIBDIR} ${WSTP_LIB} ${EXTRA_LIBS} -o $@
quotient : quotient.o
${CC} -I${INCDIR} quotient.o -L${LIBDIR} ${WSTP_LIB} ${EXTRA_LIBS} -o $@
reverse : reversetm.o
${CC} -I${INCDIR} reversetm.o -L${LIBDIR} ${WSTP_LIB} ${EXTRA_LIBS} -o $@
sumalist : sumalisttm.o sumalist.o
${CC} -I${INCDIR} sumalisttm.o sumalist.o -L${LIBDIR} ${WSTP_LIB} ${EXTRA_LIBS} -o $@
.c.o :
${CC} -c -I${INCDIR} $<
addtwotm.c : addtwo.tm
${WSPREP} $? -o $@
bitopstm.c : bitops.tm
${WSPREP} $? -o $@
countertm.c : counter.tm
${WSPREP} $? -o $@
reversetm.c : reverse.tm
${WSPREP} $? -o $@
sumalisttm.c : sumalist.tm
${WSPREP} $? -o $@
Library Dependencies
Building Mac OS X WSTP Programs with Xcode
Xcode 6.x+
Creating a Project for addtwo
2. From the File menu, choose New ▶ Project.
The Choose a template for your new project sheet appears.
3. Under the OS X section click Application. Then click the Command Line Tool icon. Click Next.
The Choose options for your new project sheet appears.
4. Enter addtwo in the Product Name text box. From the Type drop-down menu, select C. Uncheck the Use Automatic Reference Counting checkbox. Click Next.
The Choose Folder sheet opens.
5. Create a directory in your file system for the addtwo project. You may choose to check or uncheck the Source Control checkbox according to your need to have the addtwo project be a git repository. Click Create.
Xcode opens the addtwo worksheet.
6. Copy the source files to the project directory.
Start the Terminal application (/Applications/Terminal.app) and change (cd) to the directory where the Wolfram System was installed. When you reach that directory, cd to $InstallationDirectory/SystemFiles/Links/WSTP/DeveloperKit/MacOSX-x86-64/WSTPExamples.
cd "/Applications/Mathematica.app/Contents/SystemFiles/Links/WSTP/DeveloperKit/MacOSX-x86-64/WSTPExamples"
Copy the addtwo source files to your project directory that you chose in step 4 using the copy (cp) command.
7. Run wsprep on the template file.
Change directory to the project directory.
Use wsprep to generate a source file.
/Applications/Mathematica.app/Contents/SystemFiles/Links/WSTP/DeveloperKit/MacOSX-x86-64/CompilerAdditions/wsprep addtwo.tm -o addtwo.tm.c
8. Add the files to the project.
- From the File Chooser sheet select addtwo.c, make sure the checkbox for addtwo in the Add to targets section is checked, and click Add.
- From the File Chooser sheet select addtwo.tm.c, make sure the checkbox for addtwo in the Add to targets section is checked, and click Add.
- Click the Finder icon in the dock to make the finder active. Go to the folder where the Wolfram System is installed.
- Click the Mathematica icon while pressing the Ctrl key. A popup menu will appear. Select Show Package Contents to view the contents of Mathematica. Open the folders Contents/SystemFiles/Links/WSTP/DeveloperKit/MacOSX/CompilerAdditions.
- Drag the file wstp.h from the Finder window to the addtwo folder icon in the Navigator area of the Xcode addtwo workspace window. The Choose options for adding these files sheet appears. Make sure that Copy items into destination folder is not checked and click the Finish button.
- Drag the file libWSTPi4.a from the Finder window to the addtwo folder icon in the Navigator area of the Xcode addtwo workspace window. Make sure that Copy items into destination folder is not checked and click the Finish button.
- Click the blue project icon for addtwo in the Navigator area of the Xcode addtwo workspace window. Click the addtwo target under the TARGETS section. Click the Build Settings tab. Browse to the Linking ▶ Other Linker Flags element and add -lc++ and -framework Foundation to the text entry pane.
9. Remove main.c from the project.
Right-click the file main.c and select Delete. In the drop-down menu click the Move to Trash button.
From the Project menu, select Build.
Creating a Project for Factor
2. From the File menu, choose New ▶ Project.
The Choose a template for your new project sheet appears.
3. Under the OS X section, click Application. Then click the Command Line Tool icon. Click Next.
The Choose options for your new project sheet appears.
4. Enter "factor" in the Product Name text box. From the Type drop-down menu, select C. Uncheck the Use Automatic Reference Counting checkbox. Click Next.
The Choose Folder sheet opens.
5. Create a directory in your file system for the factor project. You may choose to check or uncheck the Source Control checkbox according to your need to have the addtwo project be a git repository. Click Create.
Xcode opens the addtwo worksheet.
6. Copy the source files to the project directory
Start the Terminal application (/Applications/Terminal.app) and change (cd) to the directory where the Wolfram System was installed. When you reach that directory, cd to $InstallationDirectory/SystemFiles/Links/WSTP/DeveloperKit/MacOSX-x86-64/WSTPExamples.
cd "/Applications/Mathematica.app/Contents/SystemFiles/Links/WSTP/DeveloperKit/MacOSX-x86-64/WSTPExamples"
Copy the factor source files to your project directory that you chose in step 4 using the copy (cp) command.
7. Add the files to the project.
- From the File Chooser sheet select factor.c, make sure the checkbox for factor in the Add to targets section is checked, and click Add.
- Click the Finder icon in the dock to make the finder active. Go to the folder where the Wolfram System is installed.
- Click the Mathematica icon while pressing the Ctrl key. A popup menu will appear. Select Show Package Contents to view the contents of Mathematica. Open the folder Contents/SystemFiles/Links/WSTP/DeveloperKit/MacOSX/CompilerAdditions.
- Drag the file wstp.h from the Finder window to the factor folder icon in the Navigator area of the Xcode factor workspace window. The Choose options for adding these files sheet appears. Make sure that Copy items into destination folder is not checked and click the Finish button.
- Drag the file libWSTPi4.a from the Finder window to the factor folder icon in the Navigator area of the Xcode factor workspace window. Make sure that Copy items into destination folder is not checked and click the Finish button.
- Click the blue project icon for factor in the Navigator area of the Xcode addtwo workspace window. Click the factor target under the TARGETS section. Click the Build Settings tab. Browse to the Linking ▶ Other Linker Flags element and add -lc++ and -framework Foundation to the text entry pane.
8. Remove main.c from the project.
Right-click the file main.c and select Delete. In the drop-down menu, click the Move to Trash button.
From the Project menu, select Build.
Using wscc
wscc addtwo.tm addtwo.c -o addtwo
Running a Prebuilt Example from the Wolfram Language Kernel
:Begin:
:Function: addtwo
:Pattern: AddTwo[i_Integer, j_Integer]
:Arguments: { i, j }
:ArgumentTypes: { Integer, Integer }
:ReturnType: Integer
:End:
:Evaluate: AddTwo::usage = "AddTwo[x, y] gives the sum of two machine integers x and y."
int addtwo( int i, int j)
{
return i+j;
}
int main(int argc, char* argv[])
{
return WSMain(argc, argv);
}
Invoking the Wolfram Language Kernel from within a Prebuilt Example
factor -linkmode launch -linkname '"/Applications/Mathematica.app/Contents/MacOS/WolframKernel" -wstp'
Supported Link Protocols
- Turn off compiler optimization until your program is working. This makes compiling faster, debugging easier, and, besides, the optimizer may be broken and the cause of some problems. (Optimized code uses the stack and registers differently than unoptimized code in such a way that may expose or mask a bug in your code. For example, the common mistake of returning a pointer to a local variable may or may not cause a problem depending on stack and register use.)
- Check the return values from the WSTP library functions or call WSError() at key points in your program. WSTP will often be able to tell you what has gone wrong.
- The files wstp.h and libWSTPi4.a are a matched set. If you have used an earlier release of WSTP, you should take care that you do not mix components when building your application.
- Check whether the C compiler you are using supports prototypes. If it does not, you will need to change your code and the way you build your project. This is explained in the section "Building WSTP Programs".