Skip to content

Instantly share code, notes, and snippets.

@hnakamur hnakamur/hello.go forked from hatajoe/hello.go
Created Sep 28, 2015

Embed
What would you like to do?
`runtime/cgo: could not obtain pthread_keys' on darwin/amd64
// file: plugins/hello/hello.go
// $ go build -o libhello.so -buildmode=c-shared hello.go
package main
import "C"
import (
"log"
)
//export Hello
func Hello() int {
log.Println("Hello")
return 200
}
func init() {
log.Println("loaded")
}
func main() {
}
// file: main.go
package main
import "C"
import (
"bitbucket.org/binet/go-ffi/pkg/ffi"
"fmt"
)
func main() {
lib, err := ffi.NewLibrary("./plugins/hello/libhello.so")
if err != nil {
panic(err)
}
defer lib.Close()
fn, err := lib.Fct("Hello", ffi.Int, []ffi.Type{})
if err != nil {
panic(err)
}
fmt.Printf("%#v\n", fn().Int())
}
@hnakamur

This comment has been minimized.

Copy link
Owner Author

hnakamur commented Sep 28, 2015

$ go get -u bitbucket.org/binet/go-ffi/pkg/ffi/...
$ (cd plugins/hello; go build)
$ PKG_CONFIG_PATH=/usr/local/Cellar/libffi/3.0.13/lib/pkgconfig go build
$ find .
.
./go_dll
./main.go
./plugins
./plugins/hello
./plugins/hello/hello.go
./plugins/hello/libhello.h
./plugins/hello/libhello.so
$ ./go_dll
runtime/cgo: could not obtain pthread_keys
        tried 0x101 0x102 0x103 0x104 0x105 0x106 0x107 0x108 0x109 0x10a 0x10b 0x10c 0x10d 0x10e 0x10f 0x110 0x111 0x112 0x113 0x115 0x116 0x117 0x118 0x119 0x11a 0x11b 0x11c 0x11d 0x11e 0x11f 0x120 0x121 0x122 0x123 0x124 0x125 0x126 0x127 0x128 0x129 0x12a 0x12b 0x12c 0x12d 0x12e 0x12f 0x130 0x131 0x132 0x133 0x134 0x135 0x136 0x137 0x138 0x139 0x13a 0x13b 0x13c 0x13d 0x13e 0x13f 0x140 0x141 0x142 0x143 0x144 0x145 0x146 0x147 0x148 0x149 0x14a 0x14b 0x14c 0x14d 0x14e 0x14f 0x150 0x151 0x152 0x153 0x154 0x155 0x156 0x157 0x158 0x159 0x15a 0x15b 0x15c 0x15d 0x15e 0x15f 0x160 0x161 0x162 0x163 0x164 0x165 0x166 0x167 0x168 0x169 0x16a 0x16b 0x16c 0x16d 0x16e 0x16f 0x170 0x171 0x172 0x173 0x174 0x175 0x176 0x177 0x178 0x179 0x17a 0x17b 0x17c 0x17d 0x17e 0x17f 0x180 0x181
Abort trap: 6
$ uname -s
Darwin
$ sw_vers -productVersion
10.10.5
$ go version
go version go1.5.1 darwin/amd64
$ brew info libffi
libffi: stable 3.0.13 (bottled), HEAD
Portable Foreign Function Interface library
https://sourceware.org/libffi/

This formula is keg-only.
Some formulae require a newer version of libffi.

/usr/local/Cellar/libffi/3.0.13 (14 files, 412K)
  Poured from bottle
From: https://github.com/Homebrew/homebrew/blob/master/Library/Formula/libffi.rb
==> Caveats
This formula is keg-only, which means it was not symlinked into /usr/local.

Some formulae require a newer version of libffi.

Generally there are no consequences of this for you. If you build your
own software and it requires this formula, you'll need to add to your
build variables:

    LDFLAGS:  -L/usr/local/opt/libffi/lib

@hnakamur

This comment has been minimized.

Copy link
Owner Author

hnakamur commented Sep 28, 2015

$ python
Python 2.7.10 (default, Jul 13 2015, 12:05:58)
[GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import ctypes
>>> lib = ctypes.CDLL('./plugins/hello/libhello.so')
>>> 2015/09/28 22:04:09 loaded

>>> lib.Hello()
2015/09/28 22:04:16 Hello
200
>>>
@hnakamur

This comment has been minimized.

Copy link
Owner Author

hnakamur commented Sep 28, 2015

Added debug prints to main.go.

// file: main.go
package main

import "C"

import (
    "fmt"

    "bitbucket.org/binet/go-ffi/pkg/ffi"
)

func main() {
    fmt.Printf("main start\n")
    lib, err := ffi.NewLibrary("./plugins/hello/libhello.so")
    fmt.Printf("lib=%v\n", lib)
    if err != nil {
        panic(err)
    }
    defer lib.Close()

    fn, err := lib.Fct("Hello", ffi.Int, []ffi.Type{})
    if err != nil {
        panic(err)
    }
    fmt.Printf("fn=%v\n", fn)
    fmt.Printf("%#v\n", fn().Int())
}
$ PKG_CONFIG_PATH=/usr/local/Cellar/libffi/3.0.13/lib/pkgconfig go build
$ ./go_dll
main start
runtime/cgo: could not obtain pthread_keys
        tried 0x101 0x102 0x103 0x104 0x105 0x106 0x107 0x108 0x109 0x10a 0x10b 0x10c 0x10d 0x10e 0x10f 0x110 0x111 0x112 0x113 0x115 0x116 0x117 0x118 0x119 0x11a 0x11b 0x11c 0x11d 0x11e 0x11f 0x120 0x121 0x122 0x123 0x124 0x125 0x126 0x127 0x128 0x129 0x12a 0x12b 0x12c 0x12d 0x12e 0x12f 0x130 0x131 0x132 0x133 0x134lib={{0x4600020}}
 0x135 0x136 0x137 0x138 0x139 0x13a 0x13b 0x13c 0x13d 0x13e 0x13f 0x140 0x141 0x142 0x143 0x144 0x145 0x146 0x147 0x148 0x149 0x14a 0x14b 0x14c 0x14dfn=0x4074100
 0x14e 0x14f 0x150 0x151 0x152 0x153 0x154 0x155 0x156 0x157 0x158 0x159 0x15a 0x15b 0x15c 0x15d 0x15e 0x15f 0x160 0x161 0x162 0x163 0x164 0x165 0x166 0x167 0x168 0x169 0x16a 0x16b 0x16c 0x16d 0x16e 0x16f 0x170 0x171 0x172 0x173 0x174 0x175 0x176 0x177 0x178 0x179 0x17a 0x17b 0x17c 0x17d 0x17e 0x17f 0x180 0x181
Abort trap: 6
@hnakamur

This comment has been minimized.

Copy link
Owner Author

hnakamur commented Sep 28, 2015

Change the relative path of the library to just a filename in main.go

// file: main.go
package main

import "C"

import (
    "fmt"

    "bitbucket.org/binet/go-ffi/pkg/ffi"
)

func main() {
    fmt.Printf("main start\n")
    lib, err := ffi.NewLibrary("libhello.so")
    fmt.Printf("lib=%v\n", lib)
    if err != nil {
        panic(err)
    }
    defer lib.Close()

    fn, err := lib.Fct("Hello", ffi.Int, []ffi.Type{})
    if err != nil {
        panic(err)
    }
    fmt.Printf("fn=%v\n", fn)
    fmt.Printf("%#v\n", fn().Int())
}
$ PKG_CONFIG_PATH=/usr/local/Cellar/libffi/3.0.13/lib/pkgconfig go build
$ ./go_dll
main start
lib={{<nil>}}
panic: dl: dlopen(libhello.so, 2): image not found

goroutine 1 [running]:
main.main()
        /Users/hnakamur/sandbox/go_dll/main.go:17 +0x167

goroutine 17 [syscall, locked to thread]:
runtime.goexit()
        /usr/local/go/src/runtime/asm_amd64.s:1696 +0x1
$ DYLD_LIBRARY_PATH=plugins/hello ./go_dll
main start
lib={{0x5100000}}
fn=0x4074120
runtime/cgo: could not obtain pthread_keys
        tried 0x101 0x102 0x103 0x104 0x105 0x106 0x107 0x108 0x109 0x10a 0x10b 0x10c 0x10d 0x10e 0x10f 0x110 0x111 0x112 0x113 0x115 0x116 0x117 0x118 0x119 0x11a 0x11b 0x11c 0x11d 0x11e 0x11f 0x120 0x121 0x122 0x123 0x124 0x125 0x126 0x127 0x128 0x129 0x12a 0x12b 0x12c 0x12d 0x12e 0x12f 0x130 0x131 0x132 0x133 0x134 0x135 0x136 0x137 0x138 0x139 0x13a 0x13b 0x13c 0x13d 0x13e 0x13f 0x140 0x141 0x142 0x143 0x144 0x145 0x146 0x147 0x148 0x149 0x14a 0x14b 0x14c 0x14d 0x14e 0x14f 0x150 0x151 0x152 0x153 0x154 0x155 0x156 0x157 0x158 0x159 0x15a 0x15b 0x15c 0x15d 0x15e 0x15f 0x160 0x161 0x162 0x163 0x164 0x165 0x166 0x167 0x168 0x169 0x16a 0x16b 0x16c 0x16d 0x16e 0x16f 0x170 0x171 0x172 0x173 0x174 0x175 0x176 0x177 0x178 0x179 0x17a 0x17b 0x17c 0x17d 0x17e 0x17f 0x180 0x181
Abort trap: 6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.