python調(diào)用C/C++函數(shù)

OS

window10 64

python版本

python --version
Python 3.10.7

準(zhǔn)備C++代碼

tips

  1. demo里兼容了python2.x和python3.x
  2. 動態(tài)庫名字為test,則初始化方法也必須為init_test/PyInit_test,否則會報(bào):LINK : error LNK2001: 無法解析的外部符號 PyInit_xxx
  3. c++代碼需要用extern "C" 修飾init函數(shù),(其實(shí)此方式不支持C++,用C得方式調(diào)用了C++而已)

cpp/test.cpp

// Python includes
#include <Python.h>

int Add(int x, int y) 
{ 
    return x + y; 
} 

PyObject* WrappAdd(PyObject* self, PyObject* args) 
{ 
    int x, y; 
    if (!PyArg_ParseTuple(args, "ii", &x, &y)) 
    { 
        return NULL; 
    } 
    return Py_BuildValue("i", Add(x, y)); 
} 

//-----------------------------------------------------------------------------
static PyMethodDef test_methods[] = {
  {"Add", WrappAdd, METH_VARARGS, "Add"},
  {NULL, NULL, 0, NULL}        /* Sentinel */
};

//-----------------------------------------------------------------------------
#if PY_MAJOR_VERSION < 3
PyMODINIT_FUNC init_test(void)
{
  (void) Py_InitModule("test", test_methods);
}
#else /* PY_MAJOR_VERSION >= 3 */
static struct PyModuleDef test_module_def = {
  PyModuleDef_HEAD_INIT,
  "test",
  "Internal \"test\" module",
  -1,
  test_methods
};

PyMODINIT_FUNC PyInit_test(void)
{
  return PyModule_Create(&test_module_def);
}
#endif /* PY_MAJOR_VERSION >= 3 */

使用導(dǎo)出模塊 setup.py

from distutils.core import setup, Extension

import io

long_description = io.open("[README.md](http://README.md)", encoding="utf-8").read()  
module_device = Extension('test',  sources = ['cpp/test.cpp']  )

setup (name = 'test-cpp2python',
            version = '1.1.0',
            description = 'test-cpp2python',
            long_description=long_description,
            long_description_content_type="text/markdown",
            author='hcm',
            url='',
            license='MIT',
            ext_modules = [module_device],
            options={'build':{'build_lib':'./test'}},
            classifiers=[
                "Development Status :: 5 - Production/Stable",
                "Environment :: Console",
                "Intended Audience :: Developers",
                "Intended Audience :: Education",
                "Intended Audience :: Information Technology",
                "Intended Audience :: Science/Research",
                "License :: OSI Approved :: MIT License",
                "Operating System :: Microsoft :: Windows",
                "Programming Language :: Python",
                "Programming Language :: Python :: 3",
                "Programming Language :: Python :: 3 :: Only",
                "Programming Language :: Python :: 3.6",
                "Programming Language :: Python :: 3.7",
                "Programming Language :: Python :: 3.8",
                "Programming Language :: Python :: 3.9",
                "Programming Language :: Python :: 3.10",
                "Programming Language :: C++",
                "Programming Language :: Python :: Implementation :: CPython",
                "Topic :: Scientific/Engineering",
                "Topic :: Software Development",
            ],)

導(dǎo)出python擴(kuò)展模塊

python setup.py build

查看生成的pyd文件

test/test.cp310-win_amd64.pyd

測試

cd test
python
>>> import test
>>> test.Add(1,2)
3
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容