Linux下swig的正確安裝 | 第一個(gè)(C++轉(zhuǎn)Python接口)測(cè)試示例 | (二)
本博文教程在linux環(huán)境下進(jìn)行C++編譯和測(cè)試,因此,需要linux下C++環(huán)境可用
本博文相關(guān)代碼主要來(lái)自于官方 PDF文檔查閱鏈接
一個(gè)博主的win10 下 Swig使用教程
文檔查閱
PDF 文檔查閱鏈接
http://www.swig.org/Doc4.0/index.html
SWIG 的作用:
SWIG的主要目的是簡(jiǎn)化將C / C ++與其他編程語(yǔ)言集成的任務(wù);
SWIG本質(zhì)上是一個(gè)生成代碼的工具,用于使C/ c++代碼對(duì)其他各種編程語(yǔ)言可用。
這些高級(jí)編程語(yǔ)言是SWIG代碼生成器的目標(biāo)語(yǔ)言,而C或c++是輸入語(yǔ)言。
運(yùn)行SWIG時(shí)必須指定單一目標(biāo)語(yǔ)言,這將導(dǎo)致為C/ c++和指定的目標(biāo)語(yǔ)言生成相互接口的代碼。
第一部分:Linux下swig的安裝
查看 gcc 、g++版本
gcc -v Using built-in specs. COLLECT_GCC=gcc .. .. gcc version 7.5.0 (Ubuntu 7.5.0-3ubuntu1~18.04) g++ -v Using built-in specs. COLLECT_GCC=g++ .. .. gcc version 7.5.0 (Ubuntu 7.5.0-3ubuntu1~18.04)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Linux系統(tǒng)下 root 用戶 和 普通用戶的 swig 安裝方式如下
出現(xiàn)如下報(bào)錯(cuò)的可能原因:
依賴庫(kù) PCRE 沒有安裝
PCRE 安裝了,但是安裝的版本和 swig 不匹配
configure: error: Cannot find pcre-config script from PCRE (Perl Compatible Regular Expressions) library package. This dependency is needed for configure to complete, Either: - Install the PCRE developer package on your system (preferred approach). - Download the PCRE source tarball, build and install on your system as you would for any package built from source distribution. - Use the Tools/pcre-build.sh script to build PCRE just for SWIG to statically link against. Run 'Tools/pcre-build.sh --help' for instructions. (quite easy and does not require privileges to install PCRE on your system) - Use configure --without-pcre to disable regular expressions support in SWIG (not recommended).
1
2
3
4
5
6
7
8
9
10
11
12
13
解決方法上一篇博文有親測(cè)的安裝教程:
SWIG簡(jiǎn)介 | win10和Linux下的安裝 | 第一講
指定參數(shù) 不加載 PCRE 模塊 – 也可以解決該問題
# 刪除已有 swig ,重新 下載 操作如下 wget http://prdownloads.sourceforge.net/swig/swig-4.0.2.tar.gz tar zxvf swig-4.0.2.tar.gz cd swig-4.0.2 ./configure --without-pcre make -j make install
1
2
3
4
5
6
7
8
9
10
第一個(gè)(C++轉(zhuǎn)Python接口)測(cè)試示例
下面命令的執(zhí)行,可能會(huì)需要參考:
Linux系統(tǒng) 搜索 Python.h 位置
我這是swig轉(zhuǎn)化測(cè)試主要參考 :2.4.4 Building a Python module
現(xiàn)有程序 example.c
/* File : example.c */ double My_variable = 3.0; /* Compute factorial of n */ int fact(int n) { if (n <= 1) return 1; else return n*fact(n-1); } /* Compute n mod m */ int my_mod(int n, int m) { return(n % m); }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
現(xiàn)有程序 example.i
/* File : example.i */ %module example %{ /* Put headers and other declarations here */ extern double My_variable; extern int fact(int); extern int my_mod(int n, int m); %} extern double My_variable; extern int fact(int); extern int my_mod(int n, int m);
1
2
3
4
5
6
7
8
9
10
11
12
13
步驟操作如下:
swig -python example.i # 編譯得到 .o 靜態(tài)庫(kù) gcc -c -fpic example.c example_wrap.c -I/usr/include/python3.5
1
2
3
4
5
查看效果如下:
ll total 192 drwxrwxr-x 2 zengql zengql 4096 Mar 10 17:24 ./ drwxrwxr-x 6 zengql zengql 4096 Mar 10 16:52 ../ -rw-rw-r-- 1 zengql zengql 212 Mar 10 12:01 example.c -rw-rw-r-- 1 zengql zengql 255 Mar 10 12:14 example.i -rw-rw-r-- 1 zengql zengql 1568 Mar 10 17:24 example.o -rw-rw-r-- 1 zengql zengql 2197 Mar 10 17:13 example.py -rw-rw-r-- 1 zengql zengql 110768 Mar 10 17:13 example_wrap.c -rw-rw-r-- 1 zengql zengql 53608 Mar 10 17:24 example_wrap.o
1
2
3
4
5
6
7
8
9
10
11
12
13
生成得到 一個(gè) Python 可用的 so 動(dòng)態(tài)庫(kù)
gcc -shared example.o example_wrap.o -o _example.so
1
2
3
測(cè)試 import example 是否成功
python Python 3.6.4 |Anaconda, Inc.| (default, Jan 16 2018, 18:10:19) [GCC 7.2.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import example >>> example.fact(4) 24 >>> example.fact(8) 40320
1
2
3
4
5
6
7
8
9
10
至此,第一個(gè)swig 工具 C++轉(zhuǎn)Python接口 的小 demo 終于搞定
C++ Linux Python
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請(qǐng)聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時(shí)內(nèi)刪除侵權(quán)內(nèi)容。
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請(qǐng)聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時(shí)內(nèi)刪除侵權(quán)內(nèi)容。