So, I'm attempting to embed Python into C++. I have gotten fairly far in, and have been able to do basic things like run strings of Python. As soon as I tried to use Boost::Python::Object I began getting these 4 linker errors.
I built boost using BJAM with Boost 1.54.0 and Python 2.7.5.
Python Lib Build Commands:
bootstrap
.\b2 toolset=msvc-10.0 --with-python
Minimal Code Example :
#include <boost/python.hpp>
#include <iostream>
int main(int, char **)
{
Py_Initialize();
PyRun_SimpleString("import Entity");
boost::python::object main_module = boost::python::import("__main__");
boost::python::object main_namespace = main_module.attr("__dict__");
Py_Finalize();
std::cin.get();
return 0;
}
Linker Errors:
1>PythonTest.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) class boost::python::api::object __cdecl boost::python::import(class boost::python::str)" (__imp_?import@python@boost@@YA?AVobject@api@12@Vstr@12@@Z) referenced in function _main
1>PythonTest.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) protected: __thiscall boost::python::detail::str_base::str_base(char const *)" (__imp_??0str_base@detail@python@boost@@IAE@PBD@Z) referenced in function "public: __thiscall boost::python::str::str(char const *)" (??0str@python@boost@@QAE@PBD@Z)
1>E:\Dev\PythonTest\Debug\PythonTest.exe : fatal error LNK1120: 2 unresolved externals
PythonTest.exeis being linked against the Boost.Python library. Also, you do not want to callPy_Finalize()when using Boost.Python.Py_Finalize(). In short, some internal Boost.Python objects will remain alive duringPy_Finalize(), and only attempt to be deleted when Boost.Python unloads, causing the objects to attempt deletion with a non-existent interpreter. It is noted in the Embedding - Getting Started section, and listed in the TODO.PythonTestis expecting the Boost.Python library to have been built for dynamic linking. It may be worth checking symbol visibility within the built Boost.Python library. If the symbols are not exported, then try to build for the static library by definingBOOST_PYTHON_STATIC_LIBwhen either buildingPythonTestor before includingboost/python.hpp.