New C++ Interface Building Error: mwCppParameter2 was not declared in this scope
1 view (last 30 days)
Show older comments
Hello, I am building a C++ interface with header and source file using this live script clibpublishinterfaceworkflow.
I have a function that outputs an array:
In my hpp file:
enum VarType {
...
};
typedef double var_t;
class Variables {
...
var_t const *test_get(const VarId id, const size_t len = 0);
};
In my cpp file:
var_t const *Variables::test_get(const VarId id, const size_t len) {
const size_t idx = var_id_map[id];
return x + cfg[idx].offset;
}
%% C++ class method |test_get| for C++ class |Variables|
% C++ Signature: var_t const * Variables::test_get(VarId const id,size_t const len = 0)
test_getDefinition = addMethod(VariablesDefinition, ...
"var_t const * Variables::test_get(VarId const id,size_t const len = 0)", ...
"MATLABName", "test_get", ...
"Description", "test_get Method of C++ class Variables."); % Modify help description values as needed.
defineArgument(test_getDefinition, "id", "clib.vp.VarId");
defineArgument(test_getDefinition, "len", "uint64");
defineOutput(test_getDefinition, "RetVal", "clib.array.vp.Double", "len"); % <MLTYPE> can be "clib.array.vp.Double", or "double"
validate(test_getDefinition);
And then I got the following error
MEX configured to use 'g++' for C++ language compilation.
Deleted file 'MY_PATH_THE_PROJECT/TestInterface/output/definevp.m'.
C++ compiler set to 'g++'.
Definition file definevp.m contains definitions for 30 constructs supported by MATLAB.
- 27 constructs are fully defined.
- 3 constructs partially defined and commented out.
To include the 3 undefined constructs in the interface, uncomment and complete the definitions in definevp.m.
To build the interface, call build(definevp).
MATLAB Interface to vp Library
...
Building interface file 'vpInterface.so' for clib package 'vp'.
Error using clibgen.internal.buildHelper
Build failed with error:
'MY_PATH_THE_PROJECT/TestInterface/output/vp/vpInterface.cpp: In function ‘void {anonymous}::MFPassThrough2(void*, void (*)(void*, int, matlab::data::impl::ArrayImpl**), int, matlab::data::impl::ArrayImpl**, void (*)(void*, const char*, const char*))’:
MY_PATH_THE_PROJECT/TestInterface/output/vp/vpInterface.cpp:2622:53: error: ‘mwCppParameter2’ was not declared in this scope; did you mean ‘mwCppParameter0’?
2622 | mwCpp_DIM_mwCppParameter0.push_back(mwCppParameter2);
| ^~~~~~~~~~~~~~~
| mwCppParameter0
MY_PATH_THE_PROJECT/TestInterface/output/vp/vpInterface.cpp: At global scope:
MY_PATH_THE_PROJECT/TestInterface/output/vp/vpInterface.cpp:1562:59: warning: ‘mwCppT {anonymous}::mwCppDynamicCast(uint64_t, uint32_t) [with mwCppT = std::shared_ptr >]’ defined but not used [-Wunused-function]
1562 | std::shared_ptr> mwCppDynamicCast>>(uint64_t mwCppadr, uint32_t mwCppclsID)
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
MY_PATH_THE_PROJECT/TestInterface/output/vp/vpInterface.cpp:1550:53: warning: ‘mwCppT {anonymous}::mwCppDynamicCast(uint64_t, uint32_t) [with mwCppT = std::shared_ptr<{anonymous}::mwContainerClassForClassID >]’ defined but not used [-Wunused-function]
1550 | std::shared_ptr> mwCppDynamicCast>>(uint64_t mwCppadr, uint32_t mwCppclsID)
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
...
The hpp and cpp file are definitely having no problem because I can compile and run it directly without using MATLAB. The problem seems to be the interface generation. It appears that the auto-generated vpInterface.cpp has a variable used before its declaration. However, the build() function automatically deletes the vpInterface.cpp after each build. I tried to copy the cpp out during compilation to seek any clues on this error. Here is the capured code snippet.
void MFPassThrough2(
void* mwCppPassThroughParameterCallbackData,
void (*mwCppPassThroughParameterCallbackPlhs)(void* mwCppcallbackData, int mwCppnlhs, matlab::data::impl::ArrayImpl* mwCppplhs[]),
int mwCppPassThroughParameterNrhs,
matlab::data::impl::ArrayImpl* mwCppPassThroughParameterPrhsPImpl[],
void (*mwCppPassThroughParameterCallbackError)(void* mwCppcallbackData, const char* mwCppname, const char* mwCppmsg))
{
// Invoke: mwCppPassThroughLocalVariableObj->test_get(VarId const, unsigned long const)
try {
std::vector<matlab::data::Array> mwCppPassThroughLocalVariablePrhs = mwCppcreateprhs(mwCppPassThroughParameterNrhs, mwCppPassThroughParameterPrhsPImpl);
switch (mwCppPassThroughParameterNrhs) {
case 2: {
Variables *mwCppPassThroughLocalVariableObj = mwCppGetPointer<Variables>(mwCppPassThroughLocalVariablePrhs[0]);
VarId mwCppParameter1 = mwCppGetScalarValue<VarId>(mwCppPassThroughLocalVariablePrhs[1]);
double const *mwCppParameter0 = mwCppPassThroughLocalVariableObj->get(mwCppParameter1);
std::vector<matlab::data::Array> mwCppPassThroughLocalVariablePlhs;
matlab::data::ArrayDimensions mwCpp_DIM_mwCppParameter0;
mwCpp_DIM_mwCppParameter0.push_back(mwCppParameter2);
mwCppPassThroughLocalVariablePlhs.push_back(mwCppCreatePointerContainer(mwCppParameter0,mwCpp_DIM_mwCppParameter0));
handleOutput(mwCppPassThroughParameterCallbackData, mwCppPassThroughParameterCallbackPlhs, mwCppPassThroughLocalVariablePlhs);
break;
}
case 3: {
Variables *mwCppPassThroughLocalVariableObj = mwCppGetPointer<Variables>(mwCppPassThroughLocalVariablePrhs[0]);
VarId mwCppParameter1 = mwCppGetScalarValue<VarId>(mwCppPassThroughLocalVariablePrhs[1]);
unsigned long mwCppParameter2 = mwCppGetScalarValue<unsigned long>(mwCppPassThroughLocalVariablePrhs[2]);
double const *mwCppParameter0 = mwCppPassThroughLocalVariableObj->get(mwCppParameter1, mwCppParameter2);
std::vector<matlab::data::Array> mwCppPassThroughLocalVariablePlhs;
matlab::data::ArrayDimensions mwCpp_DIM_mwCppParameter0;
mwCpp_DIM_mwCppParameter0.push_back(mwCppParameter2);
mwCppPassThroughLocalVariablePlhs.push_back(mwCppCreatePointerContainer(mwCppParameter0,mwCpp_DIM_mwCppParameter0));
handleOutput(mwCppPassThroughParameterCallbackData, mwCppPassThroughParameterCallbackPlhs, mwCppPassThroughLocalVariablePlhs);
break;
}
default: {
// error case - mwCppnrhs was not an expected value
break;
}
}
} catch (...) {
mwCpphandleError(mwCppPassThroughParameterCallbackData, mwCppPassThroughParameterCallbackError);
}
}
One can find that, in case 2, mwCppParameter2 is not declared before using it, but I still have no idea on where the problem might be.
Environment:
Ubuntu 20.04
MATLAB 2023a
I have struggled in this problem for several days. Welcome for any suggestions on how to figure out the problem.
Thanks in advance.
0 Comments
Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!