我正在尝试构建我的第一个Boost.Python示例。需要帮助Boost.Python入门
#include <iostream>
#include <boost/python.hpp>
using namespace boost::python;
class Hello {
public:
std::string greet() {
std::cout << "Hello World" << std::endl;
}
};
BOOST_PYTHON_MODULE(hello)
{
class_<Hello>("Hello")
.def("greet", &Hello::greet);
}
int main() {
std::cout << "Boost.Python Test" << std::endl;
Hello hello;
hello.greet();
return 0;
}
编辑:作为@cdhowie指出,Python开发标题丢失。我找到并包含了所需的头文件。现在,链接器在抱怨:
10:43:58 **** Build of configuration BoostPythonTest-DPar for project BoostPythonTest
****
make all
Building file: ../src/BoostPythonTest.cpp
Invoking: GCC C++ Compiler
/usr/local/bin/g++-4.7 -I/usr/local/Cellar/python3/3.3.0/Frameworks/Python.framework/Versions/3.3/include/python3.3m -I/usr/include -I/usr/local/Cellar/gcc/4.7.2/gcc/include/c++/4.7.2 -O0 -g3 -p -pg -Wall -c -fmessage-length=0 -std=c++11 -MMD -MP -MF"src/BoostPythonTest.d" -MT"src/BoostPythonTest.d" -o "src/BoostPythonTest.o" "../src/BoostPythonTest.cpp"
Finished building: ../src/BoostPythonTest.cpp
Building target: libBoostPythonTest-DPar.dylib
Invoking: MacOS X C++ Linker
/usr/local/bin/g++-4.7 -L/usr/local/Cellar/python3/3.3.0/Frameworks/Python.framework/Versions/3.3/lib/python3.3/config-3.3m -L/usr/local/Cellar/python3/3.3.0/Frameworks/Python.framework/Versions/3.3/lib -L/usr/local/Cellar/boost/1.51.0/lib -std=c++11 -Xlinker -ldl -framework CoreFoundation -lpython3.3m -dynamiclib -o "libBoostPythonTest-DPar.dylib" ./src/BoostPythonTest.o -lpython3.3m -lboost_python-mt -lpython3.3
Undefined symbols for architecture x86_64:
"boost::python::detail::init_module(PyModuleDef&, void (*)())", referenced from:
_PyInit_hello in BoostPythonTest.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
make: *** [libBoostPythonTest-DPar.dylib] Error 1
我已经挂-lpython3.3m -lboost_python-mt -lpython3.3
- 还有什么缺失?
编辑:我想我已链接到python3.3-config
列出的所有内容。由于缺少符号,链接仍然不起作用。
我相信你正确地诊断了这个问题。我现在试图在Python 3.3上构建Boost.Python。 – clstaudt 2013-04-22 09:06:38
当我尝试构建Boost.Python时,出现[以下错误](https://gist.github.com/anonymous/5433641)。 'gcc.link.dll ../../../../bin.v2/libs/python/build/gcc-4.2.1/debug/libboost_python.dylib ld:warning:目录找不到选项' -L/usr/local/Cellar/python3/3.3.0/Frameworks/Python.framework/Versions/3.3/lib/python3.3/config' ld:未知选项:-R collect2:ld返回1退出状态 这里发生了什么? – clstaudt 2013-04-22 09:53:35
@cls:如果您使用的是Apple的GCC工具链版本,请尝试将工具集指定为'darwin'而不是'gcc'。 – 2013-04-22 13:35:53