新聞中心
Go語言是一種高性能、開發(fā)效率高的編程語言,受到越來越多的開發(fā)者的青睞。而Linux系統(tǒng)則是目前更流行的服務(wù)器操作系統(tǒng)之一,也被越來越多的企業(yè)和開發(fā)者使用。因此,在Go語言中使用Linux API成為了一個非常重要的話題。

然而,Go語言本身對于Linux系統(tǒng)API的支持并不完備。這使得在Go語言中直接調(diào)用Linux API變得非常困難。這時,一個非常不錯的解決方案是使用SWIG。
SWIG是一個用于將C和C++代碼轉(zhuǎn)換成其他編程語言的工具,它能夠自動生成其他語言的綁定代碼。在這里,我們將介紹如何。
安裝SWIG
我們需要安裝SWIG。SWIG的安裝很簡單,可以通過在Linux上運行以下命令輕松完成:
“`
$ sudo apt-get install swig
“`
現(xiàn)在,SWIG已經(jīng)安裝好了。
創(chuàng)建API Wrapper
接下來,我們需要創(chuàng)建一個API wrapper來將Linux API和Go語言進行整合。在這個wrapper中,我們需要聲明Linux API中需要使用的所有函數(shù)。
例如,要在Go語言中使用Linux的read函數(shù),我們需要聲明以下代碼:
“`
ptrdiff_t
read(int fd, void *buf, size_t nbytes);
“`
這里需要注意的是,我們需要在每個函數(shù)聲明前添加“extern “C””來保證它們能在Go語言中正確導(dǎo)出。
將C代碼編譯成Go可用的動態(tài)鏈接庫
接下來,我們需要將C代碼編譯成Go可以鏈接的動態(tài)鏈接庫。這可以通過使用gcc編譯器和-shared選項來完成。例如,要將我們的wrapper編譯成動態(tài)鏈接庫,請運行以下命令:
“`
$ gcc -Wall -Werror -fpic -c api.c
$ gcc -shared -o libapi.so api.o
“`
在這個命令中,我們首先將api.c編譯成一個目標文件api.o,然后將其鏈接成libapi.so動態(tài)鏈接庫。
在Go語言中使用API
現(xiàn)在,我們已經(jīng)準備好在Go語言中使用Linux API了。我們需要先聲明我們將要使用的函數(shù)。例如,如果我們需要使用Linux的read函數(shù),我們需要在Go語言中聲明以下代碼:
“`
// 在/_cgo_export.h中聲明read函數(shù)
extern __attribute__((__visibility__(“default”))) __attribute__((__cdecl__)) ptrdiff_t _wrap_read(int, void*, size_t);
// 在apiWrapper.go中聲明read函數(shù)
func wrapRead(fd int, buf []byte, nbytes uint) (n int) {
if len(buf) == 0 {
return 0
}
r := _wrap_read(int(fd), unsafe.Pointer(&buf[0]), nbytes)
if r
panic(“read error”)
}
return int(r)
}
“`
這里,我們首先在/_cgo_export.h中聲明了_wrap_read函數(shù)。然后,在apiWrapper.go中,我們定義了一個名為wrapRead的函數(shù)。這個函數(shù)將調(diào)用_wrap_read并用它的返回值替換掉錯誤返回值。
接下來,我們需要在Go語言中將動態(tài)鏈接庫和API wrapper鏈接起來。為此,我們可以使用#cgo指令和LD_PRELOAD環(huán)境變量。例如,若要使用我們剛剛創(chuàng)建的libapi.so動態(tài)鏈接庫,需要在Go代碼中加入以下代碼:
“`
// #cgo LDFLAGS: -L{path_to_lib} -lapi -Wl,-rpath={path_to_lib}
import “C”
import (
_ “unsafe”
)
func init() {
C.char_Read = C.wrapRead
}
“`
這里,我們使用了#cgo指令來指定動態(tài)鏈接庫的路徑。然后,在init函數(shù)中,我們將聲明的wrapRead函數(shù)綁定到Linux的read函數(shù)上。
現(xiàn)在,我們就可以在Go語言中直接使用Linux API了。例如,以下代碼將使用Linux的open函數(shù)打開一個文件:
“`
package mn
import (
“fmt”
“syscall”
)
func mn() {
fd, err := syscall.Open(“filename.txt”, syscall.O_RDON, 0)
if err != nil {
fmt.Println(err)
return
}
defer syscall.Close(fd)
// 使用fd和Linux的read和write函數(shù)進行操作
}
“`
結(jié)論
在本文中,我們介紹了如何。通過創(chuàng)建API wrapper和使用#cgo指令,在Go語言中使用Linux API變得更加容易。雖然這種方法可能有些復(fù)雜,但是它能夠為我們的應(yīng)用程序帶來更好的性能和更強大的功能,因此非常值得一試。
相關(guān)問題拓展閱讀:
- 如何這道tensorflow卸載成功 linux
如何這道tensorflow卸載成功 linux
安裝tensorflow
pip install –upgrade
錯誤:oserror no such file or directory site-packages/setuptools-21.0.0-py2.7.egg
解決:pip install –upgrade
–ignore-installed
Python 2.7.3 (default, Apr, 11:18:30)
on linux2
Type “help”, “copyright”, “credits” or “l(fā)icense” for more information.
>>> import tensorflow as tf
Traceback (most recent call last):
File “”, line 1, in
File “/usr/local/python27/lib/python2.7/site-packages/tensorflow/__init__.py”, line 23, in
from tensorflow.python import *
File “/usr/local/python27/lib/python2.7/site-packages/tensorflow/python/__init__.py”, line 45, in
from tensorflow.python import pywrap_tensorflow
File “做毀/usr/local/python27/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow.py”, line 28, in
_pywrap_tensorflow = swig_import_helper()
File “/usr/local/python27/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow.py”, line 24, in swig_import_helper
_mod = imp.load_module(‘_pywrap_tensorflow’, fp, pathname, description)
ImportError: /lib64/libc.so.6: version `GLIBC_2.15′ not found (required by /usr/local/python27/lib/python2.7/site-packages/tensorflow/python/_pywrap_tensorflow.so)
tensorflow0.80版本編譯的時候使用GLIBC_2.15,系統(tǒng)自帶的是GLIBC_2.12,所以報錯了。
安灶枝裝新版glibc
1.glibc下載
從
下載源代碼。我下載的版本是2.20,鏈接地址是
2.安裝
因為glibc庫使用廣泛,為了避免污染當前系統(tǒng)環(huán)境,更好自定義安裝目錄,使用時定義一下環(huán)境變量就行了。具體步驟如下:
我使用的法(直接升級glibc)隱胡敏:
wget
xz -d glibc-2.17.tar.xz
tar -xvf glibc-2.17.tar
cd glibc-2.17
mkdir build
cd build
../configure –prefix=/usr –disable-profile –enable-add-ons –with-headers=/usr/include –with-binutils=/usr/bin
make && make install
需要等大概10分鐘。
輸入strings /lib64/libc.so.6|grep GLIBC發(fā)現(xiàn)已經(jīng)更新
Python 2.7.3 (default, Apr, 11:18:30)
on linux2
Type “help”, “copyright”, “credits” or “l(fā)icense” for more information.
>>> import tensorflow as tf
Traceback (most recent call last):
File “”, line 1, in
File “/usr/local/python27/lib/python2.7/site-packages/tensorflow/__init__.py”, line 23, in
from tensorflow.python import *
File “/usr/local/python27/lib/python2.7/site-packages/tensorflow/python/__init__.py”, line 45, in
from tensorflow.python import pywrap_tensorflow
File “/usr/local/python27/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow.py”, line 28, in
_pywrap_tensorflow = swig_import_helper()
File “/usr/local/python27/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow.py”, line 24, in swig_import_helper
_mod = imp.load_module(‘_pywrap_tensorflow’, fp, pathname, description)
ImportError: /usr/lib64/libstdc++.so.6: version `GLIBCXX_3.4.14′ not found (required by /usr/local/python27/lib/python2.7/site-packages/tensorflow/python/_pywrap_tensorflow.so)
沒有GLIBCXX_3.4.14版本支持,繼續(xù)安裝(注意:libstdc++6_4.7.2-5_amd64.deb這是64位,libstdc++6_4.7.2-5_i386.deb這個是32位)
下載新版本,地址:
ar -x libstdc++6_4.7.2-5_amd64.deb&&tar xvf data.tar.gz
#cd /root/usr/lib/x86_64-linux-gnu
#ll
lrwxrwxrwx 1 root rootApr 26 15:21 libstdc++.so.6 -> libstdc++.so.6.0.17
-rw-r–r– 1 root rootJanlibstdc++.so.6.0.17
# find / -name libstdc++.so.6
/usr/lib64/libstdc++.so.6
/root/usr/lib/x86_64-linux-gnu/libstdc++.so.6
#mv /usr/lib64/libstdc++.so.6 /usr/lib64/libstdc++.so.6.bak
#cp libstdc++.so.6.0.17 /usr/lib64/
#cd /usr/lib64/
#chmod +x libstdc++.so.6.0.17
#ll libstdc++.so.6.0.17
-rwxr-xr-x 1 root rootApr 26 15:30 libstdc++.so.6.0.17
#ln -s libstdc++.so.6.0.17 libstdc++.so.6
>>> import tensorflow as tf
Traceback (most recent call last):
File “”, line 1, in
File “/usr/local/python27/lib/python2.7/site-packages/tensorflow/__init__.py”, line 23, in
from tensorflow.python import *
File “/usr/local/python27/lib/python2.7/site-packages/tensorflow/python/__init__.py”, line 45, in
from tensorflow.python import pywrap_tensorflow
File “/usr/local/python27/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow.py”, line 28, in
_pywrap_tensorflow = swig_import_helper()
File “/usr/local/python27/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow.py”, line 24, in swig_import_helper
_mod = imp.load_module(‘_pywrap_tensorflow’, fp, pathname, description)
ImportError: /usr/lib64/libstdc++.so.6: version `GLIBCXX_3.4.19′ not found (required by /usr/local/python27/lib/python2.7/site-packages/tensorflow/python/_pywrap_tensorflow.so)
從網(wǎng)上下載libstdc++.so.6.0.20
或者
放到/usr/lib64/下
#chmod +x libstdc++.so.6.0.20
#rm libstdc++.so.6
#ln -s libstdc++.so.6.0.20 libstdc++.so.6
#strings /usr/lib64/libstdc++.so.6 | grep GLIBCXX
ImportError: cannot import name pywrap_tensorflow
edit: The real fix for me was to updated setuptools to the latest and it allowed me to upgrade mock and six to the latest. I was on setuptools 3.3. In my case I also had to remove said modules by hand because they were owned by OS in ‘/usr/local/lib/python2.7/dist-packages/’
check versions of everything
pip freeze | grep -e six -e mock
easy_install –version
Update everything
wget
-O – | sudo python
pip install mock –upgrade
pip install six –upgrade
終于成功了。
>>> import tensorflow as tf
>>> hello = tf.constant(‘Hello, TensorFlow!’)
>>> sess = tf.Session()
>>> sess.run(hello)
Hello, TensorFlow!
>>> a = tf.constant(10)
>>> b = tf.constant(32)
>>> sess.run(a+b)
>>>
>>> print product
Tensor(“MatMul:0”, shape=(1, 1), dtype=float32)
>>> sess = tf.Session()
>>> result = sess.run(product)
>>> print result
>
>>> sess.close()
關(guān)于swig go linux的介紹到此就結(jié)束了,不知道你從中找到你需要的信息了嗎 ?如果你還想了解更多這方面的信息,記得收藏關(guān)注本站。
成都服務(wù)器托管選創(chuàng)新互聯(lián),先上架開通再付費。
創(chuàng)新互聯(lián)(www.cdcxhl.com)專業(yè)-網(wǎng)站建設(shè),軟件開發(fā)老牌服務(wù)商!微信小程序開發(fā),APP開發(fā),網(wǎng)站制作,網(wǎng)站營銷推廣服務(wù)眾多企業(yè)。電話:028-86922220
分享標題:使用SWIG在Go語言中使用Linux API (swig go linux)
網(wǎng)頁鏈接:http://m.fisionsoft.com.cn/article/cosehgd.html


咨詢
建站咨詢
