集成多个生成的C ++代码项目
This example shows how to integrate two different generated C++ code projects into a single, larger project.
Your generated code projects might have similar function names, but have different settings, parameters, or functionality. Generate code with namespaces to aid in integrating different projects that share the same names. Namespaces can also improve code readability.
生成MATLAB®算法的C ++代码
考虑一个简单的MATLAB函数,该功能返回引力常数。重力常数的值源自全局变量。
类型getGravityConst.m
function c = getGravityConst %#codegen global g c = g;
Suppose that you want to generate code forgetGravityConcst
这为月球和地球的场景建模。生成具有相同入口函数的两个单独的代码项目。为每个项目指定不同的全球值,因此,重力常数。
Create a code generation configuration object. Specify:
DLL构建类型。
C++ target language.
轨道体的名称为命名空间。
#pragma once
style#包括
警卫。将生成的代码文件包装到一个
。压缩
file by calling thePackngo
功能。
cfg = coder.config('dll');cfg.targetlang ="C++";CFG。CppNamespace ='moon';cfg.headerguardstyle =“使用pragmaonce”;CFG.PostCodeGencommand ='Packngo(buildInfo)';
生成代码getGravityConcst
建模月亮:
通过使用前面定义的配置object.
带有代码生成报告。
Such that the code returns the Moon's value of the gravitational constant in units of m/s^2.
在一个称为的输出文件夹中
ProjectMoon
。带有输出二进制
GetGravityConstmoon
。
代码根getGravityConcst-configCFG-report-globals{'g', -1.62}...-dProjectMoon-oGetGravityConstmoon
Code generation successful: To view the report, open('projectMoon/html/report.mldatx')
To generate code forgetGravityConcst
that models the earth, first modify the:
名称名称
Gravitational constant
输出文件名
Output folder name
cfg = coder.config('dll');cfg.targetlang ="C++";CFG。CppNamespace ='地球';cfg.headerguardstyle =“使用pragmaonce”;CFG.PostCodeGencommand ='Packngo(buildInfo)';代码根getGravityConcst-configCFG-report-globals{'g',-9.81}...-dProjectEarth-oGetGravityConstearth
代码生成成功:要查看报告,打开('ProjectEarth/html/report.mldatx')
项目集成方案:行星建模
假设您想设计一个执行行星建模的较大项目,并计算诸如落下对象的飞行时间之类的数量。飞行时间取决于每个行星的重力常数和物体的初始高度。您要使用生成的代码函数getGravityConcst
在这个较大的项目中。
Determine the Platform-Dependent File Extensions
这generated dynamic libraries have different extensions on different platforms. This code determines the correct extensions for your platform.
dllext ='';libext ='';如果ismac dllext ='.dylib';libext = dllext;Elseifisunix dllext ='.so';libext = dllext;ElseifISPC dllext ='.dll';libext ='.lib';别的disp(“平台不支持”金宝app)return结尾
编写一个使用生成的代码项目的主文件
在一般情况下,您通过编写或修改主文件来调用每个项目的功能来整合不同的项目。通过使用名称空间,即使函数名称相同,您也可以区分每个项目的生成功能。
有关如何编写使用两个项目的生成C ++代码的主文件的示例,请参见附件文件main_planetsim.cpp
。To build an executable or binary from the main file, you must specify or provide the following to the build tools (compiler, linker, and/or IDE) and their correct paths:
任何称为函数的标题文件。
在Windows平台上,导入库(
。lib
文件)。动态库(
。dll
,,,,。所以
and.dylib
文件)。Include directories for other generated source and include files.
这。压缩
文件Packngo
命令在代码生成期间创建包含生成的代码文件。将zip文件解开为构建目录或构建环境中的文件夹。您还必须使您的动态库可以通过将生成的动态库移至与可执行文件的同一文件夹来访问可执行文件。
Write a MATLAB Function that Integrates the Two Projects
As an alternative to writing a main file by hand, you can also integrate two projects into a third generated code project by using thecoder.ceval
功能。这coder.ceval
功能使您可以从生成的C/C ++代码调用外部C/C ++代码。
文件Planetsim.M
显示如何使用coder.ceval
并关联的构建配置功能将生成的项目集成到较大的项目中。
生成MEX代码planetSim
功能:
linkObjectMoon = ['projectMoon/getGravityConstMoon'libext]; linkObjectEarth = ['ProjectEarth/getgravityConstearth'libext]; cfg = coder.config('Mex');cfg.targetlang ="C++";代码根(“ Planetsim”,,,,'-config',CFG,'-d',,,,“ Planetsim”,,,,'-report',linkObjectmoon,linkObjectearth)
代码生成成功:要查看报告,打开('Planetsim/html/report.mldatx')
测试生成的MEX功能
使用MEX功能在MATLAB环境中测试生成的代码。MEX功能必须可以访问生成的链接库。将链接库移至当前目录,并调用MEX函数。
复制文件(['projectMoon/getGravityConstMoon'dllext]);复制文件(['ProjectEarth/getgravityConstearth'dllext]);[T_M,T_E] = Planetsim_mex
t_m = 3.5136
T_E = 1.4278
输出显示了月球和地球上落下物体的飞行时间。
也可以看看
coder.config
|代码根
|编码器
|coder.ceval
|Packngo
|coder.CodeConfig