0

I am trying to use a MatLab function in C++ by using the MCR (MatLab Compiler Runtime). However, I get an error when I call the function from C++.

This is the output when I try to build:

  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 ==========

This is what I have done:

  • I created the .m file foo.m:

    function y = foo(x)

    y = x + 1;

  • In command prompt, I executed:

    mcc –W cpplib:libfoo –T link:lib foo

  • This created libfoo.lib, libfoo.h, libfoo.dll and libfoo.cpp

  • Next, I created a project in MS Visual Studio 2008. I added libfoo.cpp to 'Source Files' and libfoo.h to 'Header Files'. I added three directories to Configuration Properties -> C/C++ -> General -> Additional Include Directories:

C:\Users\fmarsman\Documents\MATLAB\DLL Test 2 (the folder where all the libfoo.* files are)

C:\Program Files\MATLAB\MATLAB Compiler Runtime\v82\extern\lib\win64\microsoft (for the mclmcrrt.lib)

C:\MATLAB\R2013b\extern\include (for mclmcrrt.h)

  • To Linker -> Input -> Additional Dependencies I added:

"C:\Program Files\MATLAB\MATLAB Compiler Runtime\v82\extern\lib\win64\microsoft\mclmcrrt.lib" "C:\Users\fmarsman\Documents\MATLAB\DLL Test 2\libfoo.lib"

My source code:

  #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

I've been trying to find a solution all day but without success. I really hope someone can help me with this.

4

1 回答 1

1

从 VS 工作室项目中删除文件 libfoo.cpp。您已经导入了 libfoo.dll,因此您使用了 libfoo.dll 中的函数“foo”,因此您不需要将源代码包含到您的 VS 项目中。

于 2014-02-11T16:38:27.993 回答