boardkeystown
boardkeystown

Reputation: 184

How to build Boost and Compile Boost Python

I'm using a VM to work with boost and python. (Gave up trying on windows)

Can't figure out how to compile. Not sure what to link and path. Don't know how to get -lboost_python -lboost -lpython3.8

OS and versions info:

lsb_release -a 
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 20.04.3 LTS
Release:    20.04
Codename:   focal


gcc --version
gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0

python3 --version
Python 3.8.10

Downloaded the newest boost boost_1_78_0.tar.gz form https://boostorg.jfrog.io/artifactory/main/release/1.78.0/source/

$tar -xvzf boost_1_78_0.tar.gz
cp boost_1_78_0 ~/boost
cd ~/boost/boost_1_78_0
./bootstrap.sh
#it did whatever it did to install b2
b2 install --prefix=../boost
cd ../boost
ls
>> include lib
# Great looks like include and lib is there

Use the tutorial file like it suggest https://www.boost.org/doc/libs/1_76_0/libs/python/doc/html/tutorial/index.html

hello_ext.cpp

char const* greet()
{
   return "hello, world";
}
#include <boost/python.hpp>
BOOST_PYTHON_MODULE(hello_ext)
{
    using namespace boost::python;
    def("greet", greet);
}

test.py

import hello_ext

print(hello_ext.greet())

Compile:

tried based on post from here How to compile, create shared library, and import c++ boost module in Python

g++ -I /usr/include/python3.8 -fpic -c -o hello_ext.o hello_ext.cpp
g++ -o orm.so -shared orm.o -lboost_python -lpython3.8


python3 test.py 
Traceback (most recent call last):
  File "test.py", line 1, in <module>
    import hello_ext
ImportError: /home/bb/Desktop/boostTest2/hello_ext.so: undefined symbol: _ZNK5boost6python7objects21py_function_impl_base9max_arityEv

Tried:

g++ -I /usr/include/python3.8 -fpic -c -o hello_ext.o hello_ext.cpp
/g++ -o hello_ext.so -shared hello_ext.o -L/home/bb/boost/boost/include/boost/python -L /usr/include/python3.8

python3 test.py 
Traceback (most recent call last):
  File "test.py", line 1, in <module>
    import hello_ext
ImportError: /home/bb/Desktop/boostTest2/hello_ext.so: undefined symbol: _ZNK5boost6python7objects21py_function_impl_base9max_arityEv

What am I doing wrong? Why can't I compile? What do I need to do compile the shared object? So I can import it into python?

What is the difference between -L and -lib? Is it a library that ends with .a?

I'm really confused and would greatly appreciate any help. I've looked up many things on google and stack overflow but I've reached a point where I'm just going in circles and not sure what I'm doing wrong.

Thanks

Upvotes: 2

Views: 1810

Answers (1)

Yeab
Yeab

Reputation: 21

For me, I got the error because I had two different versions of boost library installed. one I installed using sudo apt-get install libboost-all-dev and another new version I built from source. So, I had to purge them both completely and install just the last one.

Upvotes: 2

Related Questions