フィルターのクリア

using #ifdef in c++ generated mexcode

7 ビュー (過去 30 日間)
Ebaneo Enrique
Ebaneo Enrique 2023 年 11 月 22 日
編集済み: Ebaneo Enrique 2023 年 12 月 1 日
Hello,
I am calling mexFunction.cpp from matlab
coder.ceval('mexMiFunction', .....);
the mex function in c++ originally looks like this,
void mexMiFunction(int nlhs, mxArray* plhs[],
int nrhs, const mxArray* prhs[])
{
.....
}
but I want to be able to run 2 different codes , depending on if we are generating code using CodeGenerator or not. It would look like this
#ifdef CODER //takes this path if we are using code generator
int miCustomFunction(...)
{
.... different implementation
}
#else
void mexMiFunction(int nlhs, mxArray* plhs[],
int nrhs, const mxArray* prhs[])
{
.....
}
#endif
How can I do it? thanks a lot.

採用された回答

Varun
Varun 2023 年 11 月 27 日
Hi Ebaneo,
I understand that you want to conditionally compile different parts of your code depending on whether you are using the Code Generator or not, you can use preprocessor directives in your C++ code.
In your case, you've already provided an example with the "#ifdef" and "#else" directives. However, you need to define the "CODER" macro when generating code using the Code Generator.
In your MATLAB code, when you call the "coder.ceval" function, you need to ensure that the "CODER" macro is defined. You can do this by using
coder.updateBuildInfo('addDefines', '-CODER');
For example, in MATLAB, before calling coder.ceval, you can set the compiler options like this:
coder.updateBuildInfo('addDefines', '-CODER');
coder.ceval('mexMiFunction', ...);
This tells the compiler to define the "CODER" macro during compilation, so the code inside the "#ifdef CODER" block will be included.
Refer the following documentation to learn more about using “coder.updateBuildInfo” function:
Hope this helps.
  1 件のコメント
Ebaneo Enrique
Ebaneo Enrique 2023 年 12 月 1 日
編集済み: Ebaneo Enrique 2023 年 12 月 1 日
thank you very much for your help!
the only thing I would note is that
coder.updateBuildInfo('addDefines', '-CODER');
gives me an error "macro names must be identifiers"
on the oder hand, if I use
coder.updateBuildInfo('addDefines', 'CODER');
it works, thanks

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMATLAB Coder についてさらに検索

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by