1
0

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 3 years have passed since last update.

Using cgo with the go command

Last updated at Posted at 2021-01-06

#GoからC/C++を扱う

##とりあえずここ読む
Using cgo with the go command

##Cのコードが認識されない
//とimportの間に空行が入ってると前文(preamble)として認識されないようだ。

c.go
// #include <stdlib.h>
import "C"

##.so(SharedLibrary)でDllMain的なモノはないのか?
Attach相当はGoのinitが使えるけど、Detach相当がない。
ってことで、gccの属性使ってみたらできた。

so.go
// static void _attribute__((constructor)) constructor() {
//     // Attach相当処理
// }
//
// static void __attribute__((destructor)) destructor() {
//     // Detach相当処理
// }
import "C"

なぜか2回ずつ呼ばれる気がする…。Go側でsync.Onceかましてごまかすw

1
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?