简体   繁体   English

使用SWIG对C ++库进行Matlab绑定

[英]Matlab binding of a C++ library using SWIG

I am already using SWIG to port some C++ libraries in Python, and I am really impressed by the quality of the results. 我已经在使用SWIG在Python中移植一些C ++库,我对结果的质量印象深刻。 So I'd like to stick with SWIG.. 所以我想坚持使用SWIG ..

Recently somebody asked me for a Matlab port. 最近有人问我一个Matlab端口。 Such a idea was actually already in my todolist .. but each time I was googling for that I didn't find nothing enough stable and easy to maintain (also because of my low skills with Matlab and its couple of ways to call C/C++ code). 这个想法实际上已经在我的todolist中 ..但是每次我用Google搜索时都没有发现任何足够稳定且易于维护的东西(也是因为我使用Matlab的低技能及其几种调用C / C ++的方法码)。

From my understanding, there is an unmaintained and scarcely documented way to do this in the main branch, and many other attempts on such a direction that did not reach enough maturity to become part of the main branch: link . 根据我的理解,在主分支中存在一种无法维护且几乎没有记录的方法,并且在这样的方向上进行的许多其他尝试都没有达到足够的成熟度,成为主分支的一部分: 链接 I'd like to be cross-platform thus I don't think that the COM way may be the solution for me. 我想成为跨平台,因此我不认为COM方式可能是我的解决方案。

However, this time I found that there is a branch that looks quite promising: link Did somebody already test this solution? 然而,这次我发现有一个看起来非常有希望的分支: 链接有人已经测试过这个解决方案吗? In case, a simple working example that calls the ported C++ code from Matlab would be very helpful.. 在这种情况下,从Matlab调用移植的C ++代码的简单工作示例将非常有用。

EDIT 1: I have removed any reference to C++11 编辑1:我删除了对C ++ 11的任何引用

EDIT 2: The matlab branch (cited in the answers) is currently in evaluation to be merged with the swig master (see: https://github.com/jaeandersson/swig/issues/72 and https://sourceforge.net/p/swig/mailman/message/35209729/ ) 编辑2: matlab分支(在答案中引用)目前正在评估与swig master合并(参见: https//github.com/jaeandersson/swig/issues/72https://sourceforge.net/ p / swig / mailman / message / 35209729 /

Since I am part of the current effort of implementing a MATLAB module for SWIG (which I wrote about in the mail you linked), I could provide a bit of background and report on the status as November 2014. 由于我是目前为SWIG实施MATLAB模块的工作的一部分(我在你所链接的邮件中写的),我可以提供一些背景信息并报告2014年11月的状态。

There have been several efforts to implement a MATLAB module for SWIG, using a variety of different approaches. 已经做了一些努力来使用各种不同的方法为SWIG实现MATLAB模块。 As of today (November 2014), none of them have yet been successful in the sense of being incorporated into the master branch of SWIG (we hope to change that though in a not too distant future, see below). 截至今天(2014年11月),它们都没有成功融入SWIG的主要分支(我们希望在不久的将来改变它,见下文)。

Like you, I am using SWIG to port a C++ project (called CasADi ) to Python and have been following developments of MATLAB support in SWIG for a couple of years now as this would be very useful for our project. 和你一样,我使用SWIG将一个C ++项目(称为CasADi移植到Python,并且已经在SWIG中开发了几年的MATLAB支持,因为这对我们的项目非常有用。 After an effort that I was somewhat involved in failed to get past the proof-of-concept stage, I decided to give it a shot myself. 经过努力,我有点参与未能通过概念验证阶段,我决定自己试一试。 In April this year (2014), I started coding and it went much smoother than I had expected. 在今年4月(2014年),我开始编码,它比我预期的要顺畅得多。 After about 2 weeks of coding, I already had the core functionality implemented (wrapping functions, classes and so on), which I posted to the SWIG community. 经过大约两周的编码,我已经实现了核心功能(包装函数,类等),我将其发布到SWIG社区。 I was happy to see a lot of positive response from the SWIG list and even more happy to see that people like Kris (see his answer) joining the effort and making great contributions. 我很高兴看到SWIG名单上有很多积极的回应,更高兴看到人们喜欢Kris(见他的回答)加入努力并做出了巨大的贡献。

The state of the module right now is that it has most functionality implemented and has used successfully for CasADi and other projects. 现在模块的状态是它实现了大部分功能,并成功用于CasADi和其他项目。 For me, it demonstrates clearly that the chosen approach is the right one even if there is some work left to be done before the module is stable and documented. 对我来说,它清楚地表明所选择的方法是正确的,即使在模块稳定并记录之前还有一些工作要做。 I hope to be able to make another push and actually manage to bring the SWIG module to a state where it can be incorporated into SWIG's master branch. 我希望能够再次推动并实际设法将SWIG模块带入可以合并到SWIG主分支的状态。 But that depends on if/when I manage to secure funding to work on this. 但这取决于我是否/何时设法获得资金来解决这个问题。 I'm confident that it could happen quite soon however. 我相信它很快就会发生。

Finally, your question mentions C++11. 最后,您的问题提到了C ++ 11。 This is really a quite separate problem from the work on the MATLAB module. 这与MATLAB模块的工作实际上是一个完全不同的问题。 The stuff involving C++11 are mostly taken care of in SWIG in a matter which is independent of which language you're outputting to. 涉及C ++ 11的东西主要是在SWIG中处理的,这与你输出的语言无关。 So consult the C++11 support in SWIG in general. 因此,请参阅SWIG中的C ++ 11支持。

As you've found, there has been some recent activity for SWIG and MATLAB. 正如您所发现的,SWIG和MATLAB最近有一些活动。 This was started by Joel Andersson (building on previous efforts) and I've since then helped. 这是由乔尔安德森(Joel Andersson)开始的(建立在之前的努力基础上),从那时起我就一直帮忙。 We're both out-of-time, but did get it to work to a reasonable state. 我们都是不合时宜的,但确实让它工作到合理的状态。 A few people have used it already to wrap "production" C++ code. 有些人已经用它来包装“生产”C ++代码。 Sadly, we don't have an easy TODO list anywhere, but check conversations on the swig-devel list. 遗憾的是,我们在任何地方都没有简单的TODO列表,但请检查swig-devel列表中的对话。

Using this from matlab is quite easy. 在matlab中使用它非常容易。 You can find examples in swig/Doc/Examples/matlab/. 您可以在swig / Doc / Examples / matlab /中找到示例。 The test-suite has a few more of them. 测试套件中还有一些。

For building simple examples, you can just run swig to generate the C++ mex file, and then from matlab do 要构建简单的示例,您可以运行swig来生成C ++ mex文件,然后从matlab执行

mex yourModuleWrap.cxx

For more complicated stuff, you'll have to add your own library to the mex file. 对于更复杂的东西,您必须将自己的库添加到mex文件中。 On the swig-devel list, you'll also find sample files for using CMake. 在swig-devel列表中,您还可以找到使用CMake的示例文件。

HTH HTH

Kris 短剑的一种

I too have helped a bit with matlab integration with swig - though primarily as a tester. 我也帮助matlab与swig集成了一点 - 尽管主要是作为测试人员。 You can follow the instructions to interface my library (CoolProp) using SWIG following the instructions at: http://www.coolprop.dreamhosters.com:8010/sphinx/ 您可以按照以下说明按照说明使用SWIG连接我的库(CoolProp): http//www.coolprop.dreamhosters.com :8010/sphinx /

Roughly the process is this: 这个过程大致是这样的:

  1. Compile a version of swig that includes the matlab support. 编译包含matlab支持的swig版本。 There is a build script here: https://github.com/CoolProp/CoolProp/blob/master/dev/scripts/build_swig_matlab.py 这里有一个构建脚本: https//github.com/CoolProp/CoolProp/blob/master/dev/scripts/build_swig_matlab.py
  2. Using this special swig version, compile just like normal. 使用这个特殊的swig版本,就像正常一样编译。 We use CMake, so a rough snippet to make everything play nicely together is something like: 我们使用CMake,所以一个粗略的片段可以让所有东西很好地融合在一起就像是:

``` ```

if (COOLPROP_MATLAB_SWIG_MODULE)

  # Must have SWIG
  FIND_PACKAGE(SWIG REQUIRED)
  INCLUDE(${SWIG_USE_FILE})

  find_package(Matlab REQUIRED)

  IF(MATLAB_FOUND)
      message(STATUS "MATLAB Found, MATLAB MEX will be compiled.")
  ELSE(MATLAB_FOUND)
      MESSAGE("MATLAB not found...nothing will be built.")
  ENDIF(MATLAB_FOUND)

  set(I_FILE "${CMAKE_SOURCE_DIR}/src/CoolProp.i")

  list (APPEND APP_SOURCES ${CMAKE_SOURCE_DIR}/wrappers/MATLAB/Matlabdef.def) # To export mexFunction

  SET_SOURCE_FILES_PROPERTIES(${I_FILE} PROPERTIES CPLUSPLUS ON)

  SWIG_ADD_MODULE(CoolPropMATLAB_wrap matlab ${I_FILE} ${APP_SOURCES})
  SWIG_LINK_LIBRARIES(CoolPropMATLAB_wrap ${MATLAB_LIBRARIES})

  add_definitions(/DMATLAB_MEX_FILE) #define matlab macros
  add_definitions(/DMX_COMPAT_32)

  add_custom_command(TARGET CoolPropMATLAB_wrap
                     POST_BUILD
                     COMMAND 7z a "+CoolProp.7z" "+CoolProp"
                     WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}")

  if(WIN32) # 32-bit or 64-bit mex
    if (CMAKE_CL_64)
        SET_TARGET_PROPERTIES(CoolPropMATLAB_wrap PROPERTIES PREFIX "" SUFFIX .mexw64)
    else()
        SET_TARGET_PROPERTIES(CoolPropMATLAB_wrap PROPERTIES SUFFIX .mexw32)
    endif()
  else()
    if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
        if (${BITNESS} EQUAL "32")
            SET_TARGET_PROPERTIES(CoolPropMATLAB_wrap PROPERTIES PREFIX "" SUFFIX .mexmaci32 PREFIX "")
        elseif((${BITNESS} EQUAL "64"))
            SET_TARGET_PROPERTIES(CoolPropMATLAB_wrap PROPERTIES PREFIX "" SUFFIX .mexmaci64 PREFIX "")
        endif()
    else()
        if (CMAKE_SIZEOF_VOID_P MATCHES "8")
            SET_TARGET_PROPERTIES(CoolPropMATLAB_wrap PROPERTIES PREFIX "" SUFFIX .mexa64 PREFIX "")
        else()
            SET_TARGET_PROPERTIES(CoolPropMATLAB_wrap PROPERTIES PREFIX "" SUFFIX .mexglx PREFIX "")
        endif()
    endif()
  endif()
  add_dependencies (CoolPropMATLAB_wrap generate_headers)
  install (FILES ${CMAKE_CURRENT_BINARY_DIR}/+CoolProp.7z DESTINATION ${CMAKE_INSTALL_PREFIX}/MATLAB)
  install (TARGETS CoolPropMATLAB_wrap DESTINATION ${CMAKE_INSTALL_PREFIX}/MATLAB)
endif()

``` ```

  1. Load the compiled module into MATLAB 将已编译的模块加载到MATLAB中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM