possible to compile python.exe without Visual Studio, with MinGw
<p>I am trying to achieve things laid out on this page:
<a href="https://blogs.msdn.microsoft.com/pythonengineering/2016/04/26/cpython-embeddable-zip-file/" rel="nofollow">https://blogs.msdn.microsoft.com/pythonengineering/2016/04/26/cpython-embeddable-zip-file/</a></p>

<p>The code I am trying to compile is just this:</p>

<pre><code>#include "Python.h"

int
wmain(int argc, wchar_t **argv)
{
    return Py_Main(argc, argv);
}
</code></pre>

<p>In VisualStudio 15 I have to add the python/include and link to the python libs directories in the project and also add:</p>

<pre><code>#include "stdafx.h"
</code></pre>

<p>and then it compiles and works fine.  I'm just curious, is it possible to do this with mingw, or another open source C/C++ compiler?</p>

<p>If I place a file <code>my_python.cpp</code> which contains the following: </p>

<pre><code>#include "include/Python.h"

int
wmain(int argc, wchar_t **argv)
{
    return Py_Main(argc, argv);
}
</code></pre>

<p>in the root directory of a fresh 32-bit python 3.5 install (windows 7 x64), cd to that directory and try to run:</p>

<p><code>gcc my_python.cpp -Llibs -lpython35 -o my_python.exe</code>
I get this error:</p>

<pre><code>c:/mingw/bin/../lib/gcc/mingw32/5.3.0/../../../libmingw32.a(main.o):(.text.startup+0xa0): undefined reference to `WinMain@16'
</code></pre>

<p>Any way to fix this, get it running without Visual Studio?</p>

<p>The error is unrelated to Python. <code>wmain</code> is Visual Studio specific. GCC does not treat <code>wmain</code> as entry point, it just sits there as a function which never gets called.</p>

<p>GCC requires <code>main</code> or <code>WinMain</code> as entry point. If neither of those entry points is found, then the compiler will complain. So let's just use <code>main</code> as entry point.</p>

<p><code>Py_Main</code> presumably expects wide character string input. <code>CommandLineToArgvW</code> will always provide that. Example:</p>

<pre><code>#include &lt;Windows.h&gt;
#include "include/Python.h"

int main()
{
    int argc;
    wchar_t** argv = CommandLineToArgvW( GetCommandLineW(), &amp;argc );
    return Py_Main(argc, argv);
}
</code></pre>

<p>If you still get the same error, just provide <code>WinMain</code> entry point to make it happy</p>

<pre><code>#include &lt;Windows.h&gt;
#include "include/Python.h"

int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
    int argc;
    wchar_t** argv = CommandLineToArgvW( GetCommandLineW(), &amp;argc );
    return Py_Main(argc, argv);
}
</code></pre>

<p>Also note, <code>*.lib</code> files are usually for Visual Studio. GCC version expects library names with <code>*.a</code> extension.</p>

python c++ c visual-studio mingw