Making human readable cpp using coder

조회 수: 1 (최근 30일)
Arwel
Arwel 2022년 3월 4일
답변: Benjamin Thompson 2022년 3월 7일
Hi,
I want to use Coder to make some C++ source code, and there are a couple of things that are important to preserve that I'm struggling to acheive.
Basically, it's essential that it keeps the variable names and so on, so that it makes sense in the context of everything else that happens around this source code for suture readers. But, Coder insists on being 'helpful' and dumping these contexts.
Here is a very basic toy example to start with......
function [out1, out2] = myFun(params, foo, bar)
% Vital we keep these variable names by way of explanation
carbon = params(1);
nitrogen = params(2);
oxygen = params(3);
hydrogen = params(4);
% A comment here will explain the calculation
myPar1 = (carbon + hydrogen) * (oxygen - foo);
myPar2 = foo * bar;
% Another comment to keep here....
row1 = [myPar1 myPar2];
row2 = [foo bar];
% Making the outputs is alse expained by variable names...
out1 = [row1 ; row2];
out2 = sin(myPar1) * nitrogen;
end
Now, this is a meaningless snippet, but the point is that it's essential that the reader is able to know that it's 'carbon' etc in order to follow what's going on, but if I run this through coder I end up with....
// myFun.cpp
//
// Code generation for function 'myFun'
//
// Include files
#include "myFun.h"
#include <cmath>
// Function Definitions
void myFun(const double params[4], double foo, double bar, double out1[2][2],
double *out2)
{
double myPar1;
// Vital we keep these variable names by way of explanation
// A comment here will explain the calculation
myPar1 = (params[0] + params[3]) * (params[2] - foo);
// Another comment to keep here....
// Making the outputs is alse expained by variable names...
out1[0][0] = myPar1;
out1[1][0] = foo * bar;
out1[0][1] = foo;
out1[1][1] = bar;
*out2 = std::sin(myPar1) * params[1];
}
// End of code generation (myFun.cpp)
... so all the context has dissapeared. Obviously for this simple example I could just hard code the thing. But in th real code this behaviour makes it impoissible to follow, and frankly next to useless.
I'm running coder like this.....
cfg = coder.config('lib');
cfg.GenerateReport = true;
cfg.ReportPotentialDifferences = false;
cfg.GenCodeOnly = true;
cfg.InlineBetweenUserFunctions = 'Readability';
cfg.InlineBetweenMathWorksFunctions = 'Readability';
cfg.InlineBetweenUserAndMathWorksFunctions = 'Readability';
cfg.TargetLang = 'c++';
cfg.PreserveArrayDimensions = true;
cfg.PreserveVariableNames = 'All';
ARGS = cell(1,1);
ARGS{1} = cell(3,1);
ARGS{1}{1} = coder.typeof(0,[4 1],[0 0]);
ARGS{1}{2} = coder.typeof(0);
ARGS{1}{3} = coder.typeof(0);
codegen -config cfg myFun -args ARGS{1}
This is a very basic example, but in the real thing losing these variable names makes things pretty much unfollowable. Preserving the white spacing would also be incredibly useful.
What am I missing? How can I get Coder to keep my variable names, and as much of the structure of the original Matlab as possible?
Cheers,
Arwel

답변 (1개)

Benjamin Thompson
Benjamin Thompson 2022년 3월 7일
Try setting cfg.BuildConfiguration to 'Debug' or 'Faster Builds'. The tool is seeing local variables that it can remove to reduce memory usage by your code.

카테고리

Help CenterFile Exchange에서 MATLAB Coder에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by