11 responses to “How to write a .dll file in Windows 32 bit and 64 bit and Compile in Matlab 32 bit and 64 bit and how to write a .so file in Ubuntu (Linux) and Compile in 64 bit Matlab”

  1. Fabian

    Thanks for this usefull blog entry!

  2. Xin

    Thank you very much for your sharing.
    I also installed Microsoft Visual Studio 2008 Pro SP1 and MATLAB 2009b on my computer. But When I tried to use ‘loadlibrary’, it always came out an error that ‘fatal error C1083: Cannot open include file:’test.h’: No such file or directory’
    Do you have any idea where the problem is?
    Any help would be appreciated.


  3. Xin

    Hi Todd,
    Thank you very much for your reply.
    I am sure that I chose full install during the install of visual studio. But I got the following in MATLAB:
    >> !set VS
    VS90COMNTOOLS=C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\Tools\
    >> !”%VS90COMNTOOLS%..\..\VC\vcvarsall.bat”
    Setting environment for using Microsoft Visual Studio 2008 x86 tools.

    I think MATLAB is using the x86 compiler instead of x64. Do you know how to check the x64 compiler? I have re-installed the visual studio several times, and it still goes like this.
    Thanks a lot for your help.


  4. Tobias


    I downloaded Microsoft SDK with the “x64 Compilers and Tools”. I have VS2008 Professional from before. When I follow the example on this page, for the case “Windows 64 bit with 64 bit MATLAB”, I get the following error:

    ??? Error using ==> loadlibrary at 368
    Failed to preprocess the input file.
    Output from preprocessor is:’cl’ is not recognized as an internal or external command, operable program or batch file.

    The error comes when loadlibrary is trying to evaluate: [res,ccout]=system(preprocess_command),


    preprocess_command =

    “%VS90COMNTOOLS%..\..\VC\vcvarsall.bat” amd64>nul&cl -nologo -I”C:\Program Files\Matlab\extern\include” -E “C:\Users\myUser\Desktop\Loadlibrary test\Test.h” > “Test.i”

    If I remove the call to system(); I get the following error message later in the code:

    ??? Error using ==> loadlibrary at 388
    Call to Perl failed. Possible error processing header file.
    Output of Perl command:
    Can not open file Test.i because No such file or directory at C:\Program
    Files\Matlab\toolbox\matlab\general\private\prototypes.pl line 34.

    Can you help me please? What should I do?

  5. guest

    Oh Yes. 6 houre searching an dnow i have the answer. you are my new hero for today!
    thanks soooooo much!


  6. Nick


    I tried the Win64/Matlab64 example but when building the DLL I received the error: C1853: ‘x64\Release\test.pch’ precompiled header is from a previous version of the compiler, or the precompiled header is C++ and you are using it from C (or vice cersa)

    I tried renaming test.cpp and it will build and loadlibrary in Matlab but not calllib, any ideas? thank you

  7. Dinu

    Great Tutorial! Thanks a lot!

    One small thing: before running loadlibrary(…), one should make sure the right compiler is selected: mex -setup

Leave a Reply