3

I'm generating code for a function with several variable-sized arrays. The code needs to be compiled in both linux (with gcc) and windows machines (with MinGW).

To "ensure capacity" in those variables, Matlab creates several counter variables, named i349, i350, ..., i386, ... i400. Apparently, the windows compiler doesn't like this. It throws two errors that say :

line x: error: expected identifier or '(' before numeric constant

line y: error: lvalue required as left operand of assignment

line y+1: error: lvalue required as increment operand

These are all solved by manually picking other variable names. While I could use some script to automatically do so, I'd rather avoid it.

Two questions:

  1. Why does these variable names cause a problem with the compiler?

  2. How can I tell Matlab not to generate variables with these names?

Mefitico
  • 816
  • 1
  • 12
  • 41
  • I'm guessing that token is reserved by the MinGW compiler, probably relating to [the architecture](https://en.wikipedia.org/wiki/Intel_80386) of the same name. – Reticulated Spline Feb 13 '20 at 16:08

2 Answers2

4

Why does these variable names cause a problem with the compiler?

These are predefined macros by the compiler. It's nonstandard behavior, but this is used for backwards compatibility in determining what system the code is being compiled for.

How can I tell Matlab not to generate variables with these names?

You don't want to. Instead, pass a -std=c... option to the compiler to have it act in standard-compliant mode.

S.S. Anne
  • 15,171
  • 8
  • 38
  • 76
1

If you get a name collision that isn't fixable with compiler flags you can use the ReservedNameArray config setting with MATLAB Coder:

cfg = coder.config('lib');
cfg.ReservedNameArray = 'Name1;Name2';

codegen foo.m -config cfg

That will cause Coder to mangle identifiers to not collide with the provided names.

Ryan Livingston
  • 1,898
  • 12
  • 18