Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does C++ call python

2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how C++ calls python, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to understand it!

1. First of all, it is necessary to match the vs development project.

Pay attention to the version; I use the 32-bit python here, so I also choose the 32-bit compilation environment to configure the vs project.

Note: you need to copy some files from the python installation directory to use as a vs project.

2. C++ calls Python result

Py code

The cdll library referenced here also needs to be placed in the running directory, and the py file also needs to be placed in the running directory (that is, the directory where exe is generated)

Import osimport timefrom ctypes import * def testDLL (): pDll = CDLL (". / pythonTestCDll.dll") pstr = create_string_buffer (1024 '\ 0') # create string buffer # declare input and output parameters GetAndSetString = pDll.GetAndSetString GetAndSetString.restype = c_char_p GetAndSetString.argtypes = [c_char_p] pchar = GetAndSetString (pstr) szbuffer = c_char_p (pchar) # cast to c_char_p type Take its value print (pstr.value) print (szbuffer.value) def Start (): testDLL ()

C++ Code

# include # include "Python.h" using namespace std;void Hello (); void Add (); void Start (); void Hello1 () {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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report