Python - Py_Initialize unresolved during compilation

2024/9/19 9:32:40

I have statically compiled Python2.7 without any error. To test my build, I use the following snippet:

#include "Python.h"
int main()
{Py_Initialize();
}

And I am compiling it like this:

$ gcc -static -I/path/to/python/header -L/path/to/my/staticpythonlib \ -lpython2.7 -ldl -l_all_other_needed_lib /tmp/my_previous_snippet.c -o myouput

However, an error occured. gcc claims the famous undefined reference.

test.c:(.text+0x1): Undefined reference to 'Py_Initialize'

Curiously I used gcc with the verbosity flag (I won't paste the result here) and the compiler says, it's using my libpython, but couldn't find the reference. So I listed the symbols of my static python2.7 library :

$ nm /path/to/pythonlib |grep Py_Initialize
frozenmain.o           U Py_Initialize
pythonrun.o  0000009e9 T Py_Initialize
pythonrun.o  000000052 T Py_Initialize_Ex
main.o                 U Py_Initialize

We can see, that Py_Initialize is correctly referenced in pythonrun.o. However i don't know how the compiler chose the correct object file.

My questions are :

  1. How can I be sure, that gcc uses the correct object file in my .a lib?
  2. Is there anything wrong on my compilation options?

Thanks for your help.

Answer

The order matters! More specifically, the order in the arguments for gcc matters. More specifically, if a bar object uses a function bluh from library bleh, then the order -lbleh bar.o is problematic because it provides no reason to gcc look for the function bluh in bleh. On the other hand, when you use bar.o -lbleh then gcc knows you are referring to bluh and when it gets to handle -lbleh it tries to resolve the dependence. This is quickly mentioned at http://gcc.gnu.org/onlinedocs/gcc/Link-Options.html. As a rule, always specifies libraries after your objects.

To reproduce your problem, create a file a1.c as in:

#include "Python.h"int main()
{Py_Initialize();return 0;
}

Now compile with gcc -static -I/usr/include/python2.7 -L/usr/lib/python2.7/config/ -lpython2.7 -ldl -lm -lutil -lz -pthread -o a1 a1.c. This gives the undefined reference to Py_Initialize. Of course you have to change the paths to match your installation.

Now, instead, compile with gcc -static -I/usr/include/python2.7 -L/usr/lib/python2.7/config/ -o a1 a1.c -lpython2.7 -ldl -lm -lutil -lz -pthread and it works (ignoring the possible many warnings, which is a different matter).

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

Related Q&A

Python download large csv file from a url line by line for only 10 entries

I have a large csv file of the client and shared via a url to download and I want to download it line by line or by bytes and I want to limit only for 10 entries.I have the following code which will do…

Flask-Login still logged in after use logouts when using remember_me

To logout a user in flask using Flask-login, i simply call logout_user(), but after adding some additional checks with session, after I click logout and click back to "login page" again, im s…

How to write integers to a file

I need to write ranks[a], ranks[b], countto a file, each time on a new lineI am using:file = open("matrix.txt", "w") for (a, b), count in counts.iteritems():file.write(ranks[a], ran…

seaborn changing xticks from float to int

I am plotting a graph with seaborn as sns and pylab as plt:plt.figure(figsize=(10,10),) sns.barplot(y = whatever_y, x = whatever_x , data=mydata) plt.xticks(fontsize=14, fontweight=bold)The xticks are …

What are the use cases for a Python distribution?

Im developing a distribution for the Python package Im writing so I can post it on PyPI. Its my first time working with distutils, setuptools, distribute, pip, setup.py and all that and Im struggling a…

Recovering a file deleted with python

So, I deleted a file using python. I cant find it in my recycling bin. Is there a way I can undo it or something. Thanks in advance.EDIT: I used os.remove. I have tried Recuva, but it doesnt seem to fi…

Using Py_buffer and PyMemoryView_FromBuffer with different itemsizes

This question is related to a previous question I asked. Namely this one if anyone is interested. Basically, what I want to do is to expose a C array to Python using a Py_buffer wrapped in a memoryview…

selenium remotewebdriver with python - performance logging?

Im trying to get back some performance log info from a remote webdriver instance. Im using the Python Selenium bindings.From what I can see, this is information I should be able to get back. Think it m…

Python - replace unicode emojis with ASCII characters

I have an issue with one of my current weekend projects. I am writing a Python script that fetches some data from different sources and then spits everything out to an esc-pos printer. As you might ima…

How do I get my python object back from a QVariant in PyQt4?

I am creating a subclass of QAbstractItemModel to be displayed in an QTreeView.My index() and parent() function creates the QModelIndex using the QAbstractItemModel inherited function createIndex and p…