主要内容

集成多个生成的C ++代码项目

此示例显示了如何将两个不同生成的C ++代码项目集成到一个较大的项目中。

您生成的代码项目可能具有相似的功能名称,但具有不同的设置,参数或功能。使用名称空间生成代码,以帮助集成共享相同名称的不同项目。名称空间还可以提高代码可读性。

Generate C++ Code for a MATLAB® Algorithm

Consider a simple MATLAB function that returns a gravitational constant. The value of the gravitational constant is derived from a global variable.

typegetGravityConst.m
函数C = getGravityConst%#codegen global g c = g;

假设您要生成代码getGravityConstthat models scenarios for the Moon and for the Earth. Generate two separate code projects with the same entry-point function. Specify a different global value, and hence, gravitational constant, for each project.

创建代码生成配置对象。指定:

  • DLL build type.

  • C ++目标语言。

  • The name of the orbital body as the namespace.

  • #pragma一次风格#includeguards.

  • Packaging of the generated code files into a。zip通过调用packNGofunction.

cfg = coder.config('dll'); cfg.TargetLang =“ C ++”;cfg.cppnamespace ='月亮';cfg.HeaderGuardStyle =“使用pragmaonce”;cfg.PostCodeGenCommand ='packNGo(buildInfo)';

Generate code forgetGravityConstto model the Moon:

  • 通过使用先前定义的配置对象。

  • With a code generation report.

  • 因此,代码以m/s^2的单位返回月球的重力常数值。

  • In an output folder calledprojectMoon

  • With output binaries calledgetGravityConstMoon

代码根getGravityConst-configcfg-报告- 全球{'g',-1.62}。。。-dprojectMoon-ogetGravityConstMoon
代码生成成功:要查看报告,打开('ProjectMoon/html/report.mldatx')

生成代码getGravityConst这对地球进行建模,首先修改:

  • Namespace name

  • 引力常数

  • Output file name

  • 输出文件夹名称

cfg = coder.config('dll'); cfg.TargetLang =“ C ++”;cfg.cppnamespace ='earth';cfg.HeaderGuardStyle =“使用pragmaonce”;cfg.PostCodeGenCommand ='packNGo(buildInfo)';代码根getGravityConst-configcfg-报告- 全球{'g', -9.81}。。。-dprojectEarth-ogetGravityConstEarth
Code generation successful: To view the report, open('projectEarth/html/report.mldatx')

Project Integration Scenario: Planetary Modeling

Suppose that you want to design a larger project that performs planetary modeling and computes quantities such as the flight times of falling objects. The flight time depends on the gravitational constant for each planet and the initial height of the object. You want to use the generated code functions forgetGravityConstin this larger project.

确定依赖平台的文件扩展名

生成的动态库在不同平台上具有不同的扩展。此代码确定了您平台的正确扩展。

dllext ='';libext ='';如果ismac dllext ='.dylib';libext = dllext;别的如果isunix dllext ='。所以';libext = dllext;别的如果ispc dllext ='.dll';libext ='.lib';别的disp('Platform not supported')返回end

Write a Main File That Uses the Generated Code Projects

In the general case, you integrate different projects by writing or modifying a main file to call each of the projects' functions. By using namespaces, you can distinguish the generated functions for each project, even though the function names are the same.

For an example of how to write a main file that uses the generated C++ code for both projects, see the attached filemain_planetSim.cpp。要从主文件构建可执行文件或二进制文件,您必须指定或提供以下内容,以构建构建工具(编译器,链接器和/或IDE)及其正确的路径:

  • Header files for any called functions.

  • On Windows platforms, import libraries (.libfiles).

  • Dynamic libraries (.dll,。so。dylibfiles).

  • 包括其他生成源的目录,并包括文件。

The。zipfiles that thepackNGocommand creates during code generation contain the generated code files. Unpack the zip files to folders in your build directory or build environment. You must also make your dynamic libraries accessible to the executable, for example, by moving the generated dynamic libraries to the same folder as the executable.

编写一个集成两个项目的MATLAB函数

作为手动编写主文件的替代方法,您还可以通过使用该项目将两个项目集成到第三生成的代码项目中CODER.CEVALfunction. TheCODER.CEVALfunction enables you to call external C/C++ code from generated C/C++ code.

The filePlanetsim.Mshows how to useCODER.CEVAL和associated build configuration functions to integrate the generated projects into the larger project.

planetsim.m

Generate MEX code for the行星function:

linkObjectMoon = [“ ProjectMoon/GetGravityConstmoon”libext];linkObjectearth = ['projectEarth/getGravityConstEarth'libext];cfg = coder.config('mex'); cfg.TargetLang =“ C ++”;Codegen(Codegen)('planetSim','-config',cfg,'-d','planetSim','-报告'、linkObjectMoon linkObjectEarth)
Code generation successful: To view the report, open('planetSim/html/report.mldatx')

Test the Generated MEX Function

Use the MEX function to test the generated code in the MATLAB environment. The MEX function must have access to the generated link libraries. Move the link libraries to the current directory and call the MEX function.

copyfile([“ ProjectMoon/GetGravityConstmoon”dllext]);copyfile(['projectEarth/getGravityConstEarth'dllext]);[T_M,T_E] = Planetsim_mex
T_M = 3.5136
t_e = 1.4278

The output shows the flight times for the falling object on the Moon and on the Earth.

See Also

|||||

相关话题