legacy_code: Build errors because of compilerOptions

1 view (last 30 days)
Hello,
I want to create some trivial s-function with legacy_code.
There are 2 macros (TEST_DEF1 and TEST_DEF2), which I want to put into "compilerOptions". The 2 macros are used in the function TestFnc().
But somehow, the TestFnc() does not see the 2 macros defined in "compilerOptions" before...
MexTest.c:
#include "MexTest.h"
void TestFnc(double *p_u, double *p_y)
{
*p_y = *p_u + 1;
#ifndef TEST_DEF1
#error "undefined: TEST_DEF1"
#endif
#ifndef TEST_DEF2
#error "undefined: TEST_DEF2"
#endif
}
MexTest.h:
#ifndef MEXTEST_H_
#define MEXTEST_H_
void TestFnc(double *p_u, double *p_y);
#endif
legacy_code tool
def = legacy_code('initialize');
def.HeaderFiles = {'MexTest.h'};
def.SourceFiles = {'MexTest.c'};
def.SFunctionName = 'Test_SFunc_gen';
def.OutputFcnSpec = 'void TestFnc(double u1[1], double y1[1])';
%% Compile C-MEX-s-function
legacy_code('sfcn_cmex_generate', def);
legacy_code('generate_for_sim',def);
legacy_code('compile', def, {'-DTEST_DEF1', '-DTEST_DEF2'});
Mex Build Errors:
### Start Compiling Test_SFunc_gen
mex('-IC:\Users\xxxx\Documents\', '-c', '-outdir', 'C:\Users\xxxx\AppData\Local\Temp\tp05e00c12_2732_4dab_a0f8_f30bd05992f8', 'C:\Users\xxxx\Documents\MexTest.c')
Building with 'MinGW64 Compiler (C)'.
Error using mex
C:\Users\xxxx\Documents\MexTest.c: In function 'TestFnc':
C:\Users\xxxx\Documents\MexTest.c:8:10: error: #error "undefined: TEST_DEF1"
#error "undefined: TEST_DEF1"
C:\Users\xxxx\Documents\MexTest.c:12:10: error: #error "undefined: TEST_DEF2"
#error "undefined: TEST_DEF2"
How could I use the "-D" option correctly for my case?
(Matlab version 2021a)
Thank you very much!
Best regards
Tony

Accepted Answer

Benjamin Thompson
Benjamin Thompson on 14 Jan 2022
Edited: Benjamin Thompson on 14 Jan 2022
Not sure if you already found this answer or not, but thought I would post since your question actually helped me with the formatting of the third argument to legacy_code. Calling legacy_code with the 'sfcn_cmex_generate' and 'compile' parameters is equivalent to calling legacy code once with 'generate_for_sim'. So you did not need the second call to legacy_code at all. Just call it twice, using the 'compile' argument due to needing to pass compiler options to mex.
%% Compile C-MEX-s-function
legacy_code('sfcn_cmex_generate', def);
legacy_code('compile', def, {'-DTEST_DEF1', '-DTEST_DEF2'});

More Answers (1)

Tequila Drinks
Tequila Drinks on 1 Dec 2021
Hello,
nobody at Mathworks could (or want to) answer that trivial question !?
This is not a good experience with this "Matlab Answers"-Forum...
...

Community Treasure Hunt

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

Start Hunting!