gpt4 book ai didi

python - 如何防止 Python 2.7 的 在 SWIG 生成的 Python 包装器中包含

转载 作者:行者123 更新时间:2023-11-28 06:24:44 25 4
gpt4 key购买 nike

我正在开发一个 C++ 库,其中使用 SWIG 生成其 Python 包装器。我的一些 C++ 文件使用 <inittypes.h>在 sprintf 中调用 PRId64 和其他宏。

我能够在 Scientific Linux 6(RHEL6 克隆)上使用 Python 2.6 和 GCC 4.4.7 编译我的库,但是在 Scientific Linux 7(RHEL7 克隆)上的 Python 2.7 和 GCC 4.8.2 产生了许多如下错误。

/home/oxon/libTARGET/inc/target/T2EvalBoard.h:562:145: warning: too many arguments for format [-Wformat-extra-args]
In file included from /home/oxon/libTARGET_build/src/targetPYTHON_wrap.cxx:3117:0:
/home/oxon/libTARGET/inc/target/BaseCameraModule.h: In member function ‘virtual void TARGET::BaseCameraModule::ReceiveEvent(uint32_t&, uint8_t**)’:
/home/oxon/libTARGET/inc/target/BaseCameraModule.h:211:66: error: expected ‘)’ before ‘PRIu32’
sprintf(str, "Cannot read event data. Requested length is %" PRIu32 " bytes, but only %" PRId64 " bytes were read.", length, fBytesReturned);

我知道我必须先在头文件中添加以下行才能使用 PRId64及其他。

#define __STDC_FORMAT_MACROS
#include <inttypes.h>

但是targetPYTHON_wrap.cxx ,这是一个由 SWIG 生成的源文件,包括 <Python.h>在文件的开头,所以上面的行被忽略了。确实,下面的代码无法编译,因为<Python.h>包括 <inttypes.h>

#include <Python.h>

#define __STDC_FORMAT_MACROS
#include <inttypes.h>

#include <stdio.h>

int main()
{
printf("Output: " PRIu32 "\n", 100);
return 0;
}

我如何使用 PRId64和其他带有 <Python.h> 的宏和痛饮?

最佳答案

在 SWIG 中,以下代码将行添加到 SWIG 包装器的最顶部,因此它将在 Python.h 之前定义:

%begin %{
#define __STDC_FORMAT_MACROS
#include <inttypes.h>
%}

关于python - 如何防止 Python 2.7 的 <Python.h> 在 SWIG 生成的 Python 包装器中包含 <inttypes.h>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28721218/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com