簡體   English   中英

在Windows中為Python編譯SWIG包裝器

[英]Compiling SWIG wrapper for python in Windows

首先,感謝大家在過去的幾年中在此論壇上給我的所有答案,但是今天我找不到明確的答案,所以我該發布了。

我設法在Debian distrib上編譯並運行C ++代碼test5.cpp ,將其(也許不是正確的詞)包裝到帶有SWIG的python模塊Amod.py (這需要一個“翻譯”文件test5.i 。我使用了並將數據分析模塊成功導入到其他更復雜的python代碼中(基本上是對numpy數組,matplotlib等的操作)。

現在我想在Windows計算機上運行相同的代碼,但是python無法再導入該模塊,庫文件_Amod.so是一個.so文件,而不是Windows期望的.pyd文件。 但是我找不到在Windows上重新編譯它的快速簡便的方法。 我挖了CodeBlocks文檔,但措手不及,我迷路了。 http://wiki.codeblocks.org/index.php?title=Adding_support_for_non_C/C%2B%2B_files_to_the_build_system

基本上,我想運行與以下工作代碼等效的Windows(希望對SWIG初學者有所幫助):

編譯:

swig -c++ -python test5.i
g++ -fPIC -c test5.cpp
g++ -fPIC -c test5_wrap.cxx -I/usr/include/python2.7
g++ -shared test5.o test5_wrap.o -o _Amod.so

在不浪費時間的情況下,我應該使用Wich軟件和編譯器,以及如何使用? (我已經有了CodeBlocks)非常感謝。

為了獲得所需的信息,以下是test5.i ,將C ++數組包裝在numpy數組中,並添加了一些內聯(替換函數)以進行檢查(所有內容都被流血的淚水幫助中挖掘出來):

/* %module module_name is used in compilation as: g++ -shared main.o main_wrap.o -o _module_name.so (with the underscore)*/
%module Amod

%{
/* Put header files here or function declarations like below. The #define SWIG_FILE_WITH_INIT line inserts a macro that specifies that the resulting C file should be built as a python extension, inserting the module init code. check http://www.swig.org/Doc1.3/Python.html#Python_nn3 */
#define SWIG_FILE_WITH_INIT
#include "test5.h"
%}

/* numpy.i and import_array() allow SWIG to manipulate C++ pointer (like double* ivec) like numpy array, because SWIG doesn't know a priori, that the pointer refer to an array. check http://docs.scipy.org/doc/numpy/reference/swig.interface-file.html */

%include "numpy.i"

%init %{
import_array();
%}


/* C++ function arg must fits the typemap directives available in numpy.i. */

%apply (int DIM1, double* INPLACE_ARRAY1) {(int len1, double* ivec),(int len2, double* ovec),(int len3, double* gauss)};
%rename (abel) abel_swig;
%exception abel_swig {
    $action
    if (PyErr_Occurred()) SWIG_fail;
}
%inline %{
void abel_swig(int len1, double* ivec, int len2, double* ovec, int algo, double alpha) {
    if (len1 != len2) {
        PyErr_Format(PyExc_ValueError,"Arrays of lengths (%d,%d) given",len1, len2);
        return;
    }
    memset(ovec, 0, len1*sizeof(double));
    return abel(len1, ivec, len2, ovec, algo, alpha);
}
%}


%rename (convol_gauss) convol_gauss_swig;
%exception convol_gauss_swig {
    $action
    if (PyErr_Occurred()) SWIG_fail;
}
%inline %{
void convol_gauss_swig(int len1, double* ivec, int len2, double* ovec, int len3, double* gauss) {
    if ((len1 != len2)||(len1 != len3)) {
        PyErr_Format(PyExc_ValueError,"Arrays of lengths (%d,%d,%d) given, they must be the same",len1, len2, len3);
        return;
    }
    memset(ovec, 0, len1*sizeof(double));
    return convol_gauss(len1, ivec, len2, ovec, len3, gauss);
}
%}


/* Put header files here or function declarations like below */

%include "test5.h"

和標頭test5.h

#ifndef TEST5_H_INCLUDED
#define TEST5_H_INCLUDED
#include <cstring>
#include <iostream>
#include <cmath>
void convol_gauss(int size, double* ivec, int size2, double* ovec, int size3, double* gauss);
void abel(int len1, double* ivec, int len2, double* ovec, int algo);

#endif // TEST5_H_INCLUDED

好的,我設法做到了。

1-請注意您.i文件使用utf-8編碼,因為如果Python使用其他編碼,它會阻止swig編譯。(# #error Must use Python with unicode enabled

2-在www.swig.org上下載適用於Windows的swig並安裝MinGW

3種類型的cmd或Powershell

swig -c++ -python test5.i
g++ -c -Wall test5.cpp
g++ -c -Wall test5_wrap.cxx -I C:\Python27\include -I C:\Python27\Lib\site-packages\numpy\core\include\
g++ -Wall -shared -I C:\Python27\include -I C:\Python27\Lib\site-packages\numpy\core\include\ test5.o test5_wrap.o -o _Amod.pyd -L C:/Python27/libs/ -lpython27

-IC:\\Python27\\Lib\\site-packages\\numpy\\core\\include\\解決錯誤: fatal error : numpy\\arrayobject.h no such file or directory

-LC:/Python27/libs/ -lpython27解決錯誤: undefined reference to _imp__Py...

與Linux編譯相反,必須仔細鏈接庫C:/Python27/libs/python27.lib以及標頭的目錄(C:\\ Python27 \\ include和numpy C:\\ Python27 \\ Lib \\ site -packages \\ numpy \\ core \\ include)希望它能有所幫助,但我不認為這是一項非常干凈的工作。 考慮到我遇到的錯誤,請參閱上一篇文章

暫無
暫無

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

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