我正在尝试构建我的第一个 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
列出的所有内容。由于缺少符号,链接仍然不起作用。