所以我有一个GUI程序,有很多“东西”在继续。我正在添加一个python脚本界面,以便有人可以与此环境进行交互。我使用boost python。所以我的第一件事就是我想创建一个新的模块。为简单起见,现在我只是模块是世界你好...嵌入在C++中的Python
#include <boost/python.hpp>
char const* greet() {
return "hello, world" ;
}
BOOST_PYTHON_MODULE(cerrnimapi) {
boost::python::def("greet", greet) ;
}
在我的系统我有一个类,看起来像这样...
Controller::Controller() {
Py_Initialize() ;
main_module = boost::python::import("__main__") ;
main_namespace = main_module.attr("__dict__") ;
}
void Controller::execute_script(std::string filename) {
try {
boost::python::api::object ignored =
boost::python::exec_file(filename.c_str(), main_namespace) ;
} catch(boost::python::error_already_set const &) {
if (PyErr_ExceptionMatches(PyExc_ZeroDivisionError)) {
} else {
PyErr_Print();
}
}
}
现在,当我去执行脚本我得到一个错误的图形用户界面...
Traceback (most recent call last):
File "/home/mokon/repository/trunk/python.py", line 1, in <module>
import cerrnimapi
ImportError: No module named cerrnimapi
所以当然,我构建了一些错误。我的构建系统使用自动工具使这里有涉及到这一点,构建系统的几件...
在configure.ac:
AM_PATH_PYTHON
AC_ARG_VAR([PYTHON_INCLUDE], [Include flags for python, bypassing python-config])
AC_ARG_VAR([PYTHON_CONFIG], [Path to python-config])
AS_IF([test -z "$PYTHON_INCLUDE"], [
AS_IF([test -z "$PYTHON_CONFIG"], [
AC_PATH_PROGS([PYTHON_CONFIG],
[python$PYTHON_VERSION-config python-config],
[no],
[`dirname $PYTHON`])
AS_IF([test "$PYTHON_CONFIG" = no], [AC_MSG_ERROR([cannot find python-config for $PYTHON.])])
])
AC_MSG_CHECKING([python include flags])
PYTHON_INCLUDE=`$PYTHON_CONFIG --includes`
AC_MSG_RESULT([$PYTHON_INCLUDE])
])
AC_ARG_VAR([PYTHON_LD], [Linker flags for python, bypassing python-config])
AS_IF([test -z "$PYTHON_LD"], [
AS_IF([test -z "$PYTHON_CONFIG"], [
AC_PATH_PROGS([PYTHON_CONFIG],
[python$PYTHON_VERSION-config python-config],
[no],
[`dirname $PYTHON`])
AS_IF([test "$PYTHON_CONFIG" = no], [AC_MSG_ERROR([cannot find python-config for $PYTHON.])])
])
AC_MSG_CHECKING([python linker flags])
PYTHON_LD=`$PYTHON_CONFIG --ldflags`
AC_MSG_RESULT([$PYTHON_LD])
])
在我的OBJ /目录Makefile.am ...
pyexec_LTLIBRARIES = cerrnimapi.la
cerrnimapi_la_SOURCES = ${SRC_DIR}/lib/PythonAPI.cpp
cerrnimapi_la_LDFLAGS = -avoid-version -module $(PYTHON_LD)
cerrnimapi_la_CXXFLAGS = $(PYTHON_INCLUDE)
我的makefile和我的主程序一起构建了共享库及其obj文件夹。这没有帮助。我也做了一个make install来安装cerrnimapi lib到python文件夹中。这没有帮助。
我也尝试将PythonAPI.cpp添加到我的主程序源中,但无济于事。
任何想法?让我知道什么额外的信息会有帮助。
另外,更多的,我想想我认为我做错了。我不应该只是能够将我的库编译到我的C++程序中,嵌入式python interp将能够看到符号? –
您的构建过程是否生成名为“cerrnimapi.so”的共享库?这就是进口要寻找的东西。至于图书馆中的静态链接,这不是直接鼓励,但如果你想破解它,这里有信息:http://mdqinc.com/blog/2011/08/statically-linking-python-with-cython-generated-模块和 - 包/。 –