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
- Create a Microsoft Visual Studio Project
- Create a main.c File That Uses the Library
- Configure the Platform
- Specify External Dependencies
- Build and Run the Executable
Generate a C Dynamic Library
- 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
. - 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. - Generate a DLL for the MATLAB function
foo
. The-args
option specifies that the inputa
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:
- Select > > .
- Select > > and select Empty project. Enter a project name.
- Click OK.
Create a main.c File That Uses the Library
Write a main.c
file that uses foo.dll
. Themain.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:
- From the Solution Explorer, right-click the Source Files folder and select >
- Select C++ File (.cpp). In the Name field, enter
main.c
. - Click Add.
- 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:
- Select > .
- 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.
- Highlight your project in the Solution Explorer, and then select > .
- The code generator produces types in the file
rtwtypes.h
, which includes the filetmwtypes.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 foldersC:\dll_test\codegen\dll\foo
and_matlabroot_\extern\include
toAdditional Include Directories. Separate the entries with a semicolon. - Under > > , add
foo.lib
to Additional Dependencies. - Under > > , add the folder
C:\dll_test\codegen\dll\foo
toAdditional Library Directories.
Build and Run the Executable
- Build the executable. Select > .
- 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. - Run the executable. Verify that the output appears as you expect.