Creating DLL
Create a new project in visual studio community 2017File -> New -> Project
On New Project Dialog
Installed -> Visual C++ -> Windows Desktop -> Windows Desktop Wizard
On Wizard Dialog
Application type -> Dynamic Link Library (.dll)
Additional Info:
check "Empty Project"
uncheck "Precompiled Header"
Add Files to project
Project -> Add New Item -> .cpp/.h ... etc
Set x64 or Win32 Platform as Active Configuration
Solution Explorer -> right click project -> Properties -> Configuration Manager
Code
simple.c
#define EXPORT_FCNS #include "helper.h" #include "simple.h" int sq(int x) { return (x*x); }simple.h
#ifndef SIMPLEH_H #define SIMPLEH_H #include "helper.h" #ifdef __cplusplus extern "C" { #endif EXPORTED_FUNCTION int sq(int x); #ifdef __cplusplus } #endif #endifhelper.h
#ifndef HELPER_H #define HELPER_H #ifdef _WIN32 #ifdef EXPORT_FCNS #define EXPORTED_FUNCTION __declspec(dllexport) #else #define EXPORTED_FUNCTION __declspec(dllimport) #endif #else #define EXPORTED_FUNCTION #endif #endif
Loading DLL on Matlab
Setup compiler
on command window
mex -setup
Script
loadlibrary('simple') libfunctions simple calllib('simple','sq', 3)
No comments:
Post a Comment