zoukankan      html  css  js  c++  java
  • matlab转成C++

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

      $matlabrootexterninclude
      
    • 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:

      $matlabrootexternlibwin32microsoft
      

      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	ooutputfolder
      

    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.

  • 相关阅读:
    Retrofit/Okhttp API接口加固技术实践(上)
    浅析C#中的结构体和类
    iOS中 支付宝钱包具体解释/第三方支付 韩俊强的博客
    Java并发之volatile二
    dynamic initializer和全局变量
    二叉树转换成森林&amp;森林变成二叉树
    这才是真正的裸眼3D!超级震撼!!
    每一个开发人员都应该有一款自己的App
    Hibernate HQL的使用
    我的Android进阶之旅------&gt;Android 关于arm64-v8a、armeabi-v7a、armeabi、x86下的so文件兼容问题
  • 原文地址:https://www.cnblogs.com/fydeblog/p/14049325.html
Copyright © 2011-2022 走看看