Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Kotlin/NativeからCの標準ライブラリを呼び出そう
Search
Yuta Tomiyama
December 04, 2021
Programming
480
0
Share
Kotlin/NativeからCの標準ライブラリを呼び出そう
2021/12/04 Piyogrammer Conference 2021にて発表
Yuta Tomiyama
December 04, 2021
More Decks by Yuta Tomiyama
See All by Yuta Tomiyama
ビルドプロセスをデバッグしよう!
yt8492
2
460
モバイルアプリ開発を始めよう!
yt8492
0
110
Git勉強会
yt8492
0
200
なんでもやってみる勇気
yt8492
0
130
Android Autoが思ったよりしんどい話
yt8492
0
240
apollo-kotlinにcontributeした話
yt8492
0
180
DMM TVのSDカードダウンロード機能を実装した話
yt8492
1
980
今だからこそ知りたいKotlin Multiplatform
yt8492
0
330
State management and API calls in Jetpack Compose: Learning Apollo + Jetpack Compose through React Hooks
yt8492
0
1.3k
Other Decks in Programming
See All in Programming
Agentic UI beyond Chats Architecture Patterns & Open Standards @ngMunich 05/2026
manfredsteyer
PRO
0
110
密結合なバックエンドから TypeScript のコードを生成する
kemuridama
1
270
Cloudflare で始める Data Platform
ta93abe
0
200
When benchmarks go bad - what I learned from measuring performance wrong
hollycummins
0
400
色即是空、空即是色、データサイエンス
kamoneggi
1
140
[BalkanRuby 2026] Drop your app/services!
palkan
3
610
Agentic UI in the Frontend: Architectures with Open Standards @JAX 2026 in Mainz
manfredsteyer
PRO
0
120
プラグインで拡張される Context をtype-safe にする難しさと設計判断
kazupon
2
220
開発とはなにか、Essenceカーネルで見えるもの
ukin0k0
0
200
AgentCore Optimizationを始めよう!
licux
3
260
Modding RubyKaigi for Myself
yui_knk
0
360
ソースコード→AST→オペコード、の旅を覗いてみる
o0h
PRO
1
140
Featured
See All Featured
The Illustrated Guide to Node.js - THAT Conference 2024
reverentgeek
1
350
SEOcharity - Dark patterns in SEO and UX: How to avoid them and build a more ethical web
sarafernandez
0
180
Evolving SEO for Evolving Search Engines
ryanjones
0
200
Digital Ethics as a Driver of Design Innovation
axbom
PRO
1
290
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
11
910
Navigating Weather and Climate Data
rabernat
0
190
Building Better People: How to give real-time feedback that sticks.
wjessup
370
20k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
25
1.9k
How to Ace a Technical Interview
jacobian
281
24k
Hiding What from Whom? A Critical Review of the History of Programming languages for Music
tomoyanonymous
2
810
How to Build an AI Search Optimization Roadmap - Criteria and Steps to Take #SEOIRL
aleyda
1
2k
Crafting Experiences
bethany
1
150
Transcript
Kotlin/Nativeから Cの標準ライブラリを呼び出そう 2021/12/04 Piyogrammer Conference
自己紹介 HN: マヤミト ID: yt8492 会津大学 学部4年 普段はAndroidアプリ書いたりサーバー書いたり 今回のイベントの運営でもあります GitHub:
https://github.com/yt8492 趣味: Kotlin, Twitter, ウマ娘 Twitter: yt8492
Kotlin/Nativeとは? - KotlinのコードをJavaのバイトコード以外にコンパイルする技術 - VMなしで実行可能なバイナリ - C/C++向けのライブラリとCヘッダ - Swift/Objective-C向けのApple framework
- 当然JavaのライブラリやJVMのみをターゲットとしたKotlinのライブラリは使えない 😇 - 目的のtargetに対応したKotlinのライブラリを使う - 目的のtargetの処理をブリッジを書いて Kotlinでラップする
Kotlin標準ライブラリでできることは意外と限られている - Javaの標準ライブラリが普通にKotlinから使えるおかげで普段意識しないが、実 はKotlinの標準ライブラリではファイル操作やソケット通信などは提供されていな い - kotlinx-ioというライブラリが公式で開発中だが、ファイル操作もソケット通信もまだできない 😇
KotlinからCの標準ライブラリを呼び出そう(本題) - fopenとかをKotlin/Nativeから呼び出せばいいじゃん! - とはいえ自力でブリッジを書くのはあまりにもしんどい - Kotlin/Native向けにplatform librariesという形でCの標準ライブラリの関数など をそのままKotlinから使えるようにしたラッパーが公式で提供されている
簡単な例
Cのprintfが呼び出せた🎉
とはいえそんなに単純に済む話じゃない - KotlinとCのそもそもの言語仕様の違い - Kotlinは変数の宣言と代入はセットだが、 Cは変数宣言だけしてポインタを関数に渡すなどができ る - KotlinはGCがあるけどCにはない -
Kotlinにポインタの概念はない - などなど
KotlinでCの変数のメモリ確保など - memScopedという関数に渡すブロックの中でallocなどの関数が使える - このスコープを抜けると確保したメモリは開放される - ポインタは ptr という拡張プロパティで取得できる
Cの値とポインタをKotlinで扱う - allocで確保できる型はCVariable型を継承した型のみ - Kotlinのプリミティブ型(Intなど)がallocできないじゃん! - プリミティブ型用にCVariable型を継承したIntVar型などが用意されている - CVariable型が継承しているCPointed型には、ptrという拡張プロパティが生えて いる
- ptrはCPointer型
Kotlin/NativeでTCPのソケット通信をecho backする例 - yt8492/NativeServer https://github.com/yt8492/NativeServer - Cのsocket関数などをKotlinでラップしてJavaのSocketまわりの標準ライブラリに近いものを自作 - それを使う形でecho backを実装
これでみなさんもCのコードをKotlinに 脳内変換できるようになりましたね?
みんなもKotlin/Native、しよう!