8
$\begingroup$

I recently picked up an old project involving WSTP (formerly MathLink) programs, and as a starting place tried to get the demos provided by Wolfram to compile. The instructions were dated, referring to Visual Studio 2010 and 2012, and key sections discussed only command line tools under Visual Studio 2012. I had no luck getting anything to compile or build.

I couldn't even get wsprep to output a .c file.

Has anybody had any luck with WSTP under the Visual Studio 2015 IDE? I'd love to hear what settings you used, etc.

[update, Feb 2016. To make wsprep work, you have to launch it from the Developer Command Prompt launched as administrator. I have gotten my code to compile and link under Visual Studio 2015's IDE in 32 bit; no luck with 64 bit so far

update 2: I have the debugger working too, in 32 bit].

$\endgroup$
  • $\begingroup$ I suppose you have seen WSTP and External Program Communication and have checked WSTP Developer Guide—Windows $\endgroup$ – user9660 Dec 2 '15 at 6:12
  • $\begingroup$ @Lou Wolfram tells me they will update those pages. In the meantime, I have had no luck trying to translate them to the modern Visual Studio IDE. I'm hoping somebody out there may already have done so. $\endgroup$ – Michael Stern Dec 3 '15 at 20:33
  • 1
    $\begingroup$ I can compile with VS2015 in command line mode. But I don't know how to use the graphical interface. If you are fine with command line mode (call cl.exe directly), I can post an answer. But I have no patience for the GUI ... $\endgroup$ – Szabolcs Feb 19 '16 at 14:44
  • $\begingroup$ I guess then you're not interested in the command line workflow, only the GUI one? $\endgroup$ – Szabolcs Feb 22 '16 at 13:10
4
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ I think the OP was asking about compiling an installable MathLink program, not a LibraryLink library. If you are working with LibraryLink, isn't it much simpler to leave the compilation and linking to the CreateLibrary function? With older Mathematica and newer VS just a tiny patch is necessary: mathematica.stackexchange.com/a/41691/12 Keep the ML functions, they work fine. Furthermore, CreateLibrary expects that they are being used. No need to rename everything to WS. $\endgroup$ – Szabolcs Jun 18 '16 at 21:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.