integrate multiple generated c code projects -凯发k8网页登录
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.
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.
type getgravityconst.m
function c = getgravityconst %#codegen global g c = g;
suppose that you want to generate code for getgravityconst
that 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.
create a code generation configuration object. specify:
dll build type.
c target language.
the name of the orbital body as the namespace.
#pragma once
style#include
guards.packaging of the generated code files into a
.zip
file by calling thepackngo
function.
cfg = coder.config('dll'); cfg.targetlang = "c "; cfg.cppnamespace = 'moon'; cfg.headerguardstyle = "usepragmaonce"; cfg.postcodegencommand = 'packngo(buildinfo)';
generate code for getgravityconst
to model the moon:
by using the previously defined configuration object.
with a code generation report.
such that the code returns the moon's value of the gravitational constant in units of m/s^2.
in an output folder called
projectmoon
.with output binaries called
getgravityconstmoon
.
codegen getgravityconst -config cfg -report -globals {'g', -1.62} ... -d projectmoon -o getgravityconstmoon
code generation successful: to view the report, open('projectmoon/html/report.mldatx')
to generate code for getgravityconst
that models the earth, first modify the:
namespace name
gravitational constant
output file name
output folder name
cfg = coder.config('dll'); cfg.targetlang = "c "; cfg.cppnamespace = 'earth'; cfg.headerguardstyle = "usepragmaonce"; cfg.postcodegencommand = 'packngo(buildinfo)'; codegen getgravityconst -config cfg -report -globals {'g', -9.81} ... -d projectearth -o getgravityconstearth
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 for getgravityconst
in this larger project.
determine the platform-dependent file extensions
the generated dynamic libraries have different extensions on different platforms. this code determines the correct extensions for your platform.
dllext = ''; libext = ''; if ismac dllext = '.dylib'; libext = dllext; elseif isunix dllext = '.so'; libext = dllext; elseif ispc dllext = '.dll'; libext = '.lib'; else disp('platform not supported') return 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 file 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:
header files for any called functions.
on windows platforms, import libraries (
.lib
files).dynamic libraries (
.dll
,.so
and.dylib
files).include directories for other generated source and include files.
the .zip
files that the packngo
command 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.
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 the coder.ceval
function. the coder.ceval
function enables you to call external c/c code from generated c/c code.
the file planetsim.m
shows how to use coder.ceval
and associated build configuration functions to integrate the generated projects into the larger project.
generate mex code for the planetsim
function:
linkobjectmoon = ['projectmoon/getgravityconstmoon' libext]; linkobjectearth = ['projectearth/getgravityconstearth' libext]; cfg = coder.config('mex'); cfg.targetlang = "c "; codegen('planetsim','-config',cfg,'-d','planetsim','-report',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
| codegen
| | | |