Force CMake/VisualStudio to use static libs of Boost.Python

后端 未结 2 476
独厮守ぢ
独厮守ぢ 2021-01-06 10:33

I\'m currently trying to build on Windows (with Intel compiler) a big project compiling very well on UNIX with CMake. Here is a reduced simple example of my problem.

相关标签:
2条回答
  • 2021-01-06 10:49

    It seems like you have a problem with finding dll's on your machine. As a temporary fix, you can try to rename your .dll file to .pyd.

    0 讨论(0)
  • 2021-01-06 11:05

    When using Boost.Python, the default configuration is dynamic linking. To force static linking, define BOOST_PYTHON_STATIC_LIB in the compilation. Consider either:

    • Add add_definitions(-DBOOST_PYTHON_STATIC_LIB) within CMake
    • Add #define BOOST_PYTHON_STATIC_LIB before including boost/python.hpp
    • Add #define BOOST_PYTHON_STATIC_LIB in boost/config/user.hpp

    Boost.Python is an exception to the Boost_USE_STATIC_LIBS CMake variable. The FindBoost documentation notes:

    On Visual Studio and Borland compilers Boost headers request automatic linking to corresponding libraries. [...] Boost automatic linking typically requests static libraries with a few exceptions (such as Boost.Python).

    Unfortunately, the BOOST_PYTHON_STATIC_LIB option is neither listed in the Boost user settable options, Choosing a Boost.Python Library Binary, nor Boost.Python Configuration Information documentation.


    A few other recommendations given your example code:

    • Per Boost.Python's embedding documentation, do not invoke Py_Finalize(). 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.
    • Do not directly include, python.h. If you must, consider including boost/python/detail/wrap_python.hpp instead.
    • Do not include any system headers before python.h (or the Boost.Python files). This restriction is imposed by Python (see here).

    The latter two recommendations documented in the #include issues section.

    0 讨论(0)
提交回复
热议问题