C++ Boost + Python: undefined reference to Py_Dealloc - python

I'm trying to use the Python module on Boost. However, I'm getting an error that I cannot find what to do with.
It seems a missing switch or something, but I really could not find...
Does anyone have a clue?
Thanks!!!
/usr/include/python3.8/object.h|478|undefined reference to `_Py_Dealloc'|
System data:
Using Code::blocks, Python 3.8, Linux Mint, installed Boost by apt-get
Build log:
g++ -Wall -fexceptions -g -I/usr/lib/x86_64-linux-gnu/ -I/usr/include/python3.8/ -I/home/tavares/Downloads/boost/boost_1_77_0/stage/lib -c /home/tavares/Trabalho/pesquisa/softwares/optpipeline/PyOPY/PyOPI/bark.cpp -o obj/Debug/bark.o
g++ -Wall -fexceptions -g -I/usr/lib/x86_64-linux-gnu/ -I/usr/include/python3.8/ -I/home/tavares/Downloads/boost/boost_1_77_0/stage/lib -c /home/tavares/Trabalho/pesquisa/softwares/optpipeline/PyOPY/PyOPI/main.cpp -o obj/Debug/main.o
g++ -L/usr/lib/x86_64-linux-gnu/ -o bin/Debug/PyOPI obj/Debug/bark.o obj/Debug/main.o -lboost_program_options -lboost_system -lboost_python38
/usr/bin/ld: obj/Debug/bark.o: in function `_Py_DECREF':
/usr/include/python3.8/object.h:478: undefined reference to `_Py_Dealloc'
/usr/bin/ld: obj/Debug/bark.o: in function `boost::python::detail::none()':
/usr/local/include/boost/python/detail/none.hpp:16: undefined reference to `_Py_NoneStruct'
/usr/bin/ld: /usr/local/include/boost/python/detail/none.hpp:16: undefined reference to `_Py_NoneStruct'
/usr/bin/ld: obj/Debug/bark.o: in function `boost::python::api::object::object()':
/usr/local/include/boost/python/object_core.hpp:400: undefined reference to `_Py_NoneStruct'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu//libboost_python38.so: undefined reference to `PyExc_ValueError'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu//libboost_python38.so: undefined reference to `PyLong_AsLong'

This is kinda old, but try adding
$(python3.10-config --ldflags --embed)
to the commands at the linking phase.
Your link command becomes something like
g++ -L/usr/lib/x86_64-linux-gnu/ -o bin/Debug/PyOPI obj/Debug/bark.o obj/Debug/main.o -lboost_program_options -lboost_system -lboost_python38
$(python3.10-config --ldflags --embed)
Hope this helps someone.

Related

(DEBIAN) Weird error when building Python from source

After running make I am getting this weird error or something more like an error
Wno-unused-parameter -Wno-missing-field-initializers -Werror=implicit-function-declaration -fvisibility=hidden -I./Include/internal -I. -I./Include -DPy_BUILD_CORE -o Objects/descrobject.o Objects/descrobject.c
^Cmake: *** [Makefile:1803: Objects/descrobject.o] Interrupt
Any Idea Why??

Embedded python fails to compile on Raspberry Pi

I installed python 3.9.1 on my Raspberry Pi following the instructions here https://www.ramoonus.nl/2020/10/06/how-to-install-python-3-9-on-raspberry-pi/ and set it as the default python interpreter. I got my compiling and linking parameters for embedded Python following the instructions here https://docs.python.org/3.9/extending/embedding.html#compiling-and-linking-under-unix-like-systems
I tried a simple test with the following code (test.c) :
#include <Python.h>
int
main(int argc, char *argv[])
{
wchar_t *program = Py_DecodeLocale(argv[0], NULL);
if (program == NULL) {
fprintf(stderr, "Fatal error: cannot decode argv[0]\n");
exit(1);
}
Py_SetProgramName(program); /* optional but recommended */
Py_Initialize();
PyRun_SimpleString("from time import time,ctime\n"
"print('Today is', ctime(time()))\n");
Py_Finalize();
PyMem_RawFree(program);
return 0;
}
and then
gcc -I/usr/local/opt/python-3.9.1/include/python3.9 -I/usr/local/opt/python-3.9.1/include/python3.9 -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -c test.c -o test.o
and
gcc -L/usr/local/opt/python-3.9.1/lib/python3.9/config-3.9-arm-linux-gnueabihf -L/usr/local/opt/python-3.9.1/lib -lcrypt -lpthread -ldl -lutil -lm -o test.o
and got
/usr/lib/gcc/arm-linux-gnueabihf/4.9/../../../arm-linux-gnueabihf/crt1.o: In function '_start': /build/glibc-P1SmLh/glibc-2.19/csu/../ports/sysdeps/arm/start.S:119: undefined reference to 'main' collect2: error: ld returned 1 exit status
Trying to compile the example at https://docs.python.org/3.9/extending/embedding.html#pure-embedding throws the same error. What could the problem be?
Edit:
After Expolarity's comment I changed the linker command to:
gcc test.o -L/usr/local/opt/python-3.9.1/lib/python3.9/config-3.9-arm-linux-gnueabihf -L/usr/local/opt/python-3.9.1/lib -lcrypt -lpthread -ldl -lutil -lm -o test
which seems to have worked but threw me a bunch of other errors:
test.o: In function main': /home/pi/Downloads/test.c:6: undefined reference to Py_DecodeLocale'
/home/pi/Downloads/test.c:11: undefined reference to Py_SetProgramName' /home/pi/Downloads/test.c:12: undefined reference to Py_Initialize'
/home/pi/Downloads/test.c:13: undefined reference to PyRun_SimpleStringFlags' /home/pi/Downloads/test.c:15: undefined reference to Py_Finalize'
/home/pi/Downloads/test.c:16: undefined reference to `PyMem_RawFree'
collect2: error: ld returned 1 exit status
This seems more serious. Any ideas?
After tttapa's answer over here it finally worked by adjusting the linker command as so:
gcc test.o -L/usr/local/opt/python-3.9.1/lib/python3.9/config-3.9-arm-linux-gnueabihf -L/usr/local/opt/python-3.9.1/lib -lcrypt -lpthread -ldl -lutil -lm -lpython3.9 -o test
Edit: Expolarity also answered correctly just after tttapa. Thanks a lot everyone!

Linking error trying to compile C code embedded with Python

I'm trying to import and run this python function inside C code:
import requests
import json
url = 'http://127.0.0.1:5000/'
def verify():
code = input('Show-me the code: ')
r = requests.post(url, data=json.dumps({'code': code}))
return r.json()
Here is the C code:
#include <Python.h>
int main(void){
PyObject *myModuleString, *myModule, *myFunction, *myResult;
Py_Initialize();
myModuleString = PyUnicode_FromString((char*)"verify");
myModule = PyImport_Import(myModuleString);
myFunction = PyObject_GetAttrString(myModule,(char*)"verify");
myResult = PyObject_CallObject(myFunction, NULL);
const char* s = PyUnicode_AsUTF8(myResult);
printf("REPR: %s\n", s);
Py_Finalize();
return 0;
}
Create the object file works fine:
$ gcc -c `python3.7-config --cflags --ldflags` source_code.c
$
But it does not work at the end:
$ gcc source_code.o -o source_code.bin
/usr/bin/ld: source_code.o: relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: final link failed: nonrepresentable section on output
collect2: error: ld returned 1 exit status
The -fPIE argument also does not help:
$ gcc -c `python3.7-config --cflags --ldflags` -fPIE source_code.c
$
$ gcc source_code.o -o source_code.bin
/usr/bin/ld: source_code.o: in function `main':
/home/user/source_code.c:7: undefined reference to `Py_Initialize'
/usr/bin/ld: /home/user/source_code.c:9: undefined reference to `PyUnicode_FromString'
/usr/bin/ld: /home/user/source_code.c:10: undefined reference to `PyImport_Import'
/usr/bin/ld: /home/user/source_code.c:12: undefined reference to `PyObject_GetAttrString'
/usr/bin/ld: /home/user/source_code.c:13: undefined reference to `PyObject_CallObject'
/usr/bin/ld: /home/user/source_code.c:15: undefined reference to `PyUnicode_AsUTF8'
/usr/bin/ld: /home/user/source_code.c:18: undefined reference to `Py_Finalize'
collect2: error: ld returned 1 exit status
How can I define the references to generate the binary file?
I had a similar error. Adding the -no-pie argument to the compile statement worked for me.
You could try this:
gcc -c `python3.7-config --cflags --ldflags` source_code.c -no-pie

Python and C++ integration.Problems with dynamic library

I use Swig.(Mac os 10.13)
My shell script:
swig -c++ -python -o example_wrap.cpp example.i
g++ -c -std=c++17 -fPIC example.cpp
g++ -c -std=c++17 -fPIC example_wrap.cpp -o example_wrap.o \
-I/usr/local/Cellar//python/3.7.2_2/Frameworks/Python.framework/Versions/3.7/include/python3.7m
ld -bundle -macosx_version_min 10.13 -flat_namespace \
-undefined suppress -o _example.so *.o
I spent enough time to seek how to create C++ dynamic library for Python, but I have never used the last line.Most often I create a library from an IDE.
g++ -shared is more familiar, but it doesn't work.
Many such errors appear:
Undefined symbols for architecture x86_64:
"_PyArg_ParseTuple", referenced from:
_wrap_printTree(_object*, _object*) in example_wrap.o
I know about this methods from Python.h.
So, the questions are - how does the last line work(ld -bundle ...)? Are there other methods to create the dynamic library?How can I use g++ -shared?
Here is a small CMakeLists.txt that should work for the example you posted:
cmake_minimum_required(VERSION 3.10) # change this to your needs
project(foo VERSION 0.0 LANGUAGES CXX C)
find_package(SWIG REQUIRED)
include(${SWIG_USE_FILE})
find_package(PythonLibs)
include_directories(${PYTHON_INCLUDE_PATH})
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
set(CMAKE_SWIG_FLAGS "")
add_library(exampleImpl SHARED example.h example.cpp)
target_compile_features(exampleImpl PUBLIC cxx_std_17)
set_source_files_properties(example.i PROPERTIES CPLUSPLUS ON)
swig_add_library(example LANGUAGE python SOURCES example.i)
swig_link_libraries(example ${PYTHON_LIBRARIES} exampleImpl)
To make sure cmake uses the right python library, you can pass an appropriate option upon configure time, see here.

Issue with GRIB API, Jasper, Pygrib

During the recent days i had problem with GRIB API installation, i was able to install it with disable the --disable-shared flag but the problem is then i got an error while running Pygrib test "python animate.py"
(GRIB_API ERROR : JPEG support not enabled.)
So i had to install the GRIB API again without disable the shared but i got this error:
Linking C shared library ../lib/libgrib_api.so
/usr/bin/ld: /usr/local/lib/libjasper.a(jpc_math.o): relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC
/usr/local/lib/libjasper.a: could not read symbols: Bad value
collect2: error: ld returned 1 exit status
make[2]: *** [lib/libgrib_api.so] Error 1
make[1]: *** [src/CMakeFiles/grib_api.dir/all] Error 2
make: *** [all] Error 2
then i started to compile Jasper again with -fPIC flag but i still got the
"recompile with -fPIC" for that i changed the flags in MAKE file as:
CFLAGS = -fPIC
CPP = gcc -E
CPPFLAGS =
CXX = g++
CXXCPP = g++ -E
CXXDEPMODE = depmode=gcc3
CXXFLAGS = -fPIC
FFLAGS = -fPIC
still no resualt ... i searched many but no resualt
Thanks in advance

Categories

Resources