1

(Linked to Cython without Console on Windows but this one is for Python 2.7 and mingw, the solutions don't work directly here)

This allows to create a .exe with Cython:

cython test.py --embed
call vcvarsall.bat x64
cl test.c /I C:\Python38\include /link C:\Python38\libs\python38.lib

It works, and then test.exe starts the program in a terminal console window.

How to compile with cython --embed it without a console window?

I tried

cl test.c /I C:\Python38\include /link C:\Python38\libs\python38.lib /subsystem:window

but it fails with:

LIBCMT.lib(exe_winmain.obj) : error LNK2019: unresolved external symbol WinMain referenced in function "int __cdecl __scrt_common_main_seh(void)" (?__scrt_common_main_seh@@YAHXZ)

Basj
  • 41,386
  • 99
  • 383
  • 673
  • you probably need to add `/ENTRY:mainCRTStartup` but I cannot test it now (see also https://stackoverflow.com/questions/22934206/what-is-the-difference-between-main-and-maincrtstartup) – ead Jul 26 '22 at 09:30
  • Thanks @ead. I tried `cl test.c /I C:\Python38\include /link C:\Python38\libs\python38.lib /ENTRY:mainCRTStartup /subsystem:windows` but I still have `LIBCMT.lib(exe_main.obj) : error LNK2019: unresolved external symbol main referenced in function "int __cdecl __scrt_common_main_seh(void)" (?__scrt_common_main_seh@@YAHXZ)`. – Basj Jul 26 '22 at 09:32

1 Answers1

1

Cython produces usually wmain instead of main so you could use widechar as command line argument when calling the resulting executable.

Thus you need to tell the linker to look for wmain this is done via option:

/ENTRY:wmainCRTStartup

which differently as mainCRTStartup will look for wmain.

It is important to use /ENTRY:wmainCRTStartup and not just /ENTRY:wmain because otherwise the code, which should run before wmain is called (e.g. initialization of static variables) won't run.

The full solution is then:

cl test.c /I C:\Python38\include /link C:\Python38\libs\python38.lib /subsystem:windows /ENTRY:wmainCRTStartup
Basj
  • 41,386
  • 99
  • 383
  • 673
ead
  • 32,758
  • 6
  • 90
  • 153