簡體   English   中英

使用 Python 中的默認參數調用 C++ 函數

[英]Calling C++ function with default parameters from Python

我已經從 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; }

}

根據這個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 中工作嗎?

默認參數是 C++ 的一個優點。 如果您在 C++ 外部調用,則需要傳遞兩個參數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM