Skip to content

Instantly share code, notes, and snippets.

@oshimaya
Created December 3, 2015 17:21
Show Gist options
  • Save oshimaya/d2e0b6057dc553e22c69 to your computer and use it in GitHub Desktop.
Save oshimaya/d2e0b6057dc553e22c69 to your computer and use it in GitHub Desktop.
package main
// cgo test function
// build this:
// 1. go build -o cgotest1 cgotest.go
// 2. go build -o cgotest2 -ldflags='-linkmode=internal' cgotest.go
/*
#include <stdio.h>
__attribute__((constructor)) void cgofunc() {
printf("Run CGO function! \n");
}
*/
import "C"
import "time"
func main() {
time.Sleep(10*time.Second)
}
@oshimaya
Copy link
Author

oshimaya commented Dec 3, 2015

When linkmode is internal, C constructor is not executed...
$ ./cgotest1
Run CGO function!
$ ./cgotest2
$
Then, cgotest1 has the symbol 'cgofunc', but cgotest2 has none.
$ nm cgotest1 | grep cgofunc
0000000000436ca0 T cgofunc
$ nm cgotest2 | grep cgofunc
$

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment