legacy_code: Build errors because of compilerOptions

2 次查看(过去 30 天)
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

采纳的回答

Benjamin Thompson
Benjamin Thompson 2022-1-14
编辑:Benjamin Thompson 2022-1-14
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'});

更多回答(1 个)

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

类别

Help CenterFile Exchange 中查找有关 Write C Functions Callable from MATLAB (MEX Files) 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by