将SDK SWIG转换为Python

将SDK SWIG转换为Python,python,c++,g++,mingw,swig,Python,C++,G++,Mingw,Swig,我是SWIG的新手,所以我绝对没有这方面的经验,但我真的很想使用Riftek LASER()的SDK SDK本身包含一些头文件、dll和def文件。到目前为止,我已经编写了以下SWIG文件: /* rfdevice.i */ %module rfdevice %{ #include <windows.i> #include <typemaps.i> #include "include/RF625Device.h" #include "include/RF625Device

我是SWIG的新手,所以我绝对没有这方面的经验,但我真的很想使用Riftek LASER()的SDK

SDK本身包含一些头文件、dll和def文件。到目前为止,我已经编写了以下SWIG文件:

/* rfdevice.i */
%module rfdevice
%{
#include <windows.i>
#include <typemaps.i>
#include "include/RF625Device.h"
#include "include/RF625Device_Legacy.h"
#include "include/RFDevice.h"
#include "include/RFEthernetDetector.h"
#include "include/RFEthernetDevice.h"
#include "include/RFQDPMotorDevice.h"
#include "include/RFSerialDevice.h"
#include "include/RFString.h"
#include "include/RFTypeDefs.h"
#include "include/serial.h"
%}
%include <windows.i>
%include <typemaps.i>

%apply void *INPUT {void *lpResultBuffer};
%apply float *OUTPUT {float *lpPointsBuffer};
%apply USHORT *OUTPUT {USHORT *lpCount};

%include "include/RFString.h"
%include "include/serial.h"
namespace RFDevice {
    %include "include/RFTypeDefs.h"
    %include "include/RFEthernetDetector.h"
    %include "include/RFDevice.h"
    %include "include/RFEthernetDevice.h"
    %include "include/RFQDPMotorDevice.h"
    %include "include/RFSerialDevice.h"
    %include "include/RF625Device_Legacy.h"
    %include "include/RF625Device.h"
}
这里的“输入”和“输出”导致了SWIG的一些truble。我不断收到以下编译器错误:

include\RF625Device_Legacy.h(259): Error: Syntax error in input(3).

在SWIG文档中的参数处理下,有可能解决该问题。我已经将它包含在I文件中,但我一直收到编译器错误

你可以这样开始。我使用CMake

这是我的CMakeLists.txt文件。到目前为止,我可以创建RFString对象

cmake_minimum_required(VERSION 2.8.9)

find_package(SWIG REQUIRED)
include(${SWIG_USE_FILE})

find_package(PythonLibs REQUIRED)

include_directories(${PYTHON_INCLUDE_PATH})
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)

set_source_files_properties(RFDevice_SDK.i PROPERTIES CPLUSPLUS ON)

set(DEPS_PREFIX ${CMAKE_SOURCE_DIR}/linux)

link_directories(${DEPS_PREFIX})

set(RFDEVICE_LIBS_NAMES RFDevice3)

foreach(lib ${RFDEVICE_LIBS_NAMES})
  find_library(
    ${lib}_LIB
    NAMES ${lib}
    PATHS ${DEPS_PREFIX}
    PATH_SUFFIXES i386 x86_64
  )
  set(RFDEVICE_LIBS ${RFDEVICE_LIBS} ${${lib}_LIB})
endforeach()

message(${RFDEVICE_LIBS})

set(swig_rfdevice_sdk_HEADERS
  ./include/linuxTypeDefs.h
  ./include/RFString.h
)

set(swig_rfdevice_sdk_SOURCES
)

swig_add_module(swig_rfdevice_sdk python RFDevice_SDK.i ${swig_rfdevice_sdk_HEADERS} ${swig_rfdevice_sdk_SOURCES})

swig_link_libraries(swig_rfdevice_sdk ${RFDEVICE_LIBS} ${PYTHON_LIBRARIES})
这是.o文件

%module(docstring="This is a Python wrapper for RFDevice3") swig_rfdevice_sdk
#pragma SWIG nowarn=320
%{

  #define SWIG_FILE_WITH_INIT
  // Try to ignore cast between pointer-to-function and pointer-to-object
  #include "stdint.h"
  #include "linuxTypeDefs.h"
  #include "RFString.h"
%}

#ifdef _SWIG_WIN32
%include "windows.i"
#endif

%include "carrays.i"
%array_class(char, charArrayClass);
%array_functions(char, charArray);

// Individual modules
%include "linuxTypeDefs.h"
%include "RFString.h"

我必须安装一个旧版本的glibc++才能工作。或者,您可以链接到旧版本的glibc++。

您应该在此处包含来自编译器/链接器的错误消息,请参见为什么要用g++编译*.c文件?无法在i(接口)文件中包含dll/def文件,但您必须在编译代码时包含它们谢谢您的回答V-master。亚历山大:SDK中没有包含*.c文件。只有*.h文件。当我查看文件时,语法看起来比C++更类似。实际上,我用G++比GCC更少的编译器错误。1)从不包含。i文件使用了包含的。这是没有意义的,它们不是C或C++ 2)SWIG不递归头,所以头文件必须以正确的顺序包含。3) 不要包含带有命名空间的.i文件。您得到的语法错误很可能是由于未知的定义造成的,例如,导出声明在解释如何创建接口时,我将使用ctypes,在Python中创建具有相同二进制布局的结构,并简单地调用它们。
%module(docstring="This is a Python wrapper for RFDevice3") swig_rfdevice_sdk
#pragma SWIG nowarn=320
%{

  #define SWIG_FILE_WITH_INIT
  // Try to ignore cast between pointer-to-function and pointer-to-object
  #include "stdint.h"
  #include "linuxTypeDefs.h"
  #include "RFString.h"
%}

#ifdef _SWIG_WIN32
%include "windows.i"
#endif

%include "carrays.i"
%array_class(char, charArrayClass);
%array_functions(char, charArray);

// Individual modules
%include "linuxTypeDefs.h"
%include "RFString.h"