看板 Python 關於我們 聯絡資訊
感謝你的熱心回應 不過我照著做, compile時出現 error LNK2019: 無法解析的外部符號 __Py_Dealloc 在函式 _main 中被參考 error LNK2019: 無法解析的外部符號 __Py_NegativeRefcount 在函式 _main 中被參考 error LNK2001: 無法解析的外部符號 __Py_RefTotal 請問該怎麼處理呢 感謝 ※ 引述《LiloHuang (十年一刻)》之銘言: : 步驟一:建立一個 Console-based 的 VC 專案 : 主程式內容大概如下,不保證完全符合需求或者可以運作 : 請依照自己的需求進行修改... : #include <Windows.h> : #include <Python.h> : #include <marshal.h> : #include "resource.h" : int main() { : HRSRC res = FindResource(NULL, MAKEINTRESOURCE(IDR_PYTHON1), TEXT("PYTHON")); : if (!res) return -1; : HGLOBAL handle = LoadResource(NULL, res); : if (!handle) return -1; : char *ptr = (char *)LockResource(handle); : size_t size = SizeofResource(NULL, res); : if (!ptr || size < 8) return -1; : Py_Initialize(); : PyObject *code_object = NULL; : PyObject *module = NULL; : do { : code_object = PyMarshal_ReadObjectFromString(ptr + 8, size - 8); : if (!code_object) break; : module = PyImport_ExecCodeModule("test.pyc", code_object); : if (!module) break; : if (PyObject_HasAttrString(module, "foobar")) { : printf("great!\n"); : } : } while (0); : Py_XDECREF(module); : Py_XDECREF(code_object); : Py_Finalize(); : return 0; : } : 步驟二:產生 .pyc 檔案 : 編譯 .py 成 .pyc 的方式是 python -m py_compile test.py : 步驟三:加入 .pyc 資源檔 : 透過 VC 資源檔建立新的自訂資源項目,假設命名為 PYTHON : 加入資源的方式有點類似此教學,http://goo.gl/yHFN0R : 但是請務必透過 [自訂] 按鈕,而不是選擇已知的資源類型 : 再把稍早編譯好的 test.pyc 擺到 Resource.rc 同一層目錄後 : 用記事本打開 Resource.rc 檔案,把新增的自訂資源項目改成 : IDR_PYTHON1 PYTHON "test.pyc" : 完成以上動作就可以了,有問題請推文我會盡量回應。 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 1.169.175.179 ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1424000307.A.B4C.html
LiloHuang: 你的 Python 應該不是自編譯的,因此你不應該使用 02/15 19:41
LiloHuang: Debug 模式來編譯你的專案。請改用 Release 模式就可以 02/15 19:42
LiloHuang: 如果你有把 python27.lib 複製成 python27_d.lib 02/15 19:43
LiloHuang: 也請不要這麼做,這樣就會造成你現在的問題。 02/15 19:43