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.

  • 相关阅读:
    VS Code C++ 代码格式化方法(clang-format)
    linux函数深入探索——open函数打开文件是否将文件内容加载到内存空间
    总结一下数据库的 一对多、多对一、一对一、多对多 关系
    netstat -st输出解析(二)
    Connection reset by peer的常见原因及解决办法
    Linux errno详解
    Linux之清理linux内存cache
    十大经典排序算法(动图演示)
    android studio导出apk
    多线程还是多进程的选择及区别
  • 原文地址:https://www.cnblogs.com/fydeblog/p/14049325.html
Copyright © 2011-2022 走看看