[cgo]Python C API Error: undefined reference to 'Py_True' - python

Environment:
Ubuntu20.04,Golang 1.9.2,Python 3.7.10
I am trying to use this package: https://github.com/DataDog/go-python3
The problem happened when 'go get github.com/DataDog/go-python3' was executed.
By the way, I have noticed that there is a similar issure:https://github.com/DataDog/go-python3/issues/41. However the answer does not work for me.
I use 'go get -x' to chech what is happening:
mininet#mininet-vm:~/go/src/Dockerfiles$ go get -x github.com/DataDog/go-python3
...
pkg-config --cflags python3
pkg-config --libs python3
CGO_LDFLAGS="-g" "-O2" "-L/opt/py/lib" "-lpython3.7m" /usr/local/go/pkg/tool/linux_amd64/cgo -objdir
...
# github.com/DataDog/go-python3
/usr/bin/ld: $WORK/github.com/DataDog/go-python3/_obj/_cgo_main.o:/tmp/go-build/github.com/DataDog/go-python3/_obj/cgo-generated-wrappers:236: undefined reference to `Py_True'
/usr/bin/ld: $WORK/github.com/DataDog/go-python3/_obj/_cgo_main.o:/tmp/go-build/github.com/DataDog/go-python3/_obj/cgo-generated-wrappers:233: undefined reference to `Py_None'
/usr/bin/ld: $WORK/github.com/DataDog/go-python3/_obj/_cgo_main.o:/tmp/go-build/github.com/DataDog/go-python3/_obj/cgo-generated-wrappers:230: undefined reference to `Py_False'
collect2: error: ld returned 1 exit status
see CGOFLAGS, I have add '-L/opt/py/lib -lpython3.7m' correctly to the end by using pkg-config and setting 'PKG_CONFIG_PATH' and 'LD_LIBRARY_PATH' varibles.
I don't know what happened, maybe go 1.9 is too old? But It seems like a link problem.
I use VM and docker to retry. And the problem is the same.
Thanks for all your possible help.

Related

Error while creating dll from C code for Python code

I have some C code which uses "mex.h". I am trying to create shared library (ie create dll file) so that it can be used in Python by using ctypes library.
Following is the command that I have tried for creating dll file, but I am getting an error.
Command Used
gcc -fPIC -IC:\Progra~1\MATLAB\R2020b\extern\include -IC:\Progra~1\MATLAB\R2020b\extern\lib -lC:\Progra~1\MATLAB\R2020b\bin\win64\libmat.dll -lC:\Progra~1\MATLAB\R2020b\bin\win64\libmx.dll -shared -o whitsmw_Cd1.dll whitsmw_Cd1.c
Error
c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/bin/ld.exe: cannot find -lC:\Progra~1\MATLAB\R2020b\bin\win64\libmat.dll
c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/bin/ld.exe: cannot find -lC:\Progra~1\MATLAB\R2020b\bin\win64\libmx.dll
collect2.exe: error: ld returned 1 exit status
I have checked the paths are correct. I would be helpful if someone can provide some help.

Cross compiling python for ARM from source

I am cross compiling python 3.7 for ARM using TI-SDK.
I have downloaded the source code on ubuntu and followed the below instructions
Set the path of SDK (export PATH = $PATH:)
./confgure
make
When I run make, I get the errors below. Can you please tell me how to resolve the linker errors?
*ar rc libpython3.5m.a Modules/config.o Modules/getpath.o Modules/main.o Modules/gcmodule.o
ar rc libpython3.5m.a Modules/_threadmodule.o Modules/signalmodule.o Modules/posixmodule.o Modules/errnomodule.o Modules/pwdmodule.o Modules/_sre.o Modules/_codecsmodule.o Modules/_weakref.o Modules/_functoolsmodule.o Modules/_operator.o Modules/_collectionsmodule.o Modules/itertoolsmodule.o Modules/atexitmodule.o Modules/_stat.o Modules/timemodule.o Modules/_localemodule.o Modules/_iomodule.o Modules/iobase.o Modules/fileio.o Modules/bytesio.o Modules/bufferedio.o Modules/textio.o Modules/stringio.o Modules/zipimport.o Modules/faulthandler.o Modules/_tracemalloc.o Modules/hashtable.o Modules/symtablemodule.o Modules/xxsubtype.o
ranlib libpython3.5m.a
gcc -pthread -Xlinker -export-dynamic -o python Programs/python.o libpython3.5m.a -lpthread -ldl -lutil -lm
libpython3.5m.a(fileutils.o): In function `set_inheritable':
/home/pcadmin/work/OTIS/Python-3.5.9/Python/fileutils.c:892: undefined reference to `fcntl64'
/home/pcadmin/work/OTIS/Python-3.5.9/Python/fileutils.c:903: undefined reference to `fcntl64'
libpython3.5m.a(fileutils.o): In function `get_inheritable':
/home/pcadmin/work/OTIS/Python-3.5.9/Python/fileutils.c:777: undefined reference to `fcntl64'
/home/pcadmin/work/OTIS/Python-3.5.9/Python/fileutils.c:777: undefined reference to `fcntl64'
libpython3.5m.a(fileutils.o): In function `set_inheritable':
/home/pcadmin/work/OTIS/Python-3.5.9/Python/fileutils.c:892: undefined reference to `fcntl64'
libpython3.5m.a(fileutils.o):/home/pcadmin/work/OTIS/Python-3.5.9/Python/fileutils.c:903: more undefined references to `fcntl64' follow
collect2: error: ld returned 1 exit status
Makefile:555: recipe for target 'python' failed
make: *** [python] Error 1*
Please comment if more information is required to help me out with this issue.
I found the solution. Thought it may help others if i post it.
The linker error is due to the dependency on libxml2. I have cross compiled libxml2 and linked to python and it worked.
The configure script includes the path of libxml along with other options.
./configure CC=arm-linux-gnueabihf-gcc CXX=arm-linux-gnueabihf-g++ AR=arm-linux-gnueabihf-ar LD=arm-linux-gnueabihf-ld RANLIB=arm-linux-gnueabihf-ranlib --host=arm-linux-gnueabihf --target=arm -prefix=/home/sagar/otis/python3_install --without-sqlite3 --without-pdo-sqlite --without-pear --enable-simplexml --disable-mbregex --enable-sockets --enable-fpm --disable-opcache --enable-libxml --without-zlib --with-libxml-dir=/home/sagar/otis/libxml/_install/ --build=x86_64-linux-gnu --disable-all --disable-ipv6 ac_cv_have_long_long_format=yes ac_cv_file__dev_ptmx=no ac_cv_file__dev_ptc=no

"Cannot find -lfftw3_omp" error when compiling a third-party package. How to link the existing library?

I'm trying to compile a third-party package on my Ubuntu 16.04 system in order to use it in Anaconda which is the default Python environment. When I run make the compilation stops with the following error:
/home/myname/anaconda3/compiler_compat/ld: cannot find -lfftw3_omp
/home/myname/anaconda3/compiler_compat/ld: cannot find -lm
collect2: error: ld returned 1 exit status
error: command 'gcc' failed with exit status 1
Makefile:109: recipe for target '_shtns.so' failed
make: *** [_shtns.so] Error 1
It seems that Anaconda does not have access to the two libraries mentioned in the error message. The library fftw3_omp.so definitely exists in the folder /home/myname/usr/lib. I'm not sure what -lm is, though. How can I link the missing libraries correctly?
I got the similar error:
/home/sfchen/anaconda3/envs/dlpy2/compiler_compat/ld: cannot find -lpthread
/home/sfchen/anaconda3/envs/dlpy2/compiler_compat/ld: cannot find -lc
and solved by follow steps.
find the root of the lib (e.g. lpthread)
ld -lpthread --verbose
output like this:
attempt to open //usr/local/lib/x86_64-linux-gnu/libpthread.so failed
attempt to open //usr/local/lib/x86_64-linux-gnu/libpthread.a failed
attempt to open //lib/x86_64-linux-gnu/libpthread.so failed
attempt to open //lib/x86_64-linux-gnu/libpthread.a failed
attempt to open //usr/lib/x86_64-linux-gnu/libpthread.so succeeded
opened script file //usr/lib/x86_64-linux-gnu/libpthread.so
opened script file //usr/lib/x86_64-linux-gnu/libpthread.so
attempt to open /lib/x86_64-linux-gnu/libpthread.so.0 succeeded
/lib/x86_64-linux-gnu/libpthread.so.0
attempt to open /usr/lib/x86_64-linux-gnu/libpthread_nonshared.a succeeded
libc.so.6 needed by /lib/x86_64-linux-gnu/libpthread.so.0
found libc.so.6 at //lib/x86_64-linux-gnu/libc.so.6
ld-linux-x86-64.so.2 needed by /lib/x86_64-linux-gnu/libpthread.so.0
found ld-linux-x86-64.so.2 at //lib/x86_64-linux-gnu/ld-linux-x86-64.so.2
ld: warning: cannot find entry symbol _start; not setting start address
From the 5th line above
attempt to open //usr/lib/x86_64-linux-gnu/libpthread.so succeeded
get the path1 = /usr/lib/x86_64-linux-gnu/libpthread.so
From the error message: /home/sfchen/anaconda3/envs/dlpy2/compiler_compat/ld: cannot find -lpthread
get the path2 = /home/sfchen/anaconda3/envs/dlpy2/compiler_compat/
Last, input like this :
ln -s path1 path2
Specifically,
ln -s /usr/lib/x86_64-linux-gnu/libpthread.so /home/sfchen/anaconda3/envs/dlpy2/compiler_compat/
that's all, hope it may be helpful!

Build caffe with Python ( cannot find -lboost_python3 )

I'm trying to build caffe with python but it keep saying this
CXX/LD -o python/caffe/_caffe.so python/caffe/_caffe.cpp
/usr/bin/ld: cannot find -lboost_python3
collect2: error: ld returned 1 exit status
make: *** [python/caffe/_caffe.so] Error 1
This is what I get when I try to locate boost_python
$ sudo locate boost_python
/usr/lib/x86_64-linux-gnu/libboost_python-py27.a
/usr/lib/x86_64-linux-gnu/libboost_python-py27.so
/usr/lib/x86_64-linux-gnu/libboost_python-py27.so.1.55.0
/usr/lib/x86_64-linux-gnu/libboost_python-py33.a
/usr/lib/x86_64-linux-gnu/libboost_python-py33.so
/usr/lib/x86_64-linux-gnu/libboost_python-py33.so.1.55.0
/usr/lib/x86_64-linux-gnu/libboost_python-py34.a
/usr/lib/x86_64-linux-gnu/libboost_python-py34.so
/usr/lib/x86_64-linux-gnu/libboost_python-py34.so.1.55.0
/usr/lib/x86_64-linux-gnu/libboost_python.a
/usr/lib/x86_64-linux-gnu/libboost_python.so
I've add this path also
## .bashrc
export LD_LIBRARY_PATH="/usr/lib/x86_64-linux-gnu":$LD_LIBRARY_PATH
Any idea why is that happing?
I've found the problem. it turned out that it tries to look for something with that name of libboost_python3.so after changing the name in Makefile.config from boost_python3 to boost_python-py34, it worked just fine!
I know this thread is quite old, but :
dnf install boost-python3-devel
may help !
I wanted to build Caffe and faced the same issue. Unfortunately, none of the answers work in my case. I checked the location of lboost_python by the following command:
find /usr/lib -name libboost_python-py
I found the lboost_python libraries and here are some of them:
libboost_python37.a
libboost_python37-mt.a
libboost_python37-mt.so
libboost_python37-mt.so.1.68.0
Then in the Makefile.config file I changed the following line:
PYTHON_LIBRARIES := boost_python python3.7m to
PYTHON_LIBRARIES := boost_python37-mt python3.7m

Python / C++ binding, how to link agains static c++ library (portaudio) with distutils?

I am trying to staticaly link the "c++ portaudio library" against my "C++ demo module" which is a python callable library (module).
I'm doing this with distutils, and in order to perform the static linking, I've added the libportaudio to the extra_objects argument, as follows:
module1 = Extension(
"demo",
sources=cppc,
# TODO remove os dependency
extra_compile_args=gccArgs,
# link against shared libraries
#libraries=[""]
# link against static libraries
extra_objects=["./clib-3rd-portaudio/libportaudio.a"]) # << I've added the static lib here
Compiling with "python setup.py build" results in the following linker error:
/usr/bin/ld: ./clib-3rd-portaudio/libportaudio.a(pa_front.o): relocation R_X86_64_32 against `.rodata.str1.8' can not be used when making a shared object; recompile with -fPIC
./clib-3rd-portaudio/libportaudio.a: error adding symbols: Bad value
collect2: error: ld returned 1 exit status
So at this point I've tried the obvious, I've added the -fPIC flagg to gccArgs (note extra_compile_args=gccArgs above), as follows:
gccArgs = [
"-Icsrc",
"-Icsrc/paExamples",
"-Icinc-3rd-portaudio",
"-Icinc-3rd-portaudio/common",
"-Icinc-3rd-portaudio/linux",
"-fPIC"] # << I've added the -fPIC flag here
However this results in the exact same error, so I guess the -fPIC flag is not the root cause. I'm probably missing something trivial, but I'm a bit lost here, hope somebody can help.
As the error message said, you should recompile the external library libportaudio.a with -fPIC argument, NOT your own codes. That's why it doesn't help to add -fPIC to your extra_compile_args.
Several other posts suggest that the file libportaudio.a cannot be used to build shared library, probably because the default build settings of portaudio don't include -fPIC.
To recompile portaudio correctly, download the source and try to run ./configure with -shared option (or something similar). If you cannot find the proper option, then modify the Makefile and append -fPIC to the extra compile options. You can also compile each object file manually and pack them into libportaudio.a.
Since your target file (libdemo.so) is a shared library, you must make sure ANY object codes included inside are compiled with -fPIC option. To understand why you need this option, please refer to:
What does -fPIC mean when building a shared library? and Position Independent Code (PIC) in shared libraries

Categories