Why the generated C codes have "b_"?

조회 수: 8 (최근 30일)
Hao Yan
Hao Yan 2019년 6월 19일
댓글: Andrei Gherghescu 2024년 9월 12일
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일
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일
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일
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!
  댓글 수: 1
Andrei Gherghescu
Andrei Gherghescu 2024년 9월 12일
Faced same issue, tried your suggestion but it doesn't work. I am using
// MATLAB Coder version : 23.2

댓글을 달려면 로그인하십시오.

카테고리

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