Help us understand the problem. What is going on with this article?

Rust と C++17 のソースコードを混ぜてビルドする

More than 1 year has passed since last update.

Rust と C++17 のソースコードを混ぜてビルドする

動機

  • C や C++ で書かれたライブラリを利用しつつ、 Rust を書きたい
  • https://crates.io/crates/cc というライブラリを使う
  • cargo による rust のコンパイル時に C++ のコンパイルもいっしょにできる
  • これで C や C++ のライブラリの FFI をいちから手書きするのではなく、 C や C++ のライブラリを呼ぶ部分は C++ で書いておいて Rust から呼ぶということができる

導入

  • Cargo.toml に cc を追加する
Cargo.toml
[package]
name = "hello-rs"
version = "0.1.0"
links = "hello"
build = "build.rs"

[build-dependencies]
cc = { version = "1.0", features = ["parallel"] }

[dependencies]
libc = "0.2"

C++ のコードを書く

src/cpp/include/hello.h
#pragma once

#ifdef __cplusplus
extern "C" {
#endif

  void hello();
#ifdef __cplusplus

}
#endif
src/cpp/src/hello.cpp
#include <iostream>
#include <string>

void hello(){
  std::cout << "hello cpp" << std::endl;
}
  • 基本的には C API で書くので Rust から呼び出す関数は C の型を使う

Rust で C の FFI を書く

src/main.rs
extern crate libc;

use libc::{ c_void, c_char, size_t };
use std::ffi::{ CStr, CString };

#[link(name = "hello", kind = "static")]
extern "C" {
  fn hello();
}

fn main() {
  unsafe{
    hello();
  }
}

build.rs に C++ コードのビルド方法を記述する

build.rs
extern crate cc;

fn main(){
    println!("cargo:rustc-link-search=native=/usr/local/lib");
    cc::Build::new()
        .cpp(true)
        .warnings(true)
        .flag("-std=c++17")
        .flag("-Wall")
        .flag("-Wextra")
        .flag("-v")
        .flag("-g")
        .file("src/cpp/src/hello.cpp")
        .include("src/cpp/include")
        .compile("libhello.a");
}
  • flag はひとつづつ書く
  • 任意のバージョンの c コンパイラを使いたい場合は環境変数に指定する
    • env CXX=/usr/bin/g++-7 cargo build
    • env CXX=/usr/bin/g++-7 cargo run

参考

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした