Static linking with boost python

2019-07-22 02:09发布

问题:

I'm trying to create a boost python extension but would prefer to statically link the boost python libraries. Otherwise you need the exact same version of boost installed on every machine you use the python module. I'm not using bjam though.

This works in linux (ubuntu) but results in dynamic linking:

g++ -o python_example.o -c python_example.cpp -Wall -fPIC -I/usr/include/python2.7
g++ -shared -o python_example.so python_example.o -lpython2.7 -lboost_python -lboost_system

python_example.cpp is just the basic example code:

#include <Python.h>
#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
#include <boost/python.hpp>

char const* greet()
{
   return "hello, world";
}

BOOST_PYTHON_MODULE(python_example)
{
    using namespace boost::python;
    def("greet", greet);
}

Lots of google results out there which gave me a lot of things to try but nothing that quite worked.

回答1:

Maybe put a

#define BOOST_PYTHON_STATIC_LIB

on the top of your source file will help?

https://stackoverflow.com/a/35440164/9358910