1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Paiza.ioでGo言語で独自の共有ライブラリを使うには

Last updated at Posted at 2015-02-28

Paiza.ioに用意されていない共有ライブラリを使う準備

使いたい共有ライブラリを作る

linux,amd64であればOK

ネットから落とせるようにする

これをプログラム実行時DLすれば良い。

フツーにCgoで共有ライブラリを扱う際の問題

Paiza.io側に存在しない共有ライブラリを使いたい場合、
cgoでフツーに書くことが出来ない。

フツーの共有ライブラリの使い方

// #cgo LDFLAGS: -L. -lhoge

これだと、リンカが動く際に使いたい共有ライブラリlibhoge.soが
存在しないと動かない。

でも、このlibhoge.soは動かそうとしているGo言語のソースが動いて
初めてダウンロードされ、ローカルから参照可能になる。

dlopenすればいい

例えば、

int foo(char *p)

なる関数が、libhoge.soにあり、これを使いたい場合、
以下の様なコードで動かせる。

/*
# include <dlfcn.h>
static void my_dlopen(char *param) {
    void *handle;
    int (*func)(char*) = dlsym(handle, "foo");
    (*func)(param);
    dlclose(handle);
}
*/
import "C"

注意点

1秒以内にDLから実行までが完了しないと時間切れになり、
実行結果が得られない。

関連投稿

関連記事

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?