Use a C Dynamic Library in a Microsoft Visual Studio Project

This example shows how to create and configure a simple Microsoft® Visual Studio® Win32 Console Application project that calls a dynamic library (DLL) that MATLAB® Coder™ generates. This example uses Microsoft Visual Studio 2013. 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. Generate a DLL for the MATLAB function foo. Use the -args option to specify that the input a is a real double.

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

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

Create a Microsoft Visual Studio Project

In Microsoft Visual Studio, create an empty Win32 Console Application project. In Microsoft Visual Studio 2013:

  1. On the Start page window, select File > New > Project.

  2. In the New Product dialog box, select Installed > Templates > Visual C++ > Win32 > Win32 Console Application and enter a name.

  3. In the Win32 Application Wizard, select Application Settings. Select the Empty project check box.

  4. Click Finish.

Configure the Platform

Verify that the project configuration specifies the architecture that matches your computer. By default, MATLAB Coder builds a DLL for the platform that you are working on, but Microsoft Visual Studio builds for Win32. In Microsoft Visual Studio 2013:

  1. Select Build > Configuration Manager.

  2. In the Configuration Manager, set Active solution platform to match your platform.

Configure the Solution Version

Configure the project to use the release version of the C run-time library. By default, the Microsoft Visual Studio project uses the debug version of the C run-time library. However, by default, the DLL that MATLAB Coder generates uses the release version. In Microsoft Visual Studio 2013:

  1. Select Build > Configuration Manager.

  2. In the Configuration Manager, set Active solution configuration to Release.

Configure Additional Directories and Dependencies

  1. Select Project > Properties.

  2. Under Configuration Properties > C/C++ > General, add the folder c:\dll_test\codegen\dll\foo to Additional Include Directories.

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

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

Create a main.c File

Create a main.c file that calls foo.dll. The main.c function must:

  • Include the generated header file, which contains the function prototypes for the library function.

  • Call the initialize function before calling the library function for the first time.

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

For example:

#include "foo.h"
#include "foo_initialize.h"
#include "foo_terminate.h"
#include <stdio.h>


int main()
{
   foo_initialize();
   printf("%f\n", foo(25));
   foo_terminate();
	  getchar();
	  return 0;
}

Add the main.c File to the Project

  1. Select Project > Add Existing Item.

  2. Navigate to the folder that contains the main.c file.

  3. Select the main.c file.

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.

More About

Was this topic helpful?