18.C_init_func_so

command
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 7, 2025 License: MIT Imports: 2 Imported by: 0

README

これは何?

cgoを利用して共有ファイルを生成した場合でも init() は、ちゃんと呼び出されるのかについてのサンプルです。

結論としては、「ちゃんと呼び出されている」ですが、以下の挙動となります。

つまり、CGOの境界に入ったときに init() が呼び出されるという挙動になります。

$ task
task: [default] go build -o libgo.so -buildmode=c-shared .
task: [default] gcc -c -o main.o main.c
task: [default] gcc -o capp main.o -L. -lgo
task: [default] ldd ./capp
        linux-vdso.so.1 (0x00007fff5778a000)
        libgo.so => ./libgo.so (0x00007ff4bda66000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007ff4bd836000)
        /lib64/ld-linux-x86-64.so.2 (0x00007ff4bdc1f000)
task: [default] ./capp
[C ] hello world
>> main.init() called
>> cfuncs.init() called
>> exports.init() called
[GO] gofunc1
[GO] map[cfuncs:true exports:true main:true]

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL