Why are functions called by "eval" not found by my compiled standalone application?

10 次查看(过去 30 天)
I have a function "databaseConnectWithEval" that is defined as follows:
function conn = databaseConnectWithEval
conn = eval('database("MySQL ODBC","username","password");');
end
If I call this function from the MATLAB Command Window, it works as expected.
However, if I compile this function into a standalone application, and then run the executable, the following error is thrown:
Undefined function 'database' for input arguments of type 'char'.
If I use the following function, without "eval", then both the function and the compiled executable work as expected.
function conn = databaseConnect
conn = database("MySQL ODBC","username","password");
end
Why is this function inside of "eval" not found by my compiled application?

采纳的回答

MathWorks Support Team
MathWorks Support Team 2024-7-26,0:00
When a standalone application is compiled, the dependency analyzer does not parse strings or char arrays in M files, including those inside of "eval".
As a workaround, you may use what we call a "pragma function", which is a pragma that forces the compiler to include the specified function as a dependency. You can find information about the pragma function at the following documentation page.
https://www.mathworks.com/help/compiler_sdk/ml_code/function.html
In this particular case, to use the pragma function simply add the following to the main file of the application.
%#function database
Another approach is to refactor your code to remove the use of "eval". In general, we recommend avoiding the use of "eval" entirely unless absolutely necessary, as it is less efficient and usually makes your code harder to debug.
  1 个评论
John D'Errico
John D'Errico about 19 hours 前
To expand on the answer, the question is why is stuff inside an eval not found, the parser simply cannot look there. For example, suppose the user created a highly complex string to generate a function name. Even worse, suppose that function name depends on a external file that is loaded at execution time. Or what if the string inside the eval generates a name that is chosen randomly from a list of functions. Or, or, or...
The point is, you can do all sorts of strange things inside an eval, things that cannot be predicted by the parse tool. And if something is possible, then somebody, somewhere, sometime will do it. Therefore, nothing inside an eval will be found as a dependency. They had to draw the line in the sand at that point, not looking inside an eval.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB Compiler 的更多信息

标签

产品


版本

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by