1

我发现这段代码可以调用 Matlab 编译器,当从 Matlab 命令提示符调用该函数时它工作正常,我将此函数构建为 .Net 程序集,但每当我尝试在我的 C# 应用程序中使用它以构建一些 .m 文件时我得到一个例外,你认为我的问题在哪里?

Matlab代码:

function compileCode(mfile,dllName , dnetdir)

    %% Create directories if needed
    if (exist(dnetdir, 'dir') ~= 7)
        mkdir(dnetdir);
    end

    %% Build .NET Assembly
    eval(['mcc -N -d ''' dnetdir ''' -W ''dotnet:' dllName ',' ...
          '' dllName ',0.0,private'' -T link:lib ''' mfile '''']);
    end

C#代码:

var cmm = new compiler.MatlabCompiler();
MWCharArray x = new MWCharArray(@"C:\Users\ePezhman\Documents\MATLAB\Graph2D.m");
MWCharArray y = new MWCharArray("Graph");
MWCharArray z = new MWCharArray(@"C:\Matlab\dotnet");
cmm.compileCode(x,y,z);

例外:

... MWMCR::EvaluateFunction 错误 ... 'char' 类型的输入参数的未定义函数'mcc'。第 9 行 => compileCode.m 中的错误。

... Matlab M 代码堆栈跟踪 ... 在文件 C:\Users\ePezhman\AppData\Local\Temp\ePezhman\mcrCache8.0\compil0\compiler\compileCode.m,名称 compileCode,第 9 行。

4

1 回答 1

1

有趣的是,我假设您正在尝试编译一个可以动态编译其他函数的函数。

Unfortunately, I dont think the mcc function can be compiled/deployed itself


To be exact, the problem you are seeing is because MATLAB needs to know all functions called at compile-time, and by using eval, it wont figure it out on its own (since it wont parse inside the string). You can fix this particular issue by writing special comments for the compiler..

function myEval()
    %#function foo
    eval('...');
end

(Another alternative is using function handles).

Still even if you do that, it will fail at runtime inside the mcc function saying that: "License checkout failed, [...] Cannot find a valid license for Compiler".

The reason is as mentioned in the comments, mcc is a development tool and cannot be deployed to standalone programs which only depends on the free MCR runtime.

Think about it, if it was possible, it would defeat the whole purpose of buying licenses for the product, as you could create a standalone program that can compiler other codes without having the Compiler toolbox :)

于 2013-03-30T12:42:35.477 回答