Use a Dynamic Library in a Microsoft Visual Studio Project - MATLAB & Simulink (original) (raw)

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, entermex -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 > > .
  2. Select > > 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. Themain.c function must:

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 >
  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 > .
  2. Set Active solution platform tox64.

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 > .
  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, wherematlabroot is the root directory of the MATLAB installation. To return the root directory, entermatlabroot in the Command Window.
    Under > > , add the folders C:\dll_test\codegen\dll\foo and_matlabroot_\extern\include toAdditional Include Directories. Separate the entries with a semicolon.
  3. Under > > , add foo.lib to Additional Dependencies.
  4. Under > > , add the folder C:\dll_test\codegen\dll\foo toAdditional Library Directories.

Build and Run the Executable

  1. Build the executable. Select > .
  2. Make the DLL accessible to the executable. Either copy foo.dll to the folder containing the executable or add the folder containingfoo.dll to your path.
  3. Run the executable. Verify that the output appears as you expect.