The following approach works for me with a Vanilla Visual Studio 2015 installation (14.0) and Mathematica 10.2 (for Windows 8.1, x64).
- Open vs2015.
- Create a new empty project, let us call it
LibraryLinkDemo
. I always uncheck "Create Directory for solution", but it should work with that on.
- Change the platform to x64.
- Open the property pages for the project. Select Configuration: All Configurations and make sure the platform is x64 (current).
- In the General section, change Configuration Type from Application (.exe) to Dynamic Library (.dll).
- In the same section, change Output Directory to
$(ProjectDir)
.
- In the VC++ Directories section, add the following Include Directories (the paths might be slightly different on your installation).
- C:\Program Files\Wolfram Research\Mathematica\10.2\SystemFiles\IncludeFiles\C
- C:\Program Files\Wolfram Research\Mathematica\10.2\SystemFiles\Links\WSTP\DeveloperKit\Windows-x86-64\CompilerAdditions\mldev64\include
- Add the following to Library Directories: C:\Program Files\Wolfram Research\Mathematica\10.2\SystemFiles\Links\WSTP\DeveloperKit\Windows-x86-64\CompilerAdditions\mldev64\lib
Now we are ready to add some code. Let's compile the LinkObject demo.
- Locate the demo WolframLibrary source files, they are at
FileNameJoin[{FileNameDrop[NotebookDirectory[], -3],
"LibraryResources", "Source"}]
which for me is
C:\Program Files\Wolfram Research\Mathematica\10.2\SystemFiles\Links\LibraryLink\LibraryResources\Source
- Copy
demo_LinkObject.c
to your project folder, then drag-and-drop that copy into the solution explorer to add it to the project.
In that file, make the following changes to update it to the new name wstp (which apparently replaced mathlink):
- Replace #include "mathlink.h" with #include "wstp.h"
- Replace each occurrence of MLINK with WSLINK.
- Replace in each identifier each initial ML with WS.
Add wstp64i4m.lib to the linked libraries. This can be done by adding the line #pragma comment(lib, "wstp64i4m.lib")
at the beginning of the file.
Now you should be able to compile (hit F5). It will complain that it cannot debug this, but should report that the .dll file was created.
Good, now create a notebook file, call it LibraryLinkDemo.nb and save it in the project directory where there should now be a LibraryLinkDemo.dll
.
Let us test the library with the following code adapted from LibraryLink/tutorial/InteractionWithMathematica#306127487
:
funLink=LibraryFunctionLoad[FileNameJoin[{NotebookDirectory[], "LibraryLinkDemo.dll"}],"reverseString", LinkObject,LinkObject]
funLink["forwards"]
If everything worked, you should get "sdrawrof".
Aside: The .lib file
There are other ways to add the wstp64i4m.lib
:
- Drag and drop the lib file from its folder into the project. If you do this you don't even have to add the library directory.
- Go to the project properties, Linker > Input > Additional Dependencies and add
wstp64i4m.lib
, or even the full path (in quotes), "C:\Program Files\Wolfram Research\Mathematica\10.2\SystemFiles\Links\WSTP\DeveloperKit\Windows-x86-64\CompilerAdditions\mldev64\lib\wstp64i4m.lib" (right click the file in explorer holding shift and choose "Copy as path" to obtain this name). Using the full path also removes the need to add the library directory.
Note: It will not compile with the wstp64i4s.lib
because that was built with an older vs compiler, and Visual Studio has never been binary downwards compatible with static libraries.
Aside: LoadLibrary
Note that you can omit the .dll ending.
Also, you can omit the path if you execute AppendTo[$LibraryPath, NotebookDirectory[]]
. Note however that this change is permanent.
Debugging
Assuming Mathematica is running and you executed the above commands, you can now attach the Visual Studio debugger as follows:
- In the code, set a breakpoint at the first line of reverseString.
Debug > Attach to Process. Select "WolframKernel.exe". If there are multiple, use the try-and-error strategy to find the one you're looking for. Make sure the Output window says
'WolframKernel.exe' (Win32): Loaded '...\LibraryLinkDemo.dll'. Symbols loaded.
after attaching.
Now execute the above code again. The breakpoint should be hit. Note that the Kernel and front-end hang while the function is being executed. Do not try to interact with mathematica during that time, otherwise windows might erroneously suggest to kill the process losing eventual unsaved changes.
If not, try LibraryUnload@
FileNameJoin[{NotebookDirectory[], "LibraryLinkDemo.dll"}]
and rebuilding the dll.
Final note
I probably misinterpreted your post. I was thinking you meant a WolframLibrary using WSTP, not a standalone program. But I guess most of the steps apply.
cl.exe
directly), I can post an answer. But I have no patience for the GUI ... $\endgroup$ – Szabolcs Feb 19 '16 at 14:44