Undefined reference to `PyString_FromString

2024/10/1 17:29:24

I have this C code:

... [SNIP] ...
for(Node = Plugin.Head; Node != NULL; Node = Node->Next) {//Create new python sub-interpreterNode->Interpreter = Py_NewInterpreter();if(Node->Interpreter == NULL) {Die("Py_NewInterpreter() failed");}//Create path to plugins main source filesnprintf(Filename, FILENAME_MAX, "%s/main.py", Node->File);//Convert filename to python stringPFilename = PyString_FromString(Filename);if(PFilename == NULL) {Die("PyString_FromString(%s) failed", Filename);}//Import plugin main source filePModule = PyImport_Import(PFilename);if(PModule == NULL) {Die("PyImport_Import(%s) failed", Filename);}//Deallocate filenamePy_DECREF(PFilename);//Get reference to onLoad function from modulePFunction = PyObject_GetAttrString(PModule, "onLoad");if(PFunction == NULL) {Die("PyObject_GetAttrString() failed");}
}
... [SNIP] ...

Which gives this error when compiled:

/tmp/ccXNmyPy.o: In function `LoadPlugins':
/home/alex/Code/Scribe/Scribe.c:693: undefined reference to `PyString_FromString'
collect2: error: ld returned 1 exit status

Python.h is included at the top of the source file.

I'm compiling with:

gcc -funwind-tables -rdynamic -I /usr/include/python2.7/ -g -o Scribe Scribe.c -lcurses `python-config --cflags` `python-config --ldflags` -Wall

I'm basing the code on the Python C-Api docs, from here:

http://docs.python.org/2/c-api/

Specifically:

http://docs.python.org/2/c-api/string.html?highlight=pystring_fromstring#PyString_FromString

I have no idea why this is happening, halp? =c

Answer

Solved it, thanks to some help from martineau.

Turns out the python-config --cflags and python-config --ldflags lines generated flags that included the python3.3 include directory in the search path and linked the python3.3 lib.

Naturally python3.3 doesn't work so well with python2.7 C-API, which is what caused this problem.

My solution was to copy the output of python-config --cflags and python-config --ldflags and edit it so it included python2.7 instead of python3.3m:

-I/usr/include/python2.7 -I/usr/include/python2.7 -Wno-unused-result -DDYNAMIC_ANNOTATIONS_ENABLED=1 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -march=x86-64 -mtune=generic -O2 -pipe -fstack-protector --param=ssp-buffer-size=4 -D_FORTIFY_SOURCE=2 -march=x86-64 -mtune=generic -O2 -pipe -fstack-protector --param=ssp-buffer-size=4 -D_FORTIFY_SOURCE=2-lpthread -ldl -lutil -lm -lpython2.7 -Xlinker -export-dynamic

Instead of:

-I/usr/include/python3.3m -I/usr/include/python3.3m -Wno-unused-result -DDYNAMIC_ANNOTATIONS_ENABLED=1 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -march=x86-64 -mtune=generic -O2 -pipe -fstack-protector --param=ssp-buffer-size=4 -D_FORTIFY_SOURCE=2 -march=x86-64 -mtune=generic -O2 -pipe -fstack-protector --param=ssp-buffer-size=4 -D_FORTIFY_SOURCE=2-lpthread -ldl -lutil -lm -lpython3.3m -Xlinker -export-dynamic
https://en.xdnf.cn/q/70939.html

Related Q&A

How to call a method from a different blueprint in Flask?

I have an application with multiple blueprinted modules.I would like to call a method (a route) that would normally return a view or render a template, from within a different blueprints route.How can …

Dynamically define functions with varying signature

What I want to accomplish:dct = {foo:0, bar:1, baz:2} def func(**dct):pass #function signature is now func(foo=0, bar=1, baz=2)However, the ** syntax is obviously clashing here between expanding a dict…

python pandas plot with uneven timeseries index (with count evenly distributed)

My dataframe has uneven time index.how could I find a way to plot the data, and local the index automatically? I searched here, and I know I can plot something like e.plot()but the time index (x axis)…

python OpenAI gym monitor creates json files in the recording directory

I am implementing value iteration on the gym CartPole-v0 environment and would like to record the video of the agents actions in a video file. I have been trying to implement this using the Monitor wra…

Install xgboost under python with 32-bit msys failing

Trying to install xgboost is failing..? The version is Anaconda 2.1.0 (64-bit) on Windows & enterprise. How do I proceed? I have been using R it seems its quite easy to install new package in R …

Pythons _winapi module

I was trying to write some python code that requires calls to native WINAPI functions. At first I came across the pypiwin32 package. Then, somewhere on the internet I saw someone using the _winapi modu…

list comprehension with numpy arrays - bad practice?

I am wondering if the below approach would be considered bad practice, and if so, if someone could give some guidance towards another approach. Here is the code in question:a = np.array([[1,2,3],[4,5,6…

pandas: write dataframe to excel file *object* (not file)?

I have a dataframe that I want to convert to excel file, and return it using HTTP. Dataframes to_excel method accepts either a path, or an ExcelWriter, which, in turn, refers to a path.Is there any way…

win32: moving mouse with SetCursorPos vs. mouse_event

Is there any difference between moving the mouse in windows using the following two techniques?win32api.SetCursorPos((x,y))vs:nx = x*65535/win32api.GetSystemMetrics(0) ny = y*65535/win32api.GetSystemM…

Pandas: Unstacking One Column of a DataFrame

I want to unstack one column in my Pandas DataFrame. The DataFrame is indexed by the Date and I want to unstack the Country column so each Country is its own column. The current pandas DF looks like t…