在Mac OSX上从Python脚本调用c ++函数 [英] Calling c++ function, from Python script, on a Mac OSX

查看:557
本文介绍了在Mac OSX上从Python脚本调用c ++函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个c ++代码在我的mac使用非标准lybraries(在我的情况下,OpenCV libs),需要编译这个,所以它可以从其他计算机(至少从其他mac计算机)调用。从python运行。所以我有3个基本的问题:

I've a c++ code on my mac that uses non-standard lybraries (in my case, OpenCV libs) and need to compile this so it can be called from other computers (at least from other mac computers). Runned from python. So I've 3 fundamental questions:


  1. 如何编译我的项目,所以它可以从python使用?我已经读过
    ,我应该创建一个* .so文件,但是如何做呢?

  2. 如果它像一个lib一样工作,所以python调用一些特定的函数
    在python级别中选择?

  3. 还是应该包含从
    命令行执行的 main 函数?

  1. How to compile my project so it can be used from python? I've read that I should create a *.so file but how to do so?
  2. Should it work like a lib, so python calls some specific functions, chosen in python level?
  3. Or should it contain a main function that is executed from command line?

有关如何做的任何想法? PS:我使用eclipse IDE编译我的c ++项目。

Any ideas on how to do so? PS: I'm using the eclipse IDE to compile my c++ project.

干杯,

推荐答案

如果你使用linux,atmaere和nouney的答案可能适合你。

If you use linux, the answers of atmaere and nouney may work for you.

在我的例子中,当我使用MacOSX时,我想通过使用cython库。下面介绍了使事情发挥作用的简单教程:

In my case, as I use MacOSX, I figured my way through using the cython library. A straight forward tutorial to make things work is described bellow:

如何从Python脚本调用C ++:


  1. INSTALL CYTHON:要安装Cython,将python脚本与c / c ++集成的工具,请访问: https://pypi.python.org/pypi/Cython/

下载A示例:从这里下载一个C ++实现示例: http:/ /wiki.cython.org/WrappingCPlusPlus?action=AttachFile&do=get&target=cythoncpp.tgz

DOWNLOAD A EXAMPLE: Download a C++ implementation example from here: http://wiki.cython.org/WrappingCPlusPlus?action=AttachFile&do=get&target=cythoncpp.tgz

PREPARE C ++源文件:be确保您的* .cpp和* .h文件是正确的。在下载的示例中,文件名为cpp_rect.h和cpp_rect.cpp

PREPARE C++ SOURCE FILES: be sure that your *.cpp and *.h files are correct. In the downloaded example, the filenames are cpp_rect.h and cpp_rect.cpp

CREATE PYTHON WRAPER CPLASS:根据下载示例中的rectangle.pyx文件,到 http:// docs.cython.org/src/userguide/wrapping_CPlusPlus.html?highlight=cpp#create-cython-wrapper-class

CREATE PYTHON WRAPER CPLASS: according to the rectangle.pyx file in the downloaded example or to the model described in http://docs.cython.org/src/userguide/wrapping_CPlusPlus.html?highlight=cpp#create-cython-wrapper-class

准备设置.PY FILE:下载的示例已经计入此文件,但更多的信息可以在本教程中找到: http://docs.cython.org/src/userguide/wrapping_CPlusPlus.html?highlight=cpp#specify-c-language-in-setuppy 。 [PS:1最简单的编译方法是使用disutilis包,使用setup.py文件。有关编辑选项的详细信息,请访问 http://docs.cython.org/src/ reference / compilation.html#

PREPARE THE SETUP.PY FILE: the downloaded example already counts with this file but more information can be found in this tutorial: http://docs.cython.org/src/userguide/wrapping_CPlusPlus.html?highlight=cpp#specify-c-language-in-setup-py. [PS: 1 The easiest way to compile is with the disutilis package, using a setup.py file. For mor info about compilation options, visit http://docs.cython.org/src/reference/compilation.html#.

TRY TO COMPILE:转到终端,与源文件所在的文件夹相同。在这一点上,你已经ghave 4个文件:cpp_rect.h,cpp_rect.cpp,rectangle.pyx和setup.py。从那里,执行命令 $ python setup.py build_ext --inplace

TRY TO COMPILE: Go to the terminal, from the same folder where your source files are. At this point you already ghave 4 files: cpp_rect.h, cpp_rect.cpp, rectangle.pyx and setup.py. From there, execute the command $ python setup.py build_ext --inplace

g ++错误,例如它发生在我:无法执行gcc-4.2:没有这样的文件或目录u可以攻击标志系统做 sudo ln -s / usr / bin / gcc / usr / bin / gcc-4.2

If you receive a g++ error, such as it happened to me: "unable to execute gcc-4.2: No such file or directory" u can hack the flag system doing sudo ln -s /usr/bin/gcc /usr/bin/gcc-4.2. Than repeat the previous step.

测试你的代码:在这个小时,你有你需要的一切:setup.py与rectangle.pyx一起使用生成rectangle.so文件,这是我们需要的!只是构建一个pythons脚本来测试你的集成。幸运的是,该示例已经计入一个名为use_rect.py的测试脚本。

TEST YOUR CODE: At this momment, you have everything you need: the setup.py was used along with the rectangle.pyx to generate the rectangle.so file, which is WHAT we need! Just build a pythons script to test your integration. Luckly, the example already counts with a test script, named use_rect.py.

现在只需使用 $ python userect.py

这篇关于在Mac OSX上从Python脚本调用c ++函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆