LoginSignup
28
20

More than 5 years have passed since last update.

Cargoで任意の共有ライブラリをリンクするには

Last updated at Posted at 2015-07-15

背景

#[link(name = "raiburari")]
extern {
   fn foo(script: *const libc::c_char,
                        cb: extern fn(* const libc::c_char) -> c_int ) }

rustcのコマンドラインでは-Lオプションがあるので、Cと同様に共有ライブラリの
パスを指定することが可能。コード中で指定したlibraiburariがどこにあっても参照できる。

しかし、Cargoを使ってこれをするやり方がなかなか分からなかった。

驚きの結果

なんと、build.rsをいうファイルを作成して、rustで標準出力にオプションの
文字列を出力するコードを書くことで、これが可能になることが分かった。

共有ライブラリの検索パスは以下のオプション

cargo:rustc-link-search

build.rsの中身

fn main() {
    println!("cargo:rustc-link-search=native=/hoge/fuga/bar/foo");
}

これで、/hoge/fuga/bar/foo配下の共有ライブラリを参照するようになる。

参考資料

この事を学んだ結果の成果物

関連投稿

関連記事

28
20
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
28
20