Creating a standalone application with Cython + MinGW

I am trying to create a standalone application from Python code. At the moment, this is just a hello world program. I will compile it with Cython to get the .c file:

"c: \ python34 \ scripts \ cython.exe" --embed hello.py

It works great. Then I try to compile and link the generated .c file as follows:

"c: \ mingw32 \ bin \ gcc.exe" -I "c: \ python34 \ include" -L "c: \ python34 \ libs" -lpython34 -ohello.exe hello.c

This gives me a lot of errors in the links:

... \ cc7PmSei.o: hello.c :(. text + 0xe9): undefined link to `_imp__PyTuple_New '

... \ cc7PmSei.o: hello.c :(. text + 0x130): undefined reference to `_imp__PyBytes_FromStringAndSize '

... \ cc7PmSei.o: hello.c :(. text + 0x177): undefined reference to `_imp__PyModule_Create2 '

...

... \ cc7PmSei.o: hello.c :(. text + 0x12b7): undefined reference to `_imp__PyUnicode_Decode '

... \ cc7PmSei.o: hello.c :(. text + 0x12dd): undefined reference to `_imp__PyUnicode_FromStringAndSize '

... \ cc7PmSei.o: hello.c :(. text + 0x1303): undefined reference to `_imp__PyBytes_FromStringAndSize '

... / libmingw32.a (main.o): main.c: .text.startup + 0xa7): undefined reference to `WinMain @ 16 '

collect2.exe: error: ld returned 1 exit status

Additional Information: I have a 64-bit Windows 7 Home OS. I am using 32-bit 32-bit, Cython-0.20.1 and TDM-GCC 4-bit Python 3.4.1.

I did some research. Some people say that this can be caused, for example, using a 32-bit C compiler and 64-bit Python. But this is not the case here. Others ( http://eli.thegreenplace.net/2008/06/28/compiling-python-extensions-with-distutils-and-mingw/ ) say I need to create libpython34.a. But my version of Python has already appeared with this file.

Does anyone have an idea what I'm doing wrong? Thanks in advance.

+6
source share
3 answers

Ok I understood. There are two problems here:

Firstly, the undefined reference to “WinMain @ 16” is due to the fact that Cython generates “wmain” instead of “main” for Python 3. MinGW has a “-municode” command line option to support 'wmain', but it looks like it is implemented only in the latest 64-bit versions of MinGW. It is not implemented in MinGW 4.8.1 32-bit, which I installed. An alternative is to use the "main" wrapper function in C or just use Python 2.7. I chose the latter.

The remaining undefined links are in the wrong order of parameters. I can simply create the application with the following command:

"c: \ mingw \ bin \ gcc.exe" -I "c: \ python27 \ include" -L "c: \ python27 \ libs" hello.c -ohello.exe -lpython27

+2
source

In hello.c find:

#if PY_MAJOR_VERSION < 3 int main(int argc, char** argv) { #elif defined(WIN32) || defined(MS_WINDOWS) int wmain(int argc, wchar_t **argv) 

and replace wmain in the main. It worked for me.

+2
source

On mingw-w64 (32-bit from win-builds.org) gcc-4.8.1 has the -municode option and it will use wmain () as an entry point, the UTF-16 command line will also work. It is a pity that he is not mentioned anywhere in official documents. Find him on some irc magazine after three days of experimenting :)

0
source

Source: https://habr.com/ru/post/970832/


All Articles