0
我只是在學習使用Boost.Python,所以如果它看起來很愚蠢,不好意思。 我從here這些代碼包裝在python中的C++函數。
我也搜索了幾個小時找到一個工作的例子,最相關的問題是this,但我仍然有構建和運行的問題。使用Boost.python建立你好世界
這是zoo.cpp
#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
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文件
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 [email protected]
zoo.o: zoo.cpp Makefile
$(CC) $(OPTS) -c $< -o [email protected]
clean:
rm -rf *.so *.o
.PHONY: default clean
錯誤按摩:
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
這有什麼好做Boost.Python的,如使用線顯示,'蟒蛇,配置在Makefile --include'應該是'蟒蛇,配置--includes' – Praetorian