2

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
6
  • 1
    Verify that PythonTest.exe is being linked against the Boost.Python library. Also, you do not want to call Py_Finalize() when using Boost.Python. Commented Aug 5, 2013 at 15:02
  • I double-checked and I am linking against boost python. May I ask why I should not use Py_Finalize()? What does it cause? Commented Aug 5, 2013 at 15:52
  • 2
    Boost.Python does not support safely calling Py_Finalize(). In short, some internal Boost.Python objects will remain alive during Py_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. Commented Aug 5, 2013 at 16:01
  • I appreciate the help, but I'm still getting these errors. Commented Aug 5, 2013 at 22:33
  • 2
    The symbol visibility look as though PythonTest is 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 defining BOOST_PYTHON_STATIC_LIB when either building PythonTest or before including boost/python.hpp. Commented Aug 6, 2013 at 14:24

1 Answer 1

4

Boost.Python can be built for static or dynamic linking. This is controlled by BOOST_PYTHON_STATIC_LIB being defined or not defined during the build process. The boost mailing list seems to indicate that this defintion and built-types are the result of some confusion.

When BOOST_PYTHON_STATIC_LIB is not defined, then Boost.Python assumes dynamic linkage. As a result, Boost.Python decorates symbol visibility for dllimport and dllexport. Based on the linker error, the example code was expecting to import the boost::python::import() function during linkage. If you have verified that the Boost.Python library is being linked, then the problem is likely the result of Boost.Python being built for static linkage, where the functions are not decorated for exporting. To resolve this, do one of the following:

  • Build Boost.Python for dynamic linkage (i.e. make sure BOOST_PYTHON_STATIC_LIB is not defined).
  • Define BOOST_PYTHON_STATIC_LIB when building the example code.
  • Define BOOST_PYTHON_STATIC_LIB in the example code before including boost/python.hpp.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.