【问题标题】:Calling C++ function with default parameters from Python从 Python 调用具有默认参数的 C++ 函数
【发布时间】:2020-10-05 14:41:23
【问题描述】:

我已经从一个 DLL 中导出了这个简单的函数(我在 MSVC2015、Windows 10、x64 中工作):

// Main.h
#define DLL_SEMANTICS __declspec (dllexport)
extern "C"
{
    DLL_SEMANTICS int CheckVal(const int x, const int y = 1);

}

代码:

// Main.cpp

    int CheckVal(const int x, const int y)
    { cout << y << endl; return 0; }

}

根据this SO 线程使用extern "C" 不应该是一个问题,确实当 从 exe 文件调用这个函数我得到了想要的结果(即控制台的“1”)但是当我从 Python 调用它时使用:

import ctypes
from ctypes import cdll

dllPath = r"C:\MyDll.dll"
lib = cdll.LoadLibrary(dllPath)
lib.CheckVal(ctypes.c_int(1))

我正在打印一些垃圾值(通过 PTVS 调试确认 y 确实是垃圾值。)

我做错了什么或默认参数不能在 Python 中工作?

【问题讨论】:

    标签: python c++


    【解决方案1】:

    默认参数是 C++ 的一个优点。如果您在 C++ 外部调用,则需要传递两个参数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-10
      • 2013-08-11
      • 2012-11-03
      • 2014-02-27
      • 1970-01-01
      • 2011-01-27
      相关资源
      最近更新 更多