首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Boost.python构建hello世界

使用Boost.python构建hello世界
EN

Stack Overflow用户
提问于 2017-09-20 03:59:33
回答 1查看 1.1K关注 0票数 2

我只是在用Boost.Python学习,如果这看起来很傻的话,请原谅。我有这些来自这里的代码,可以在python中包装一个c++函数。

我还搜索了几个小时来找到一个有用的例子,最相关的问题是,但我在构建和运行方面仍然有问题。

我是zoo.cpp

代码语言:javascript
复制
#include <boost/python.hpp>
#include <string>

/*
 * This is the C++ function we write and want to expose to Python.
 */
const std::string hello() {
    return std::string("hello, zoo");
}

/*
 * This is a macro Boost.Python provides to signify a Python extension module.
 */
BOOST_PYTHON_MODULE(zoo) {
    // An established convention for using boost.python.
    using namespace boost::python;

    // Expose the function hello().
    def("hello", hello);
}

visit_zoo.py

代码语言:javascript
复制
import zoo
# In zoo.cpp we expose hello() function, and it now exists in the zoo module.
assert 'hello' in dir(zoo)
# zoo.hello is a callable.
assert callable(zoo.hello)
# Call the C++ hello() function from Python.
print zoo.hello()

下面是makefile

代码语言:javascript
复制
CC = g++
PYLIBPATH = $(shell python-config --exec-prefix)/lib
LIB = -L$(PYLIBPATH) $(shell python-config --libs) -lboost_python
OPTS = $(shell python-config --include) -O2

default: zoo.so
    @python ./visit_zoo.py

zoo.so: zoo.o
    $(CC) $(LIB) -Wl,-rpath,$(PYLIBPATH) -shared $< -o $@

zoo.o: zoo.cpp Makefile
    $(CC) $(OPTS) -c $< -o $@

clean:
    rm -rf *.so *.o

.PHONY: default clean

错误按摩:

代码语言:javascript
复制
g++ Usage: /usr/bin/python-config --prefix|--exec-prefix|--includes|--libs|--cflags|--ldflags|--extension-suffix|--help|--configdir -O2 -c zoo.cpp -o zoo.o
g++: error: Usage:: No such file or directory
g++: error: missing argument to ‘--prefix’
/bin/sh: 1: --exec-prefix: not found
/bin/sh: 1: --includes: not found
/bin/sh: 1: --libs: not found
/bin/sh: 1: --cflags: not found
/bin/sh: 1: --ldflags: not found
/bin/sh: 1: --extension-suffix: not found
/bin/sh: 1: --help: not found
/bin/sh: 1: --configdir: not found
Makefile:13: recipe for target 'zoo.o' failed
make: *** [zoo.o] Error 127
EN

回答 1

Stack Overflow用户

发布于 2017-09-20 05:16:01

我用了这个成功的makefile。感谢您的阅读。我得到了这里的帮助

代码语言:javascript
复制
PYTHON_VERSION = 2.7
PYTHON_INCLUDE = /usr/include/python$(PYTHON_VERSION)

# location of the Boost Python include files and library

BOOST_INC = /usr/include
BOOST_LIB = /usr/lib

# compile mesh classes
TARGET = zoo

$(TARGET).so: $(TARGET).o
    # g++ -shared -Wl,--export-dynamic $(TARGET).o -L$(BOOST_LIB) -lboost_python-$(PYTHON_VERSION) -L/usr/lib/python$(PYTHON_VERSION)/config -lpython$(PYTHON_VERSION) -o $(TARGET).so
    g++ -shared -Wl,--export-dynamic $(TARGET).o -L$(BOOST_LIB) -lboost_python -L/usr/lib/python$(PYTHON_VERSION)/config -lpython$(PYTHON_VERSION) -o $(TARGET).so

$(TARGET).o: $(TARGET).cpp
    g++ -I$(PYTHON_INCLUDE) -I$(BOOST_INC) -fPIC -c $(TARGET).cpp
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46313063

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档