Why the generated C codes have "b_"?

26 次查看(过去 30 天)
Hao Yan
Hao Yan 2019-6-19
回答: Arjun 2024-8-8,9:27
Hello everyone,
Just new to the forumn, please forgive me if i raised up the question in an improper way.
Recently, I am using MATLAB coder to generate C codes. But in the generated codes, I realilzed that some variables have b_ prefix.
For example.
start_point = 1; % this is start_point
Then, the generated codes would look like:
/* this is start_point */
int b_start_point = 1;
I am not pretty sure why the varialbe "start_point " has "b_" in front of the variable. Since it is the first time that it is defined, and also, there is no "start_point" in the C code, which means that it is not already exist.
Anyone can give me some clue about why it happens?
Thanks a lot.
  3 个评论
Vibhav
Vibhav 2024-7-30,15:21
Same thing happening with me, I think this has something to do with whether the maximum stack usage is surpassed or not. I noticed that when my code uses less stack space, the function is named without a b_ in front of the actual function name. Haven't found any documentation regarding this naming, so not sure what's happening here.
Umar
Umar 2024-7-30,20:46
Hi @ Hao Yan,
The "b_" prefix in variable names is added to prevent naming conflicts with existing variables in the generated C code. This prefix ensures that the generated code remains free from naming collisions, especially when interfacing with other code modules or libraries. By prefixing variables with "b_", MATLAB Coder helps maintain code integrity and avoids unintended variable shadowing or redefinition issues. Please let me know if you have any further questions.

请先登录,再进行评论。

回答(1 个)

Arjun
Arjun 2024-8-8,9:27
Hi,
As per my understanding, when you generate code using MATLAB Coder then some of your variables have a “b_” prefix. I tried to do the same on my computer and went through the documentation, my analysis is that while converting code from MATLAB to C/C++ there are many new variables introduced during the process and hence there can be naming conflicts. Some of the probable reasons can be follows:
  • Naming Conflicts: If the variable name conflicts with any other variable, functions, reserved words.
  • Internal Variables: MATLAB Coder might use prefixes like b_ to distinguish between different types of internal variables or to maintain a consistent naming convention throughout the generated code. It may be used to prevent variable shadowing as well.
I could not find any clear reasoning even after going through the documentation, but some findings are that:
  • By default, when possible, variables share names and memory in the generated code.
  • The code generator reuses your variable names for other variables or reuses other variable names for your variables.
  • The code generator preserves your function name and comments. When possible, the code generator preserves your variable names.
I found something which can help in preserving variable name and you must give it a try:
  • In a code generation configuration object, set the PreserveVariableNames parameter to 'UserNames'.
  • In the MATLAB® Coder™ app, set Preserve variable names to User names.
I hope this will help!

类别

Help CenterFile Exchange 中查找有关 MATLAB Coder 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by