Python学习:mac下使用boost.bython扩充python
2015-08-23 01:36
585 查看
1、第一步,先要准备boost库,编译库,需要编译python库,我使用python的环境是python2.7。我使用的boost 1.5.7版本。存放在目录为:/Volumes/data/code/c++/boost_1_57_0。boost的编译,网上有一大把。这里就不详细说了。
说明:如果你的mac机器 /usr/include头文件很少,你可以使用下面这个命令安装。
[/code]
2、编写一个hello_world.cpp,代码如下:
[/code]
3、编译,这个时候生成hello_world.so文件。
[/code]
4、把/Volumes/data/code/c++/boost_1_57_0/stage/lib/libboostpython.dylib拷贝到hello_world.so同一个目录下。测试新开发的模块。如下:
[/code]
结束语:mac/linux使用boost.python库扩展python看上去比较简单,但由于对gcc编译命令不熟悉,很多不知道,因此摸索了很久。写这里给有需要的同学使用。
说明:如果你的mac机器 /usr/include头文件很少,你可以使用下面这个命令安装。
xcode-select --install
[/code]
2、编写一个hello_world.cpp,代码如下:
#include <iostream>
#include <string>
#include <boost/python.hpp>
using namespace std;
using namespace boost::python;
void say()
{
cout<<"Hello World!"<<endl;
}
BOOST_PYTHON_MODULE(hello_world)
{
def("say", say);
}
[/code]
3、编译,这个时候生成hello_world.so文件。
g++ -fpic -c -L/Volumes/data/code/c++/boost_1_57_0/ -I/usr/include/python2.7 hello_world.cpp
g++ -shared -L/usr/lib -L/Volumes/data/code/c++/boost_1_57_0/stage/lib -lpython2.7 -lboost_python -o hello_world.so hello_world.o
[/code]
4、把/Volumes/data/code/c++/boost_1_57_0/stage/lib/libboostpython.dylib拷贝到hello_world.so同一个目录下。测试新开发的模块。如下:
fish:c++ fish$ python
Python 2.7.10 (default, Jul 14 2015, 19:46:27)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>import hello_world
>>>hello_world.say()
Hello World!
>>>
>>>quit()
[/code]
结束语:mac/linux使用boost.python库扩展python看上去比较简单,但由于对gcc编译命令不熟悉,很多不知道,因此摸索了很久。写这里给有需要的同学使用。
相关文章推荐
- 如何使用七牛Python SDK写一个同步脚本及使用教程
- Python图示入门
- 在win64下安装elipse+pythonDev
- 自制 Python3爬虫
- python学习第三天
- Python环境搭建
- 第0012道练习题_Python文本查找替换
- scrapy学习--python字典回顾
- NumPy常用【数值计算】函数总结(1):生成数组、读取数组、数组操作
- python函数(模拟算数游戏)
- 【Python】小技巧
- 完整的图片去噪代码(python)
- Python 中怎么写 swap()交换函数
- python学习 3
- Wiki——Windows下python2.7 爬虫框架Scrapy环境配置
- [Python] Python学习笔记之常用模块总结[持续更新...]
- RSA密钥证书生成(Python&C#)
- Python爬虫学习记录(1)——百度贴吧图片下载
- Python - urllib2 模块
- python001_环境搭建helloWorld