我正在尝试通过使用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。
我在配置属性-> C / C ++->常规->其他包含目录中添加了三个目录:


C:\ Users \ fmarsman \ Documents \ MATLAB \ DLL Test 2(所有libfoo。*文件所在的文件夹)

C:\ Program Files \ MATLAB \ MATLAB编译器运行时\ 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项目中包含源代码。

关于c++ - 使用MatLab dll时发生C++错误:找到一个或多个乘法定义的符号,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21707094/

10-10 17:39