![](/img/trans.png)
[英]How do I get Visual Express 2010 to find my python.h header file?
[英]How do I "Correctly" include a Python.h header in my C++ program in VSCODE?
我的環境:
1- MacOs 卡塔琳娜。
2- VScode“Visual Studio代碼”
3- PlatformIO extension (for Arduino Development in C/C++) mainly C++
4- Python3 和 Python2 都安裝在我的 mac 上。
問題:
我試圖將一個名為“sr1.py”的 python 應用程序嵌入到我的 C++ 應用程序中,但是當我將鼠標懸停在#inclde <python.h>
波浪紅線上時出現此錯誤:
#include 檢測到錯誤。 請更新您的 includePath。 此翻譯單元 (/Users/ImCodeName7/Documents/PlatformIO/Projects/speech_recognision/src/main.cpp) 禁用波浪線。C/C++(1696) 無法打開源文件“python.h”C/C++(1696)
代碼:
#include <Arduino.h>
#include <python.h>
int main()
{
char filename[] = "sr1.py";
FILE* fp;
Py_Initialize();
fp = _Py_fopen(filename, "r");
PyRun_SimpleFile(fp, filename);
Py_Finalize();
return 0;
}
請注意,我仍在學習如何配置我的環境,所以請多多包涵。 謝謝!
Edit1:源文件主要寫在 C++ (或者實際上稱為 Arduino C!)
假設這只是c_cpp_properties.json
文件中未包含正確路徑的結果。
如果您知道python
所在的路徑,您需要做的就是將以下內容添加到您的c_cpp_properties.json文件中:
{
"configurations": [
{
"name": "FOO",
"includePath": [
"${workspaceFolder}/**",
"/PATH/TO/DIR/CONTAINING-Python.h",
// --- E.g. ---
"/usr/include/python3.9/Python.h",
// Or
"/usr/include/python3.9"
],
}
],
}
如果你不這樣做,那么你必須做一些挖掘。 對於其他 *NIX 系統,過程應該類似,對於 ubuntu,請參閱這篇文章。
Python.h
應該位於:
../foo/<ENV_NAME>/include/pythonX.Y
所以你會改變
"/PATH/TO/DIR/CONTAINING/Python.h"
到"../foo/<ENV_NAME>/include/pythonX.Y"
要查找文件:
找到 python 可執行文件所在的文件夾。
which python
(或哪個 python2/python3)這應該產生一個看起來像這樣的文件路徑:
../foo/<ENV_NAME>/bin/python
將您當前的工作目錄更改為
<ENV_NAME>
cd../foo/<ENV_NAME>
在
<ENV_NAME>
中應該有一個include
目錄,在include
目錄中應該有另一個名為pythonX.Y
的目錄。 這是找到Python.h
的目錄。
例如
ENV_NAME/
├─ bin/
├─ include/
│ ├─ python3.9/
│ │ ├─ Python.h
│ │ ├─ ...
├─ lib/
├─ share/
├─ var/
├─ .../
在 Mac 上,沒有 conda 與自制軟件
有點棘手,需要更多挖掘。 就我而言,我發現它位於
/opt/homebrew/Cellar/python@3.9/3.9.9/Frameworks/Python.framework/Versions/3.9/include/python3.9
對於 windows(沒有任何 package 經理),默認位置是:
C:\Users\Foo\AppData\Local\Programs\Python\PythonXY\include
但是,如果它不存在,它應該位於以下位置之一:
%ProgramFiles%\Python XY
%ProgramFiles(x86)%\Python XY
%LocalAppData%\Programs\Python\PythonXY
%LocalAppData%\Programs\Python\PythonXY-32
%LocalAppData%\Programs\Python\PythonXY-64
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.