詳解如何在VS2019和VScode中配置C++調(diào)用python接口
why
看到這個標(biāo)題有些人說了,為什么好好的C++你非要調(diào)用python?人家明明是兩種語言呀!
但是在實際應(yīng)用中,有時候會用到C/C++調(diào)用python來更簡單地去完成一些功能,不然人家python為什么有一個文件夾叫include,里邊全是.h文件呢?
VScode中配置環(huán)境
首先是在VScode中為C++調(diào)用python接口配置環(huán)境,這里假設(shè)你已經(jīng)配置好了c++編程環(huán)境!
1、配置step1
用快捷鍵Ctrl+Shift+X打開Extensions 商店,輸入python,install:

2、配置step2
用快捷鍵Ctrl+Shift+P打開命令面板,打開C/C++:編輯配置(UI):

然后編輯c_cpp_properties.json文件,在文件中的includePath項添加自己的python include路徑:“D:\\Python\\Python37\\include”,注意格式。
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"D:\\Python\\Python37\\include"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "D:\\MinGW\\bin\\gcc.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}
3、測試
測試一下:編輯hello.py:
def printHello():
print("Hello World")
c++調(diào)用python有兩種方式,我在代碼中都進(jìn)行了測試,編輯main.cpp:
#include "D:\Python\Python37\include\Python.h"
//#include <Python.h>
#include <iostream>
using namespace std;
void cython1()
{
Py_Initialize(); //初始化python解釋器,告訴編譯器要用的python編譯器
PyRun_SimpleString("import hello"); //調(diào)用python文件
PyRun_SimpleString("hello.printHello()"); //調(diào)用上述文件中的函數(shù)
Py_Finalize(); //結(jié)束python解釋器,釋放資源
}
void cython2()
{
Py_Initialize();
PyObject *pModule = NULL;
PyObject *pFunc = NULL;
pModule = PyImport_ImportModule("hello"); //這里是要調(diào)用的文件名
pFunc = PyObject_GetAttrString(pModule, "printHello"); //這里是要調(diào)用的函數(shù)名
PyEval_CallObject(pFunc, NULL); //調(diào)用函數(shù)
Py_Finalize();
}
int main()
{
int select;
cin >> select;
select == 1 ? cython1():cython2();
return 0;
}
4、問題匯總(重磅)
1、為什么我包含了Python.h,c++代碼中調(diào)用也沒有報錯,但是運(yùn)行時會出現(xiàn)如下錯誤?(暫未從根本上解決問題,仿照后面linux上的頭文件調(diào)用方法也是無效,稍后解決會更新)
main.cpp:2:10: fatal error: Python.h: No such file or directory #include "Python.h" ^~~~~~~~~~ compilation terminated.
說明沒有include成功啊,這里我猜測是由于版本問題導(dǎo)致的,暫未得到解決,但是有一種方式是絕對可以使用的,并且已經(jīng)在代碼中體現(xiàn)了,那就是…直接inlcude絕對路徑!
#include "D:\Python\Python37\include\Python.h"
可能心細(xì)的朋友已經(jīng)發(fā)現(xiàn)了,你只是包含了頭文件,鏈接庫還沒有鏈接啊!但是我當(dāng)時的心情不允許我這么細(xì)心啊,咳咳
這里打個廣告!強(qiáng)烈建議各位C/C++ develpoers,去仔細(xì)讀一下程序員的自我修養(yǎng)這本書,真的受益匪淺。
2、由于沒有鏈接靜態(tài)庫而報錯(同上,暫時只有傻瓜式解決辦法,使用makefile解決此問題請看我之后的博客:在makefile中鏈接靜態(tài)庫)
C:\Users\11955\AppData\Local\Temp\ccLmGIL8.o:main.cpp:(.text+0xb): undefined reference to `__imp_Py_Initialize'
C:\Users\11955\AppData\Local\Temp\ccLmGIL8.o:main.cpp:(.text+0x20): undefined reference to `__imp_PyRun_SimpleStringFlags'
C:\Users\11955\AppData\Local\Temp\ccLmGIL8.o:main.cpp:(.text+0x35): undefined reference to `__imp_PyRun_SimpleStringFlags'
C:\Users\11955\AppData\Local\Temp\ccLmGIL8.o:main.cpp:(.text+0x3e): undefined reference to `__imp_Py_Finalize'
C:\Users\11955\AppData\Local\Temp\ccLmGIL8.o:main.cpp:(.text+0x56): undefined reference to `__imp_Py_Initialize'
C:\Users\11955\AppData\Local\Temp\ccLmGIL8.o:main.cpp:(.text+0x76): undefined reference to `__imp_PyImport_ImportModule'
C:\Users\11955\AppData\Local\Temp\ccLmGIL8.o:main.cpp:(.text+0x91): undefined reference to `__imp_PyObject_GetAttrString'
C:\Users\11955\AppData\Local\Temp\ccLmGIL8.o:main.cpp:(.text+0xb0): undefined reference to `__imp_PyEval_CallObjectWithKeywords'
C:\Users\11955\AppData\Local\Temp\ccLmGIL8.o:main.cpp:(.text+0xb9): undefined reference to `__imp_Py_Finalize'
解決辦法:鏈接靜態(tài)庫,我把D:\Python\Python37\libs這個文件夾復(fù)制到了工作空間的文件夾下,為了方便鏈接直鏈接該文件夾下所有文件。
g++ -o main .\main.cpp -L .\libs\* .\main.exe

VS2019中配置環(huán)境
1、step1
首先明確,如果你安裝的python是64位的,建議把VS 該項目下的解決方案平臺改為x64,并且環(huán)境是在特定的解決方案配置下(Debug/Release)進(jìn)行的,所以不要配置在Debug,卻在Release調(diào)用Python.h,這樣肯定會報錯:無法打開Python.h。
2、step2
右鍵project打開屬性,在配置屬性 - C/C++ - 常規(guī) - 附加包含目錄下添加:
D:\Python\Python37\include
在配置屬性 - VC++目錄 - 庫目錄下添加:
D:\Python\Python37\libs
3、測試
C++代碼和python代碼同上,到此我們還沒有在VS項目中添加python代碼,只需要將hello.py復(fù)制到main.cpp所在目錄即可。
4、問題匯總
1、如果遇到Link110:無法打開python36_d.lib(或者python**_d.lib),在你的python/libs目錄下會發(fā)現(xiàn)沒有這個靜態(tài)庫文件,這時候最簡單的做法就是拷貝復(fù)制一份python36.lib,并重命名為python36_d.lib。
2、如果出現(xiàn)的錯誤:
Traceback (most recent call last):
File "<string>", line 1, in <module>
NameError: name 'hello' is not defined
這是由于沒有找到hello.py文件,解決方案:在初始化python解釋器以后添加以下兩行代碼,用于指定模塊的路徑:
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append('./')");
3、如果還有錯誤,請參考C++調(diào)用python。
5、完整的cpp代碼
//#include "D:\Python\Python37\include\Python.h"
//當(dāng)然,絕對路徑永遠(yuǎn)不會失效!^o^
#include <Python.h>
#include <iostream>
using namespace std;
void cython1()
{
Py_Initialize(); //初始化python解釋器,告訴編譯器要用的python編譯器
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append('./')");
PyRun_SimpleString("import hello"); //調(diào)用python文件
PyRun_SimpleString("hello.printHello()"); //調(diào)用上述文件中的函數(shù)
Py_Finalize(); //結(jié)束python解釋器,釋放資源
}
void cython2()
{
Py_Initialize();
PyObject* pModule = NULL;
PyObject* pFunc = NULL;
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append('./')");
pModule = PyImport_ImportModule("hello"); //這里是要調(diào)用的文件名
pFunc = PyObject_GetAttrString(pModule, "printHello"); //這里是要調(diào)用的函數(shù)名
PyEval_CallObject(pFunc, NULL); //調(diào)用函數(shù)
Py_Finalize();
}
int main()
{
int select;
cin >> select;
select == 1 ? cython1() : cython2();
return 0;
}
Linux中配置環(huán)境
其實這一部分有點多余,因為我在VScode下編程也采用gcc/g++編譯器,linux與之相同,所以遇到的問題也是一致的,都是沒有辦法直接#include<Python.h>。以Ubuntu 18.04.4 LTS為例:
1、step1
前提是你已經(jīng)安裝了g++,如果使用g++ -v查看版本無果,采用以下命令安裝套件:
sudo apt-get install build-essential
首先查看自己的python版本以及位置,位置用于代碼編寫調(diào)用頭文件,版本用于編譯g++命令的指示鏈接庫。由于ubuntu18.04自帶python3,所以命令中都采用了python3而不是python:
python3 -V >Python 3.6.9 which python3 >/usr/bin/python3
2、step2
找到版本以及位置,在C++代碼中采用絕對路徑的方法調(diào)用Python.h:
#include "/usr/include/python3.6/Python.h" //或者 #include <python3.6/Python.h>
同時也要在初始化python解釋器以后添加以下兩行代碼,用于指定模塊的路徑:
Py_Initialize();
PyRun_SimpleString("import sys"); // add 1
PyRun_SimpleString("sys.path.append('./')"); //add 2
3、測試
同樣在編譯時需要指定靜態(tài)鏈接庫,不然的話就用makefile吧~
注意python的版本:
g++ -o main main.cpp -lpython3.6m ./main
注意,也可以采用編譯時包含頭文件的方式,這樣就不用在代碼中調(diào)用頭文件了,不推薦(這樣的命令我要它有何用):
g++ -o main main.cpp -lpython3.6m -I /usr/include/python3.6 ./main
4、疑問
1、為什么VScode不用再代碼中指定python文件的路徑,而VS和Linux中都需要?
2、為什么在VS2019和Linux中都不用絕對路徑就可以調(diào)用Python.h文件,而在VScode中卻沒有作用?
還望與各位朋友探討。
到此這篇關(guān)于詳解如何在VS2019和VScode中配置C++調(diào)用python接口的文章就介紹到這了,更多相關(guān)C++調(diào)用python接口內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
select函數(shù)實現(xiàn)高性能IO多路訪問的關(guān)鍵示例深入解析
這篇文章主要為大家介紹了select函數(shù)實現(xiàn)高性能IO多路訪問的關(guān)鍵示例深入解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09
C# CLR 中學(xué)習(xí) C++關(guān)鍵詞extern使用詳解
這篇文章主要為大家介紹了C# CLR 中學(xué)習(xí) C++ 之extern使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
C語言實現(xiàn)獲取文件大小與創(chuàng)建修改時間
這篇文章主要為大家詳細(xì)介紹了如何通過C語言實現(xiàn)獲取文件大小、創(chuàng)建時間與修改時間,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-11-11
C語言數(shù)組實現(xiàn)三子棋應(yīng)用實例
這篇文章主要為大家詳細(xì)介紹了C語言數(shù)組實現(xiàn)三子棋應(yīng)用實例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-01-01

