I have to generate an interface to Mathematica for a fairly large library. I would like to use LibraryLink, as the generation seems much simpler. The platform is Windows (VS 2012), and I am wondering what the constraints are with regards to compiler options, etc. First and foremost: can I compile in 32 bit mode, or does it have to be 64 bits? (The CygWin toolkit file tells me that MathKernel.exe is a PE32+ executeable, whatever that is, and that the architecture is x86-64, which is 64 bits.) Would there be any library incompatibilities when compiling with VS2012 (VC11)? And are there any options I must respect?

  • $\begingroup$ You should have two kernels on Windows: a 32-bit one and a 64-bit one. By default it'll run the 64 bit one on a 64 bit system. About LibraryLink: do you need to call Mathematica from another program, or do you need to call that library from Mathematica? LibraryLink can do the latter, but not the former. If you need to call Mathematica from another program, you will need to use MathLink. Also, I believe with MathLink it's possible to create an interface which will work with both the 64 and 32 bit versions (you won't need to make two interfaces, like with LibraryLink) $\endgroup$
    – Szabolcs
    Dec 10, 2012 at 18:19
  • $\begingroup$ If you are interested in Windows only, .NETLink is also an option for creating interfaces to libraries. I just remembered that both the 32-bit and 64-bit versions of .NETLink can be loaded into a 64-bit MathKernel, so that confirms that if you use MathLink (like .NETLink does), then you only need to create one interface that'll work with both kernels. $\endgroup$
    – Szabolcs
    Dec 10, 2012 at 18:21
  • $\begingroup$ We're calling our library from Mathematica. From what I've seen LibraryLink should be a lot simpler than MathLink to implement. As for .NETLink, our code isn't managed. And although we are currently targetting only Windows, in the past, I've had to do more than one port to Unux of code that was only targetting Windows. $\endgroup$ Dec 10, 2012 at 18:54
  • $\begingroup$ You can use .NETLink to call non-managed code, but I agree with you that for what you want to achieve, LibraryLink is probably the best solution. This might also be an interesting read: mathematica.stackexchange.com/questions/8438/… $\endgroup$
    – Szabolcs
    Dec 10, 2012 at 19:23

1 Answer 1


Use the "TargetSystemID" option to CreateLibrary (this exists for CreateExecutable and CreateObjectFile as well) to compile for a 32-bit or 64-bit target.

"TargetSystemID" -> "Windows" generates a 32-bit binary "TargetSystemID" -> "Windows-x86-64" generates a 64-bit binary

If you want to use this library from Mathematica, create a Mathematica package MyPackage, and include the dynamic libraries here:


In MyPackage.m, point to the library mylib (the name passed to CreateLibrary):

$LibraryResourcesPath =

If[FreeQ[$LibraryPath, $LibraryResourcesPath],
    PrependTo[$LibraryPath, $LibraryResourcesPath]

$MyLibrary = FindLibrary["mylib"]

Now you can call LibraryFunctionLoad on $MyLibrary.

  • $\begingroup$ I don't want to compile my library from Mathematica; it is a large, existing library, already embedded in Excel, Python and Java. It consists of about 20 DLLs, and is developed in Visual Studios. I need to know what the constraints are on the compiler options I use in Visual Studios. $\endgroup$ Dec 20, 2012 at 9:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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