exposing C++ class in Python ( only ET_DYN and ET_EXEC can be loaded)

2024/10/5 15:23:38

I was looking at here to see how to expose c++ to Python. I have built Python deep learning code which uses boost-python to connect c++ and python and it is running ok, so my system has things for boost-python alread setup. Here is my hello.cpp code (where I used WorldC and WorldP to clearly show the C++ and Python class name usage in the declaration. I don't know why the original web page is using the same class name World to cause confusion to beginners.)

#include <boost/python.hpp>
using namespace boost::python;struct WorldC
{void set(std::string msg) { this->msg = msg; }std::string greet() { return msg; }std::string msg;
};BOOST_PYTHON_MODULE(hello)
{class_<WorldC>("WorldP").def("greet", &WorldC::greet).def("set", &WorldC::set);
}

and this is how I made hello.so

g++ -shared -c -o hello.so -fPIC hello.cpp -lboostpython -lpython2.7 -I/usr/local/include/python2.7

When I run import hello in python, it gives me this error.

>>> import hello
Traceback (most recent call last):File "<stdin>", line 1, in <module>
ImportError: ./hello.so: only ET_DYN and ET_EXEC can be loaded

Can anybody tell me what's wrong?
(I'm using anaconda2 under my home directory for python environment, but since my deep learning code builds ok with boost-python, there should be no problem including boost/python.hpp in my system directory)

Answer

I've long forgotten about this problem and got to revisit this issue today.
I found two problems. The first problem was that I gave -c option which made the compiler only compile the source and not link. The second problem was that the library name was wrong(I search /usr/lib64 and there was libboost_python.so, so it should be -lboost_python instead of -lboostpython). So the correct way to build it is :

g++ -shared -o hello.so -fPIC hello.cpp -lboost_python -lpython2.7 -I/usr/local/include/python2.7

I found it runs ok in python. :)

https://en.xdnf.cn/q/70472.html

Related Q&A

Migrations in mongoengine: InvalidId

I am working with mongoengine and trying to do a simple migration. I have a field which I would like to migrate from being a StringField to a ReferenceField to another Object. I planned on doing the …

Keras pretrain CNN with TimeDistributed

Here is my problem, I want to use one of the pretrain CNN network in a TimeDistributed layer. But I have some problem to implement it.Here is my model:def bnn_model(max_len):# sequence length and resne…

How to `pause`, and `resume` download work?

Usually, downloading a file from the server is something like this: fp = open(file, wb) req = urllib2.urlopen(url) for line in req:fp.write(line) fp.close()During downloading, the download process just…

AttributeError: module rest_framework.serializers has no attribute NullBooleanField

After upgrading djangorestframework from djangorestframework==3.13.1 to djangorestframework==3.14.0 the code from rest_framework.serializers import NullBooleanFieldThrowsAttributeError: module rest_fra…

Pandas convert dataframe values to column names

I want to use dataframe values as column names and simplify the dataframe.I tried df.stack() and then index.map({0[0]}_{0[1]}.format)Input_df(Got this df by doing a groupby):link price dateA 1 …

Pandas replace part of string with values from dictionary

I would like to replace the words in my dataframedf = pd.DataFrame({"Text": ["The quick brown fox jumps over the lazy dog"]})which match the keys in the following dictionarydic = {&…

Tensorflow autoencoder cost not decreasing?

I am working on unsupervised feature learning using autoencoders using Tensorflow. I have written following code for the Amazon csv dataset and when I am running it the cost is not decreasing at every …

Seconds since epoch to relative date

Im working with dates since epoch, and already got, for example:date = 6928727.56235Id like to transform this into another relative format, so that Ill be able to transform this into something relative…

ring buffer with numpy/ctypes

Im developing a client which will receive the [EEG] data over tcp and write it to the ring buffer. I thought it can be very convenient to have the buffer as a ctypes or numpy array because its possible…

Get all available timezones

Im currently working on an application that is required to support multiple timezones.For that, Im using the dateutil library. Now, I need a way to present the user a list of all available timezones th…