前段时间完成了一次生成动态链接库并使用,没有做记录,结果又花了两天重新探索过程,及时记录非常有必要。

使用软件版本:matlab R2017a&R2017b , vs 2013 & vs2015。
完整的测试文件我已经发布至这里,可以免积分下载(该文件使用了R2017a)。

1.新建simulink模型,如下图所示。

2.配置仿真条件。

3 . 编译生成C代码

4. 编译生成动态链接库文件
上一级目录(与你的simulink模型同级的目录)得到

这里还有另外一种方法就是用nmake 编译其中的matlab_sourcecode.mk(makefile文件),这种方法会生成完整的*.exe可执行文件,找到其中的上图三个文件拷贝出来,一样可以使用。

5.使用动态链接库文件。
然后就可以在新的程序当中引用动态链接库了,比如我重新在matlab中使用该动态链接库:
用c写S-function :testsfun.c,然后执行mex testsfun.c 编译一次,就可以使用了。

以下为原simulink模型与动态链接库运行的结果对比,可以看到两个完全一致。

遇到的问题:不知道为什么,我第一次新建的simulink文件生成的代码始终有问题,我查看了生成的文件,在生成文件类型和内部变量的命名上都会有差异。生成dll没有问题,但是在使用时,每次运行都会导致软甲崩溃,必须重启才能使用。下边将部分差别标出来。
7.引用动态链接库所使用的文件testsfun.c & rtwdemo.h

 /*  File    : testsfun.c*  Abstract:**      A one integration-step delay and hold "memory" function.**      Syntax:  [sys, x0] = sfunmem(t,x,u,flag)**      This file is designed to be used in a Simulink S-Function block.*      It performs a one integration-step delay and hold "memory" function.*      Thus, no matter how large or small the last time increment was in*      the integration process, this function will hold the input variable*      from the last integration step.**      Use this function with a clock as input to get the step-size at each*      step of a simulation.**      This function is similar to the built-in "Memory" block.**  Copyright 1990-2013 The MathWorks, Inc.*/#define S_FUNCTION_NAME testsfun
#define S_FUNCTION_LEVEL 2#include <string.h>
#include "simstruc.h"
#include <windows.h>#define GETSYMBOLADDR GetProcAddress
#define LOADLIB LoadLibrary
#define CLOSELIB FreeLibrary#define U(element) (*uPtrs[element])  /* Pointer to Input Port0 */#include "rtwdemo.h"int32_T i;
void* handleLib;
void (*mdl_initialize)(boolean_T);
void (*mdl_step)(void);
void (*mdl_terminate)(void);
uint8_T (*sum_outptr);
ExternalInputs_test (*mdl_Uptr);
ExternalOutputs_test (*mdl_Yptr);/*====================** S-function methods **====================*//* Function: mdlInitializeSizes ===============================================* Abstract:*    Call mdlCheckParameters to verify that the parameters are okay,*    then setup sizes of the various vectors.*/
static void mdlInitializeSizes(SimStruct *S)
{ssSetNumSFcnParams(S, 0);  /* Number of expected parameters */if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {return; /* Parameter mismatch will be reported by Simulink */}handleLib = LOADLIB("./matlab_sourcecode_win64.dll");if (!handleLib) {printf("Cannot open the specified shared library.\n");return(-1);}ssSetNumContStates(S, 0);ssSetNumDiscStates(S, 0);if (!ssSetNumInputPorts(S, 2)) return;ssSetInputPortWidth (S, 0, 1);ssSetInputPortWidth (S, 1, 1);ssSetInputPortDirectFeedThrough(S, 0, 1);if (!ssSetNumOutputPorts(S, 1)) return;ssSetOutputPortWidth(S, 0, 1);ssSetNumSampleTimes(S, 1);ssSetNumRWork(S, 0);ssSetNumIWork(S, 0);ssSetNumPWork(S, 0);ssSetNumModes(S, 0);ssSetNumNonsampledZCs(S, 0);ssSetSimStateCompliance(S, USE_DEFAULT_SIM_STATE);ssSetOptions(S,SS_OPTION_WORKS_WITH_CODE_REUSE |SS_OPTION_EXCEPTION_FREE_CODE |SS_OPTION_USE_TLC_WITH_ACCELERATOR);#if (defined(LCCDLL)||defined(BORLANDCDLL))/* Exported symbols contain leading underscores when DLL is linked with LCC or BORLANDC */mdl_initialize =(void(*)(boolean_T))GETSYMBOLADDR(handleLib , "_matlab_sourcecode_initialize");mdl_step       =(void(*)(void))GETSYMBOLADDR(handleLib , "_matlab_sourcecode_step");
//         mdl_terminate  =(void(*)(void))GETSYMBOLADDR(handleLib , "_matlab_sourcecode_terminate");mdl_Uptr       =(ExternalInputs_test*)GETSYMBOLADDR(handleLib , "_matlab_sourcecode_U");mdl_Yptr       =(ExternalOutputs_test*)GETSYMBOLADDR(handleLib , "_matlab_sourcecode_Y");
//         sum_outptr     =(uint8_T*)GETSYMBOLADDR(handleLib , "_sum_out");#else mdl_initialize =(void(*)(boolean_T))GETSYMBOLADDR(handleLib , "matlab_sourcecode_initialize");mdl_step       =(void(*)(void))GETSYMBOLADDR(handleLib , "matlab_sourcecode_step");
//         mdl_terminate  =(void(*)(void))GETSYMBOLADDR(handleLib , "matlab_sourcecode_terminate");mdl_Uptr       =(ExternalInputs_test*)GETSYMBOLADDR(handleLib , "matlab_sourcecode_U");mdl_Yptr       =(ExternalOutputs_test*)GETSYMBOLADDR(handleLib , "matlab_sourcecode_Y");
//         sum_outptr     =(uint8_T*)GETSYMBOLADDR(handleLib , "sum_out");#endifmdl_initialize(1);
}/* Function: mdlInitializeSampleTimes =========================================* Abstract:*    S-function is continuous, fixed in minor time steps.*/
static void mdlInitializeSampleTimes(SimStruct *S)
{ssSetSampleTime(S, 0, CONTINUOUS_SAMPLE_TIME);ssSetOffsetTime(S, 0, FIXED_IN_MINOR_STEP_OFFSET);ssSetModelReferenceSampleTimeDefaultInheritance(S);
}#define MDL_INITIALIZE_CONDITIONS
/* Function: mdlInitializeConditions ========================================* Abstract:*    The memory state is stored in the RWork vector, initialize it to zero*/
static void mdlInitializeConditions(SimStruct *S)
{//     real_T *rwork = ssGetRWork(S);
//     int_T  i;
//
//     for (i = 0; i < ssGetNumRWork(S); i++) {//         rwork[i] = 0.0;
//     }
}/* Function: mdlOutputs =======================================================* Abstract:**      y = rwork*/
static void mdlOutputs(SimStruct *S, int_T tid)
{real_T           *y    =  ssGetOutputPortRealSignal(S,0); InputRealPtrsType uPtrs         = ssGetInputPortRealSignalPtrs(S,0);const real_T      *u1           = &U(0);const real_T      *u2           = &U(1);//     UNUSED_ARG(tid); /* not used in single tasking mode */mdl_Uptr->shuru1 =*u1;mdl_Uptr->shuru2 =*u2;mdl_step();    *y = mdl_Yptr->Output;
}#define MDL_UPDATE
/* Function: mdlUpdate ========================================================* Abstract:*    This function is called once for every major integration time step.*    Discrete states are typically updated here, but this function is useful*    for performing any tasks that should only take place once per integration*    step.*/
static void mdlUpdate(SimStruct *S, int_T tid)
{InputRealPtrsType uPtrs  = ssGetInputPortRealSignalPtrs(S,0);real_T            *rwork = ssGetRWork(S);int_T             i;
//     real_T           *y    = ssGetOutputPortRealSignal(S,0);//     UNUSED_ARG(tid); /* not used in single tasking mode */
//     mdl_step();
//     *y =10;
}/* Function: mdlTerminate =====================================================* Abstract:*    No termination needed, but we are required to have this routine.*/
static void mdlTerminate(SimStruct *S)
{UNUSED_ARG(S); /* unused input argument */
//     mdl_terminate();
}#if defined(MATLAB_MEX_FILE)
#define MDL_RTW
/* Function: mdlRTW ===========================================================* Abstract:*    It registers the name "InputAtLastUpdate" for the RWork vector.*/
static void mdlRTW(SimStruct *S)
{if (!ssWriteRTWWorkVect(S, "RWork", 1 /* nNames */,"InputAtLastUpdate", ssGetNumRWork(S))) {return;}/*This registration of the symbol "InputAtLastUpdate" allows sfunmem.tlc tocall LibBlockRWork(InputAtLastUpdate,[...])*/}
#endif /* MDL_RTW */#ifdef  MATLAB_MEX_FILE    /* Is this file being compiled as a MEX-file? */
#include "simulink.c"      /* MEX-file interface mechanism */
#else
#include "cg_sfun.h"       /* Code generation registration function */
#endif
/* File: rtwdemo.h  ** Abstract:*      Header file for example application program to be * used with ERT shared library target demo rtwdemo_shrlib.mdl. ** Copyright 2006-2007 The MathWorks, Inc.*/#ifndef _APP_MAIN_HEADER_
#define _APP_MAIN_HEADER_typedef struct {real_T shuru1;real_T shuru2;
} ExternalInputs_test;typedef struct {real_T Output;
} ExternalOutputs_test;#endif /*_APP_MAIN_HEADER_*//*End of file */

(笔记)Simulink生成动态链接库并使用相关推荐

  1. Makefile学习笔记06|编译动态链接库

    Makefile学习笔记06|编译动态链接库   希望看到这篇文章的朋友能在评论区留下宝贵的建议来让我们共同成长,谢谢.   这里是目录 静态链接与动态链接   链接分为两种:静态链接.动态链接. 静 ...

  2. 【linux基础】CMake如何生成动态链接库文件

    CMakeLists.txt SET(LIBRARY_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/lib) ADD_LIBRARY(filename SHARED ${CURREN ...

  3. Linux下c和cuda混合编译,并生成动态链接库.so和使用

    Linux下c和cuda混合编译,并生成动态链接库.so和使用 2016-08-27 14:27 98人阅读 评论(0) 收藏 举报 分类: Linux 版权声明:本文为博主原创文章,未经博主允许不得 ...

  4. Clion生成动态链接库.dll

    今天研究如何生成动态链接库.dll文件纠结了好久.在保证代码文件不报错的情况下,可能要注意几个方面. 一·项目结构要完整. Clion项目生成后会和其他编程工具不同的地方在于会有一个CMakelist ...

  5. Matlab生成动态链接库供C#调用

    Matlab生成动态链接库供C#调用 原文 http://www.cnblogs.com/chenyongblog/p/3156716.html 1.首先在Matlab中编写一个或几个.m文件 2.然 ...

  6. dll = MinGW gcc 生成动态链接库 dll 的一些问题汇总

    MinGW gcc 生成动态链接库 dll 的一些问题汇总 https://blog.csdn.net/liyuanbhu/article/details/42612365 网络上关于用 MinGW  ...

  7. 如何用gcc编译器生成动态链接库*.so文件(动态库)

    如何用gcc编译器生成动态链接库*.so文件(动态库) 当源文件为string.c时,生成动态链接库,假设名称为libstr.so 生成动态链接库的命令行为: gcc -fPIC -shared -o ...

  8. Matlab 2019b simulink 生成C代码(一)

    一.简介 本文主要以matlab 2019b版本,主要讲解怎么用simulink生成嵌入式c代码: 1.设置生成嵌入式C代码: 2.生成自己想要变量代码: 3.生成自定义名称函数 二.具体实现方法 1 ...

  9. gcc 生成动态链接库

    http://blog.csdn.net/ngvjai/article/details/8520840 Linux下文件的类型是不依赖于其后缀名的,但一般来讲: .o,是目标文件,相当于windows ...

最新文章

  1. WCF - 基础介绍
  2. 深入理解C代码中的注释
  3. nginx反向代理取得IP地址
  4. 数据分析、机器学习必读书,李航《统计学习方法》发布算法推导视频啦!(附作业讲解)...
  5. 前端学习(2413):关于有默认子路由的路由name
  6. Java中高效判断数组中是否包含某个元素
  7. 将Maven创建的web Servlet2.3工程改为Servlet3.0
  8. xamarin android pdf,Xamarin 中的Android资源及使用.pdf
  9. 外汇短线交易者的规则
  10. 世界首个拥有肌肉骨骼机器人问世(组图)
  11. [Wpf] . [Theme] 重构/Themes/Generic.xaml 创建一个Custom Control的典型做法
  12. 转:Dictionaryint,string怎么获取它的值的集合?急!急!急!
  13. 【Nav2中文网】一、入门
  14. 超详解三子棋(优化后)【万字教程包教包会】
  15. 如何利用Matlab制作色卡,手把手教你做一张属于自己的色卡,彩铅必备!
  16. 计算机机房空调原理,精密空调/机房专用空调双系统机组工作原理
  17. 腐蚀、膨胀、开运算、闭运算
  18. kotlin中map对value进行排序
  19. 我劝你别再做流量的奴隶了...
  20. 学习平面设计要学习哪些软件

热门文章

  1. python自媒体混剪视频_自媒体短视频运营中的混剪怎么做?我们又该如何去寻找素材呢?...
  2. 互联网广告综述之点击率系统
  3. bootstrap 选项卡_使Bootstrap选项卡与砌体一起玩
  4. 题目:1103.分糖果 II
  5. 输出一万以内最大的素数c语言,for语句计算输出10000以内最大素数怎么搞最简单??各位大神们...
  6. FPS游戏之怪物受到伤害动画
  7. Ubuntu 16.04 LTS 双网卡配置
  8. Smba文件共享快速搭建
  9. Ubuntu 立即更新网卡配置,重启网卡
  10. 如何实现div中的文字垂直水平居中