boost python library linking issue -- undefined symbol - python

I am using boost-python built for python3 to expose a simple hello-world program. The example can be found here : https://github.com/TNG/boost-python-examples/blob/master/01-HelloWorld/hello.cpp
I ran the following commands to get the shared object:
g++ -fPIC -c -I/usr/include/python3.4m -I/usr/include/python3.4m -Wno-unused-result -g -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -L/usr/lib/x86_64-linux-gnu/libboost_python-py34 hello.cpp
g++ -shared hello.o -o hello.so
After this, I run the python3 -c 'import hello' command and I get the following error:
Traceback (most recent call last):
File "", line 1, in
ImportError: hello.so: undefined symbol: _ZTIN5boost6python7objects21py_function_impl_baseE
I partly understand this issue may be because my boost-python installation may be built for an alternative python version (for instance python2.7). When I run the command:
ls /usr/lib/x86_64-linux-gnu/libboost_python*.so
There are three .so files:
1. libboost_python-py27.so
2. libboost_python-py34.so
3. libboost_python.so
How can this issue be circumvented?

use pkg-config to retrieve ldflags and cflags of your boost library

Installing miniconda might be an option for you. https://conda.io/miniconda.html
This will provide a complete, isolated, python environment. You can then
conda install boost
I've tested this on my system and it worked well. I modified the Makefile from http://www.shocksolution.com/python-basics-tutorials-and-examples/linking-python-and-c-with-boostpython/
My Makefile can be found here:
https://github.com/grelleum/boost-python-with-anaconda

Related

Python.h not found even after I used sudo install

I've been studying darkflow from the following link;
https://github.com/thtrieu/darkflow
on Ubuntu 20.04
I thought there was a problem in my flow file, so I tried to rebuild the build file by the following code
dyi#D-FIP7VAE:~/Tiny-YOLO-voc-traffic-sign-detection$ python3.7 setup.py build_ext --inplace<br/>
and this is what i got.
setup.py:6: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
import imp
running build_ext
building 'darkflow.cython_utils.nms' extension
x86_64-linux-gnu-gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O2 -Wall -g -fdebug-prefix-map=/build/python3.7-ZE4Yn0/python3.7-3.7.10=. -fstack-protector-strong -Wformat -Werror=format-security -g -fdebug-prefix-map=/build/python3.7-ZE4Yn0/python3.7-3.7.10=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -I/home/dyi/.local/lib/python3.7/site-packages/numpy/core/include -I/usr/include/python3.7m -c darkflow/cython_utils/nms.c -o build/temp.linux-x86_64-3.7/darkflow/cython_utils/nms.o
darkflow/cython_utils/nms.c:29:10: fatal error: Python.h: No such file or directory
29 | #include "Python.h"
| ^~~~~~~~~~
compilation terminated.
error: command 'x86_64-linux-gnu-gcc' failed with exit status 1
so I tried this
sudo apt-get install python3-dev
But this didn't work, and I get the same error over and over.
Can anybody find the reason why..?
You can try :
sudo apt install libpython3.7-dev

"python setup.py install" custom dependency location

I'm currently trying to install a custom version of pyyaml linked with libyaml to test how my code execution time might benefit from the extra performance offered (as suggested in https://pyyaml.org/wiki/PyYAMLDocumentation). I've installed libyaml as described in the documentation
./bootstrap && ./configure --prefix /my/install/location
but when I run the install for pyyaml
python setup.py --with-libyaml install
I get
creating build/temp.linux-x86_64-2.7/ext
/usr/bin/gcc -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/usr/bin/python2.7 -c ext/_yaml.c -o build/temp.linux-x86_64-2.7/ext/_yaml.o
gcc: error: ext/_yaml.c: No such file or directory
gcc: fatal error: no input files
How do I point the install to /my/install/location? Is there a way to extend the search path to include the extra location, or is there another way that this should be brought together?

How to compile c++ code with python2.7 i386 when I have 64 bit system with amd64 python installed

I'm using ubuntu 14.04 64 bits. I have instaled python2.7 amd64, and I use the flags 'python-config --cflags' and 'python-config --ldflags' in the makefile.
They show, respectively:
-I/usr/include/python2.7 -I/usr/include/x86_64-linux-gnu/python2.7
-fno-strict-aliasing -D_FORTIFY_SOURCE=2 -g -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes
-L/usr/lib/python2.7/config-x86_64-linux-gnu -L/usr/lib -lpthread -ldl -lutil -lm -lpython2.7 -Xlinker -export-dynamic -Wl,-O1 -Wl,-Bsymbolic-functions
My objective is to develop a 32-bit version with -m32, but I encounter the following error:
/usr/include/python2.7/pyconfig.h:7:49: fatal error: i386-linux-gnu/python2.7/pyconfig.h: File or directory not found.
# include <i386-linux-gnu/python2.7/pyconfig.h>
^
compilation terminated.
I've researched a lot trying to resolve this problem. I've tried installing python2.7:i386 alongside with python2.7:amd64 but some packages get removed and I had no avail. The only way I've suceeded was setting up a chroot environment on which I installed a pseudo-system for ubuntu trusty 32 bit. Altough I want to know if it's possible to do this with python virtualenv, and if so, could you offer some examples? I could not find nor understand how it can be used to link with my code.
This is the first time I ask a question on this site, I apologize in advance if I did it incorrectly! And thank you in advance.

Python 2.7.6 error when building from source: gcc.orig: directory": No such file or directory

I've been provisioned a very bare-bones RHEL 4.4 at work; e.g., it didn't have java or gcc installed.
I have followed this guide to install python 2.7.6 on another VM (RHEM 4.6) successfully. However on this new vm, I cannot make without getting this error:
gcc -pthread -c -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I. -IInclude -I./Include -DPy_BUILD_CORE \
-DSVNVERSION="\"`LC_ALL=C echo Unversioned directory`\"" \
-DHGVERSION="\"`LC_ALL=C `\"" \
-DHGTAG="\"`LC_ALL=C `\"" \
-DHGBRANCH="\"`LC_ALL=C `\"" \
-o Modules/getbuildinfo.o ./Modules/getbuildinfo.c
gcc.orig: directory": No such file or directory
<command-line>: warning: missing terminating " character
./Modules/getbuildinfo.c: In function â_Py_svnversionâ:
./Modules/getbuildinfo.c:63: error: missing terminating " character
./Modules/getbuildinfo.c:63: error: expected expression before â;â token
make: *** [Modules/getbuildinfo.o] Error 1
I tried what was suggested in this unrelated bug report:
SVNVERSION="Unversioned directory"
./configure
make
but received the same error.
I saw this question on super user, which suggests to check the output of the svnversion command in the Modules directory, but I don't have svn or svnversion on this machine. It also suggests to edit the function _PY_svnversion in Modules/getbuildinfo.c, which I did but received the same error.
Any ideas?
Ok I found the answer in my case:
issue configure which will create a new Makefile
edit the Makefile with vi.
search for SVNVERSION and set it to
SVNVERSION= ""
make will then work.

How to embed particular version of python interpreter in c in OS X

I want to embed python in C. But I find that the version of python interpreter which is embedded in my program is 2.7 (The default version on mac).
How could I specify particular version of python interpreter when I compile the c codes in mac os x. The gcc in os x is definitely different from in linux.
I have already installed python3 through HomeBrew.
Thanks a lot.
UPDATE:
I try to use python3.4-config --cflags and python3.4-config --ldflags to find out the required compiler and linker flags. Then I get these recommended flags when compiling & linking:
-I/usr/local/Cellar/python3/3.4.1/Frameworks/Python.framework/Versions/3.4/include/python3.4m -I/usr/local/Cellar/python3/3.4.1/Frameworks/Python.framework/Versions/3.4/include/python3.4m -Wno-unused-result -Werror=declaration-after-statement -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/usr/local/include -I/usr/local/opt/sqlite/include
and
-L/usr/local/Cellar/python3/3.4.1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/config-3.4m -ldl -framework CoreFoundation -lpython3.4m
After this, I assemble these flags along with source file into gcc, and obtain an error:
Undefined symbols for architecture x86_64:
"_PyUnicodeUCS2_FromString", referenced from:
_main in py2-5d8da5.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
The C code which I test here comes from Python Documentation
I got the same errors when trying to do this tutorial on OSX. You don't need all of the flags that the config utility spits out. You definitely don't need the corefoundation framework if you're just doing the embedding tutorial. Just use the include directory for headers:
-I/usr/local/Cellar/python3/3.4.1/Frameworks/Python.framework/Versions/3.4/include/python3.4m -I/usr/local/Cellar/python3/3.4.1/Frameworks/Python.framework/Versions/3.4/include/python3.4m
, and the library to link to:
-L/usr/local/Cellar/python3/3.4.1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/config-3.4m -lpython3.4m
so here's a one-liner to compile and link:
gcc -I/usr/local/Cellar/python3/3.4.1/Frameworks/Python.framework/Versions/3.4/include/python3.4m -I/usr/local/Cellar/python3/3.4.1/Frameworks/Python.framework/Versions/3.4/include/python3.4m -L/usr/local/Cellar/python3/3.4.1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/config-3.4m -lpython3.4m /path/to/main.c -o /path/to/output/executable

Categories

Resources