简体   繁体   English

使用Boost.python建立世界

[英]build hello world using Boost.python

I am just learning using Boost.Python, so excuse me if it seems silly. 我只是在学习使用Boost.Python,所以如果它看起来很傻,请原谅。 I have these code from here to wrap a c++ function in python. 我从这里有了这些代码,可以将c ++函数包装在python中。
I also searched for a few hours to find a working example and most relevant question was this , but still I have problem with build and run. 我还搜索了几个小时以找到一个有效的示例,最相关的问题是this ,但是我仍然无法构建和运行。

This is zoo.cpp 这是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);
}

and visit_zoo.py 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()

and here is the makefile 这是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 $@

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

clean:
    rm -rf *.so *.o

.PHONY: default clean

error massage: 错误信息:

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

I used this makefile that worked. 我使用了这个有效的makefile。 Thanks for reading. 谢谢阅读。 I got help from here 我从这里得到帮助

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

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

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