Main Content

Use a Dynamic Library in a Microsoft Visual Studio Project

This example shows how to create and configure a simple Microsoft® Visual Studio® project that calls a dynamic library (DLL) generated by MATLAB® Coder™. The example uses Microsoft Visual Studio 2017. In other versions of Microsoft Visual Studio, you might encounter a different procedure.

Generate a C Dynamic Library

  1. Create a MATLAB function foo.

    function c = foo(a) 
    %#codegen
      c = sqrt(a);
    end
    
  2. Save it as foo.m in a local writable folder, for example, C:\dll_test.

  3. Use the same version of the same compiler to generate your DLL that you use to build your Microsoft Visual Studio project. Otherwise, you can encounter linking errors.

    For this example, use the Microsoft Visual Studio 2017 compiler. To select the compiler that the code generator uses, enter mex -setup at the command line. For more information, see Supported and Compatible Compilers.

  4. Generate a DLL for the MATLAB function foo. The -args option specifies that the input a is a real double.

    codegen -config:dll foo -args {0} -report

    On Microsoft Windows® systems, codegen generates a C dynamic library, foo.dll, and supporting files in the default folder, C:\dll_test\codegen\dll\foo.

Create a Microsoft Visual Studio Project

In Microsoft Visual Studio, create an Empty Project:

  1. Select File > New > Project.

  2. Select Installed > Visual C++ > General and select Empty project. Enter a project name.

  3. Click OK.

Create a main.c File That Uses the Library

Write a main.c file that uses foo.dll. The main.c function must:

  • Include the generated header files, which contain the function prototypes for the library functions.

  • Call the terminate function after calling the library function for the last time.

By default, the code generator includes a call to the initialize function at the beginning of the generated C/C++ entry-point functions. So, you do not need to call the initialize function from main.c. See Use Generated Initialize and Terminate Functions.

To create the file:

  1. From the Solution Explorer, right-click the Source Files folder and select Add > New Item

  2. Select C++ File (.cpp). In the Name field, enter main.c.

  3. Click Add.

  4. Enter the code:

    #include "foo.h"
    #include "foo_terminate.h"
    #include <stdio.h>
    
    int main()
    {
      printf("%f\n", foo(26));
      foo_terminate();
      getchar();
      return 0;
    }

Configure the Platform

MATLAB Coder automatically uses a toolchain configured to build a 64-bit DLL. By default, Microsoft Visual Studio is configured to build for the Win32 platform. You must change the build platform to x64 to match the generated 64-bit DLL. In Microsoft Visual Studio:

  1. Select Build > Configuration Manager.

  2. Set Active solution platform to x64.

If you want to build a 32-bit DLL on a 64-bit platform, you must use a 32-bit toolchain definition. See Build 32-bit DLL on 64-bit Windows® Platform Using MSVC Toolchain.

Specify External Dependencies

To build your project, the compiler requires the associated header files. The linker requires the generated .lib files.

  1. Highlight your project in the Solution Explorer, and then select Project > Properties.

  2. The code generator produces types in the file rtwtypes.h, which includes the file tmwtypes.h. This file is stored in matlabroot\extern\include, where matlabroot is the root directory of the MATLAB installation. To return the root directory, enter matlabroot in the Command Window.

    Under Configuration Properties > C/C++ > General, add the folders C:\dll_test\codegen\dll\foo and matlabroot\extern\include to Additional Include Directories. Separate the entries with a semicolon.

  3. Under Configuration Properties > Linker > Input, add foo.lib to Additional Dependencies.

  4. Under Configuration Properties > Linker > General, add the folder C:\dll_test\codegen\dll\foo to Additional Library Directories.

Build and Run the Executable

  1. Build the executable. Select Build > Build Solution.

  2. Make the DLL accessible to the executable. Either copy foo.dll to the folder containing the executable or add the folder containing foo.dll to your path.

  3. Run the executable. Verify that the output appears as you expect.

Related Topics