我正在嘗試構建我的第一個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