![](/img/trans.png)
[英]How do I "Correctly" include a Python.h header in my C++ program in VSCODE?
[英]How do I compile a C program which includes Python.h on Windows?
我正在嘗試學習如何將 Python 解釋器嵌入到我的 C/C++ 程序中。
我喜歡這個概念,因為它可以讓我在運行時擴展 C 程序,並使用戶能夠編寫自定義腳本/插件。
Detailed instruction on embedding Python in C is provided at: https://docs.python.org/3/extending/embedding.html
我正在使用 Python 文檔中提供的示例代碼來找出所涉及的機制:
embedded.c
#include <Python.h>
int
main(int argc, char *argv[])
{
Py_SetProgramName(argv[0]); /* optional but recommended */
Py_Initialize();
PyRun_SimpleString("from time import time,ctime\n"
"print 'Today is',ctime(time())\n");
Py_Finalize();
return 0;
}
問題是我不知道如何使用 gcc 在 Windows 上編譯它。
C:\Users\user\embedded_python> gcc embedded.c
embedded.c:2:10: fatal error: Python.h: No such file or directory
#include <Python.h>
^~~~~~~~~~
compilation terminated.
我認為問題是我需要鏈接到 Python.h 文件,但我似乎無法正確獲取編譯器標志:
C:\Users\user\Desktop\embedded_python>gcc -IC:\Users\user\AppData\Local\Programs\Python\Python38 -lPython38 embedded.c
embedded.c:2:10: fatal error: Python.h: No such file or directory
#include <Python.h>
^~~~~~~~~~
compilation terminated.
我怎樣才能讓這個程序在 Windows 上編譯(最好使用 gcc/g++ 以避免 Visual Studio 膨脹)?
在 Linux 中,但我想如果你從 windows 激活你的Bash
並可以解決問題。 首先你必須安裝python3-dev
然后使用您的文件所在的命令行進行編譯(測試是您的文件名)
gcc -Wall test.c -o test $(pkg-config --cflags --libs python3)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.