[英]How to prevent <Python.h> of Python 2.7 from including <inttypes.h> in a Python wrapper generated by SWIG?
我正在開發一個使用SWIG生成其Python包裝器的C ++庫。 我的某些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>
,因此忽略了上述targetPYTHON_wrap.cxx
行。 實際上,以下代碼無法編譯,因為<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;
}
如何在<Python.h>
和SWIG中使用PRId64
和其他宏?
在SWIG中,以下代碼在SWIG包裝程序的最頂部添加了幾行,因此它將在Python.h
之前定義:
%begin %{
#define __STDC_FORMAT_MACROS
#include <inttypes.h>
%}
我加-D__STDC_FORMAT_MACROS
在CXX_FLAGS
,但尋找是否存在更好的解決方案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.