簡體   English   中英

可以在沒有 Visual Studio 的情況下使用 MinGw 編譯 python.exe

[英]possible to compile python.exe without Visual Studio, with MinGw

我正在嘗試實現此頁面上列出的內容: https : //blogs.msdn.microsoft.com/pythonengineering/2016/04/26/cpython-embeddable-zip-file/

我試圖編譯的代碼就是這樣的:

#include "Python.h"

int
wmain(int argc, wchar_t **argv)
{
    return Py_Main(argc, argv);
}

在 VisualStudio 15 中,我必須添加 python/include 並鏈接到項目中的 python libs 目錄,並添加:

#include "stdafx.h"

然后它編譯並正常工作。 我只是好奇,是否可以使用 mingw 或其他開源 C/C++ 編譯器來做到這一點?

如果我放置一個包含以下內容的文件my_python.cpp

#include "include/Python.h"

int
wmain(int argc, wchar_t **argv)
{
    return Py_Main(argc, argv);
}

在新的 32 位 python 3.5 安裝(windows 7 x64)的根目錄中,cd 到該目錄並嘗試運行:

gcc my_python.cpp -Llibs -lpython35 -o my_python.exe我收到這個錯誤:

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

有什么辦法可以解決這個問題,讓它在沒有 Visual Studio 的情況下運行?

該錯誤與 Python 無關。 wmain是特定於 Visual Studio 的。 GCC 不會將wmain視為入口點,它只是作為一個永遠不會被調用的函數坐在那里。

GCC 需要mainWinMain作為入口點。 如果沒有找到這兩個入口點,編譯器就會抱怨。 所以讓我們只使用main作為入口點。

Py_Main大概需要寬字符串輸入。 CommandLineToArgvW將始終提供。 例子:

#include <Windows.h>
#include "include/Python.h"

int main()
{
    int argc;
    wchar_t** argv = CommandLineToArgvW( GetCommandLineW(), &argc );
    return Py_Main(argc, argv);
}

如果仍然出現同樣的錯誤,只需提供WinMain入口點即可

#include <Windows.h>
#include "include/Python.h"

int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
    int argc;
    wchar_t** argv = CommandLineToArgvW( GetCommandLineW(), &argc );
    return Py_Main(argc, argv);
}

另請注意, *.lib文件通常用於 Visual Studio。 GCC 版本需要帶有*.a擴展名的庫名稱。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM