2014-02-11 99 views
0

我想通过使用MCR(MatLab编译器运行时)在C++中使用MatLab函数。 但是,当我从C++调用函数时出现错误。使用MatLab dll时发生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 &)" ([email protected]@[email protected]@[email protected]@Z) already defined in libfoo.obj 

    1>libfoo.lib(libfoo.dll) : error LNK2005: "void __cdecl foo(int,class mwArray &,class mwArray const &)" ([email protected]@[email protected]@[email protected]@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.liblibfoo.hlibfoo的。 dll and libfoo.cpp

  • 接下来,我在MS Visual Studio 2008中创建了一个项目。我将libfoo.cpp添加到'Source Files'和libfoo.ht o'头文件'。 我增加了三个目录配置属性 - > C/C++ - >常规 - >附加包含目录:

C:\用户\ fmarsman \文档\ MATLAB \ DLL测试2(该文件夹中的所有。libfoo的*文件)

C:\ Program Files文件\ MATLAB \ MATLAB编译器运行时\ V82 \ EXTERN \ LIB \ Win64的\微软(用于mclmcrrt.lib)

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

  • 要链接器 - >输入 - >附加依赖我说:

“C:\ Program Files文件\ MATLAB \ MATLAB编译器运行时\ V82 \ EXTERN \ LIB \ Win64的\微软\ mclmcrrt。 LIB” “C:\用户\ fmarsman \文档\ 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 

我我一直试图找到一个解决方案整天,但没有成功。 我真的希望有人能帮助我。

回答

1

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

+0

工作。非常感谢! – MacHans

相关问题