简体   繁体   English

如何使用 Python.h 和 MinGW 在 Windows 10 上编译调用 Python 方法的 C 文件

[英]How to compile a C-File that calls a Python-Method on Windows 10 with Python.h and MinGW

I needed to compile a C-Programm that uses the Python.h to call a Python-Method from an existing Python-File.我需要编译一个 C 程序,它使用Python.h从现有的 Python 文件调用 Python 方法。

And all that on a Windows 10 machine with MinGW .所有这些都在带有MinGW的 Windows 10 机器上。

As I could not find a straight-up answer on what compile command I need, I will now post what works for me in the hope it helps others too!由于我找不到关于我需要什么编译命令的直接答案,我现在将发布对我有用的内容,希望它也能帮助其他人!

The command that worked for me is as follows:对我有用的命令如下:

gcc -o nameOfexe nameOfCFile.c -I PathToPython\include -L PathToPython\libs  -lpythonXX

Explanation:解释:

  • nameOfexe : nameOfexe :
    the name you want for your .exe, so to speak the result of the command您想要的 .exe 名称,可以说是命令的结果
  • nameOfCFile.c : nameOfCFile.c :
    the C-File you want to compile and holds the call to the Python-Method要编译的 C 文件并保存对 Python 方法的调用
  • PathToPython\\include : PathToPython\\include
    the path to your python\\include folder, example: C:\\Python_3_9_0\\include python\\include 文件夹的路径,例如:C:\\Python_3_9_0\\include
  • PathToPython\\libs : PathToPython\\libs
    the path to your python\\libs folder, example: C:\\Python_3_9_0\\libs python\\libs 文件夹的路径,例如:C:\\Python_3_9_0\\libs
  • -lpythonXX : -lpythonXX :
    that specifies what lib to compile for python to work,指定要编译哪些库以使 python 工作,
    XX = your version: python3.9.0 --> -lpython39 XX = 你的版本:python3.9.0 --> -lpython39

Both the C-File and the Python-File are in the same Folder I run the command in. C-File 和 Python-File 都在我运行命令的同一个文件夹中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM