• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

matlab转成C++

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

笔者在尝试将matlab转成C++时,出现error LNK2019: 无法解析的外部符号, 根据下面StackOverflow的回答解决了问题,截取分享.

source link: https://stackoverflow.com/questions/17120635/calling-matlab-from-c-errors-unresolved-external-symbol

Before you start, make sure you have a supported compiler installed. That would be Visual C++ and possibly Windows SDK if you are using VS Express edition on a 64-bit Windows. Next you need to configure MATLAB by running these steps at least once:

>> mex -setup
>> mbuild -setup

Now given the following simple function:

MyAdd.m

function c = MyAdd(a,b)
    c = a + b;
end

We want to build a C++ shared library using the MATLAB Compiler mcc:

>> mcc -N -W cpplib:libMyAdd -T link:lib MyAdd.m -v

This will produce a couple of files including a header file, a DLL, and an import library:

libMyAdd.h
libMyAdd.dll
libMyAdd.lib

Next we create a C++ program to test the above library:

MyAdd_test.cpp

#include "libMyAdd.h"

int main()
{
    // initialize MCR and lib
    if (!mclInitializeApplication(NULL,0))  {
        std::cerr << "could not initialize the application" << std::endl;
        return -1;
    }
    if(!libMyAddInitialize()) {
        std::cerr << "Could not initialize the library" << std::endl;
        return -1;
    }

    try {
        // create input
        double a[] = {1.0, 2.0, 3.0, 4.0};
        double b[] = {5.0, 6.0, 7.0, 8.0};
        mwArray in1(2, 2, mxDOUBLE_CLASS, mxREAL);
        mwArray in2(2, 2, mxDOUBLE_CLASS, mxREAL);
        in1.SetData(a, 4);
        in2.SetData(b, 4);

        // call function
        mwArray out;
        MyAdd(1, out, in1, in2);

        // show result
        std::cout << "in1 + in2 = " << std::endl;
        std::cout << out << std::endl;

        double c[4];
        out.GetData(c, 4);
        for(int i=0; i<4; i++) {
            std::cout << c[i] << " " << std::endl;
        }

    } catch (const mwException& e) {
        std::cerr << e.what() << std::endl;
        return -2;
    } catch (...) {
        std::cerr << "Unexpected error thrown" << std::endl;
        return -3;
    } 

    // cleanup
    libMyAddTerminate();   
    mclTerminateApplication();

    return 0;
}

We could compile this program right from inside MATLAB using:

>> mbuild MyAdd_test.cpp libMyAdd.lib -v
>> !MyAdd_test

We could also compile it ourselves using Visual Studio. We start by creating a native console application, then set the project settings as follows:

  • From the menu, select "Project > Properties" and apply the settings to "All configurations" (both debug and release targets)

  • Under C/C++ properties, set the "Additional Include Directories" by adding both the directory containing the generated header file libMyAdd.h, in addition to the directory containing MATLAB\'s header files:

    $matlabroot\extern\include
    
  • Similarly under "Linker", set the "Additional Library Directories". That would be the same directory as before containing libMyAdd.lib, as well as in my case:

    $matlabroot\extern\lib\win32\microsoft
    

    Then go to "Linker > Input" and add the following inside "Additional Dependencies":

    libMyAdd.lib
    mclmcrrt.lib
    
  • Finally under "Debugging > Environment", you might want to extend the PATH environment variable to include the directory containing the generated libMyAdd.dll file. That way you can directly hit F5 to compile run the program directly from inside VS. This will be something like:

    PATH=%PATH%;C:\path\to\output\folder
    

If you do this kind of thing often, you could create a property sheet once, which could then be reused in other VC++ projects. See this answer for an example.


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
模糊C均值聚类代码参考(Matlab) - leivo发布时间:2022-07-18
下一篇:
matlab中如何定义函数发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap