【发布时间】:2014-02-11 16:13:06
【问题描述】:
我正在尝试通过使用 MCR(MatLab 编译器运行时)在 C++ 中使用 MatLab 函数。 但是,当我从 C++ 调用该函数时出现错误。
这是我尝试构建时的输出:
1>------ Build started: Project: MatLab DLL Test 2, Configuration: Debug x64 ------
1>Compiling...
1>main.cpp
1>libfoo.cpp
1>Generating Code...
1>Linking...
1>libfoo.lib(libfoo.dll) : error LNK2005: "void __cdecl foo(int,class mwArray &,class mwArray const &)" (?foo@@YAXHAEAVmwArray@@AEBV1@@Z) already defined in libfoo.obj
1>libfoo.lib(libfoo.dll) : error LNK2005: "void __cdecl foo(int,class mwArray &,class mwArray const &)" (?foo@@YAXHAEAVmwArray@@AEBV1@@Z) already defined in libfoo.obj
1>C:\Users\fmarsman\Documents\Visual Studio 2008\Projects\Project1\MatLab DLL Test 2\x64\Debug\MatLab DLL Test 2.exe : fatal error LNK1169: one or more multiply defined symbols found
1>Build log was saved at "file://c:\Users\fmarsman\Documents\Visual Studio 2008\Projects\Project1\MatLab DLL Test 2\MatLab DLL Test 2\x64\Debug\BuildLog.htm"
1>MatLab DLL Test 2 - 3 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
这就是我所做的:
-
我创建了 .m 文件 foo.m:
函数 y = foo(x)
y = x + 1;
-
在命令提示符下,我执行了:
mcc –W cpplib:libfoo –T 链接:lib foo
这创建了 libfoo.lib、libfoo.h、libfoo.dll 和 libfoo.cpp >
- 接下来,我在 MS Visual Studio 2008 中创建了一个项目。我将 libfoo.cpp 添加到“源文件”,并将 libfoo.h 添加到“头文件”。 我在 Configuration Properties -> C/C++ -> General -> Additional Include Directories 中添加了三个目录:
C:\Users\fmarsman\Documents\MATLAB\DLL Test 2(所有 libfoo.* 文件所在的文件夹)
C:\Program Files\MATLAB\MATLAB Compiler Runtime\v82\extern\lib\win64\microsoft(用于 mclmcrrt.lib)
C:\MATLAB\R2013b\extern\include(对于 mclmcrrt.h)
- 到链接器 -> 输入 -> 我添加的其他依赖项:
"C:\Program Files\MATLAB\MATLAB Compiler Runtime\v82\extern\lib\win64\microsoft\mclmcrrt.lib" "C:\Users\fmarsman\Documents\MATLAB\DLL 测试 2\libfoo.lib"
我的源代码:
#include <iostream>
#include <mclmcrrt.h>
#include <mclcppclass.h>
#include <libfoo.h>
using namespace std;
int main( ) {
mclInitializeApplication(NULL,0);
libfooInitialize( );
mwArray y(1, 1, mxDOUBLE_CLASS);
y = 3.0;
const mwArray x = y.Clone();
foo(1,y,x);
mclTerminateApplication( );
libfooTerminate( );
return 0;
} // main
我整天都在努力寻找解决方案,但没有成功。 我真的希望有人可以帮助我。
【问题讨论】:
标签: c++ matlab dll matlab-compiler matlab-coder