使用MatLab dll时出现C++错误:找到一个或多个多重定义的符号

C++ error when using MatLab dll: one or more multiply defined symbols found

本文关键字:符号 一个 定义 dll MatLab C++ 错误 使用      更新时间:2023-10-16

我正试图通过使用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:UsersfmarsmanDocumentsVisual Studio 2008ProjectsProject1MatLab DLL Test 2x64DebugMatLab DLL Test 2.exe : fatal error LNK1169: one or more multiply defined symbols found
1>Build log was saved at "file://c:UsersfmarsmanDocumentsVisual Studio 2008ProjectsProject1MatLab DLL Test 2MatLab DLL Test 2x64DebugBuildLog.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.liblibfoo.hlibfoo.cpp

  • 接下来,我在MSVisualStudio2008中创建了一个项目。我将libfoo.cpp添加到"源文件",将libfoo.h添加到"头文件"。我在"配置属性"->"C/C++"->"常规"->"其他包含目录"中添加了三个目录:

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编译器运行时\v82\extern\lib\win64\microsoft\mclmcrrt.lib"C:\Users\fmarsman\Documents\MATLAB\DLL Test 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

我一整天都在努力寻找解决方案,但没有成功。我真的希望有人能帮我。

从VS studio项目中删除文件libfoo.cpp。您已经导入了libfoo.dll,所以您使用的是libfoo.dll中的函数"foo",所以您不需要将源代码包括在VS项目中。

相关文章: