前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >matlab转成C++

matlab转成C++

作者头像
努力努力再努力F
发布2020-12-01 10:46:21
7500
发布2020-12-01 10:46:21
举报
文章被收录于专栏:fangyangcoderfangyangcoder

笔者在尝试将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:

代码语言:javascript
复制
>> mex -setup
>> mbuild -setup

Now given the following simple function:

MyAdd.m

代码语言:javascript
复制
function c = MyAdd(a,b)
    c = a + b;
end

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

代码语言:javascript
复制
>> 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:

代码语言:javascript
复制
libMyAdd.h
libMyAdd.dll
libMyAdd.lib

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

MyAdd_test.cpp

代码语言:javascript
复制
#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:

代码语言:javascript
复制
>> 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:

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.

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-11-27 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • MyAdd.m
  • MyAdd_test.cpp
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档