Azzera filtri
Azzera filtri

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

10 visualizzazioni (ultimi 30 giorni)
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?

Risposta accettata

MathWorks Support Team
MathWorks Support Team il 26 Lug 2024 alle 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 Commento
John D'Errico
John D'Errico circa 19 ore fa
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.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su MATLAB Compiler in Help Center e File Exchange

Prodotti


Release

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by