In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-10-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces how to use C++ to call Python code, the article is very detailed, has a certain reference value, interested friends must read!
I. configuring the python environment
1. First install Python (version doesn't matter), and add the python path to the environment variable when installing.
The folder after installation is as follows:
two。 Right-click Project-> Properties-> VC++ directory in VS to configure the environment and libraries
1) include directories:
Python installation path / include
2) Library directory:
Python installation path / libs
Right-click item-> Properties-> Connector-> input-> attach dependent Library
Under debug:
Python installation directory / libs/python37_d.lib
Under release:
Python installation directory / libs/python37.lib
Be careful
1. Debug may not be configured with python37_d.lib, so just copy a python37.lib and rename it to python37_d.lib.
2. If you keep reporting errors, but there is no problem including header files, then you need to see whether your python is 32-bit or 64-bit. Then configure the vs environment according to the python environment.
C++ calls the python function and outputs the return value
First of all, there may be a hole! Be sure to make sure that the file where the python function is located is working properly! Then put the python code in the same directory as the C++ code.
1. Define Python function
#! python3#-*-coding:utf-8-*-import base64import hmacfrom hashlib import sha1def hash_hmac (code, key): # sha1 encryption signature algorithm hmac_code = hmac.new (key.encode (), code.encode (), sha1). Digest () return base64.b64encode (hmac_code). Decode ()
2. Write C++ code
# include # includeusing namespace std;int main () {Py_Initialize (); / / call Py_Initialize () before using python; this function initializes if (! Py_IsInitialized ()) {printf ("initialization failed!") ; return 0;} PyRun_SimpleString ("import sys"); PyRun_SimpleString ("sys.path.append ('. /')"); / / this step is very important. Modify the Python path PyObject* pModule = NULL;// declaration variable PyObject* pFunc = NULL;// declaration variable pModule = PyImport_ImportModule ("hash_hmac"); / / here is the file name hash_hmac.py if (pModule = = NULL) {cout
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.