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.
Create a MATLAB function foo
.
function c = foo(a) %#codegen c = sqrt(a); end
Save it as foo.m
in a local writable
folder, for example, c:\dll_test
.
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
.
In Microsoft Visual Studio, create an empty Win32 Console Application project. In Microsoft Visual Studio 2013:
On the Start page window, select File > New > Project.
In the New Product dialog box, select Installed > Templates > Visual C++ > Win32 > Win32 Console Application and enter a name.
In the Win32 Application Wizard, select Application Settings. Select the Empty project check box.
Click Finish.
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:
Select Build > Configuration Manager.
In the Configuration Manager, set Active solution platform to match your platform.
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:
Select Build > Configuration Manager.
In the Configuration Manager, set Active
solution configuration to Release
.
Select Project > Properties.
Under Configuration Properties > C/C++ > General, add the folder c:\dll_test\codegen\dll\foo
to Additional
Include Directories.
Under Configuration Properties > Linker > General, add the folder c:\dll_test\codegen\dll\foo
to Additional
Library Directories.
Under Configuration Properties > Linker > Input, add foo.lib
to Additional
Dependencies.
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; }
Select Project > Add Existing Item.
Navigate to the folder that contains the main.c file.
Select the main.c file.
Build the executable. Select Build > Build Solution.
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.
Run the executable.