Cgo and native binding

352 views
Skip to first unread message

christkv

unread,
Dec 20, 2010, 1:12:34 PM12/20/10
to golang-nuts
Hi

Does anyone have an example of a c library integration that actually
compiles and works with osx 10.6. I have been trying to get golua to
work but have given up and right now I'm just looking for a single
project that actually compiles and runs.

:(

Attila Tajti

unread,
Dec 20, 2010, 4:10:39 PM12/20/10
to christkv, golang-nuts
I thought I have a simple project for you at http://github.com/tajtiattila/basicsound.go but apparenlty it does not work with the current release. The package builds and installs, but gopack and 6l fails with relocation errors:

[basicsound.go] (master) $ make test
gotest
rm -f _test/basicsound.a _gotest_.6
6g -o _gotest_.6  basicsound_darwin.cgo1.go _cgo_gotypes.go basicsound_test.go
rm -f _test/basicsound.a
gopack grc _test/basicsound.a _gotest_.6  _cgo_defun.6 _cgo_import.6 lib_darwin.o basicsound_darwin.cgo2.o _cgo_export.o
_test/basicsound.a(lib_darwin.o)(__DATA/__objc_classrefs): unexpected reloc for dynamic symbol OBJC_CLASS_$_NSThread
_test/basicsound.a(lib_darwin.o)(__DATA/__objc_classrefs): unexpected reloc for dynamic symbol OBJC_CLASS_$_NSMachPort
_test/basicsound.a(lib_darwin.o)(__DATA/__objc_classrefs): unexpected reloc for dynamic symbol OBJC_CLASS_$_NSRunLoop
_test/basicsound.a(lib_darwin.o)(__DATA/__objc_classrefs): unexpected reloc for dynamic symbol OBJC_CLASS_$_NSAutoreleasePool
_test/basicsound.a(lib_darwin.o)(__DATA/__objc_classrefs): unexpected reloc for dynamic symbol OBJC_CLASS_$_NSSound
_test/basicsound.a(lib_darwin.o)(__DATA/__objc_classrefs): unexpected reloc for dynamic symbol OBJC_CLASS_$_NSString
_test/basicsound.a(lib_darwin.o)(__DATA/__objc_data): unexpected reloc for dynamic symbol OBJC_METACLASS_$_NSObject
_test/basicsound.a(lib_darwin.o)(__DATA/__objc_data): unexpected reloc for dynamic symbol OBJC_METACLASS_$_NSObject
_test/basicsound.a(lib_darwin.o)(__DATA/__objc_data): unexpected reloc for dynamic symbol _objc_empty_cache
_test/basicsound.a(lib_darwin.o)(__DATA/__objc_data): unexpected reloc for dynamic symbol _objc_empty_vtable
_test/basicsound.a(lib_darwin.o)(__DATA/__objc_data): unexpected reloc for dynamic symbol OBJC_CLASS_$_NSObject
_test/basicsound.a(lib_darwin.o)(__DATA/__objc_data): unexpected reloc for dynamic symbol _objc_empty_cache
_test/basicsound.a(lib_darwin.o)(__DATA/__objc_data): unexpected reloc for dynamic symbol _objc_empty_vtable
_test/basicsound.a(lib_darwin.o)(__DATA/__objc_msgrefs): unexpected reloc for dynamic symbol objc_msgSend_fixup
_test/basicsound.a(lib_darwin.o)(__DATA/__objc_msgrefs): unexpected reloc for dynamic symbol objc_msgSend_fixup
_test/basicsound.a(lib_darwin.o)(__DATA/__objc_msgrefs): unhandled relocation for objc_msgSend_fixup (type 15 rtype 120)
_test/basicsound.a(lib_darwin.o)(__DATA/__objc_msgrefs): unhandled relocation for objc_msgSend_fixup (type 15 rtype 120)
_test/basicsound.a(lib_darwin.o)(__DATA/__objc_classrefs): unhandled relocation for OBJC_CLASS_$_NSThread (type 15 rtype 120)
_test/basicsound.a(lib_darwin.o)(__DATA/__objc_classrefs): unhandled relocation for OBJC_CLASS_$_NSMachPort (type 15 rtype 120)
_test/basicsound.a(lib_darwin.o)(__DATA/__objc_classrefs): unhandled relocation for OBJC_CLASS_$_NSRunLoop (type 15 rtype 120)
_test/basicsound.a(lib_darwin.o)(__DATA/__objc_classrefs): unhandled relocation for OBJC_CLASS_$_NSAutoreleasePool (type 15 rtype 120)
too many errors
make: *** [test] Error 1
[basicsound.go] (master) $



-- Attila

christkv

unread,
Dec 20, 2010, 4:35:20 PM12/20/10
to golang-nuts
Any idea what the last stable release of go was you used ? Planning to
roll back but have no idea which tag works :(

Thanks for the help so far though :)

On Dec 20, 10:10 pm, Attila Tajti <attila.ta...@gmail.com> wrote:
> I thought I have a simple project for you athttp://github.com/tajtiattila/basicsound.gobut apparenlty it does not work with the current release. The package builds and installs, but gopack and 6l fails with relocation errors:

christkv

unread,
Dec 20, 2010, 4:37:45 PM12/20/10
to golang-nuts
With the latest go relese build I get

CGOPKGPATH= cgo -- basicsound_darwin.go
8g -o _go_.8 basicsound_darwin.cgo1.go _cgo_gotypes.go
8c -FVw -I"/Users/christian.kvalheim/coding/languages/go/src/pkg/
runtime" _cgo_defun.c
echo 'int main() { return 0; }' >_cgo_main.c
gcc -m32 -g -fPIC -O2 -o _cgo_main.o -c _cgo_main.c
gcc -m32 -g -fPIC -O2 -o basicsound_darwin.cgo2.o -c
basicsound_darwin.cgo2.c
gcc -m32 -g -fPIC -O2 -o _cgo1_.o _cgo_main.o basicsound_darwin.cgo2.o
lib_darwin.o -framework Cocoa
i686-apple-darwin10-gcc-4.2.1: lib_darwin.o: No such file or directory
make: *** [_cgo1_.o] Error 1

with you library

On Dec 20, 10:35 pm, christkv <chris...@gmail.com> wrote:
> Any idea what the last stable release of go was you used ? Planning to
> roll back but have no idea which tag works :(
>
> Thanks for the help so far though :)
>
> On Dec 20, 10:10 pm, Attila Tajti <attila.ta...@gmail.com> wrote:
>
>
>
>
>
>
>
> > I thought I have a simple project for you athttp://github.com/tajtiattila/basicsound.gobutapparenlty it does not work with the current release. The package builds and installs, but gopack and 6l fails with relocation errors:

fango

unread,
Dec 20, 2010, 6:41:50 PM12/20/10
to golang-nuts

Mark A. R. Gerads

unread,
Dec 20, 2010, 9:59:01 PM12/20/10
to golang-nuts
Try updating to tip. I had a similar problem, but it was fine after I
updated to tip. The Cgo in the current release is trash, but the Cgo
in the tip is pristine. Seems a bit backwards to me.

Steven

unread,
Dec 20, 2010, 10:16:51 PM12/20/10
to Mark A. R. Gerads, golang-nuts
This is always going to be the case at some point. Whenever an issue makes it into a release uncaught, there will be a period of time between when the fix is submitted and when it "ships" where the only way to get the fix will be to get a between-release version.

Now, if it were broken again for the release, then that would be the beginning of an slightly ironic pattern ;-)

Attila Tajti

unread,
Dec 21, 2010, 1:24:41 AM12/21/10
to christkv, golang-nuts
On 20 Dec 2010, at 22:37, christkv wrote:

> With the latest go relese build I get
>
> CGOPKGPATH= cgo -- basicsound_darwin.go
> 8g -o _go_.8 basicsound_darwin.cgo1.go _cgo_gotypes.go
> 8c -FVw -I"/Users/christian.kvalheim/coding/languages/go/src/pkg/
> runtime" _cgo_defun.c
> echo 'int main() { return 0; }' >_cgo_main.c
> gcc -m32 -g -fPIC -O2 -o _cgo_main.o -c _cgo_main.c
> gcc -m32 -g -fPIC -O2 -o basicsound_darwin.cgo2.o -c
> basicsound_darwin.cgo2.c
> gcc -m32 -g -fPIC -O2 -o _cgo1_.o _cgo_main.o basicsound_darwin.cgo2.o
> lib_darwin.o -framework Cocoa
> i686-apple-darwin10-gcc-4.2.1: lib_darwin.o: No such file or directory
> make: *** [_cgo1_.o] Error 1

I had the very same errors after your original message (with 9e27e0a), but fixing the Makefiles solved the build problem (with master at b1ff03f). I do not understand how did you get the old version though, because I sent my first email after pushing the changes. Anyway "make install" now works, but "make test" fails during linking with the aforementioned error. I tried to "hg update tip/release/default", but none of them worked.

This seems to be related to modules using -framework only, as I have no problem building the samles stdio or life in $GOROOT/misc/cgo.

I wonder if it is a know problem or I should try to find the offending changeset with hg bisect.

--
Attila

christkv

unread,
Dec 21, 2010, 4:27:53 AM12/21/10
to golang-nuts
hmm

I still get

CGOPKGPATH= cgo -- basicsound_darwin.go
8g -o _go_.8 basicsound_darwin.cgo1.go _cgo_gotypes.go
8c -FVw -I"/Users/christian.kvalheim/coding/languages/go/src/pkg/
runtime" _cgo_defun.c
echo 'int main() { return 0; }' >_cgo_main.c
gcc -m32 -g -fPIC -O2 -o _cgo_main.o -c _cgo_main.c
gcc -m32 -g -fPIC -O2 -o basicsound_darwin.cgo2.o -c
basicsound_darwin.cgo2.c
gcc -m32 -g -fPIC -O2 -o _cgo1_.o _cgo_main.o basicsound_darwin.cgo2.o
-framework Cocoa
Undefined symbols:
"_basicsound_init", referenced from:
__cgo_69997af0e2e9_Cfunc_basicsound_init in
basicsound_darwin.cgo2.o
(maybe you meant: __cgo_69997af0e2e9_Cfunc_basicsound_init)
"_basicsound_play", referenced from:
__cgo_69997af0e2e9_Cfunc_basicsound_play in
basicsound_darwin.cgo2.o
(maybe you meant: __cgo_69997af0e2e9_Cfunc_basicsound_play)
"_basicsound_stop", referenced from:
__cgo_69997af0e2e9_Cfunc_basicsound_stop in
basicsound_darwin.cgo2.o
(maybe you meant: __cgo_69997af0e2e9_Cfunc_basicsound_stop)
ld: symbol(s) not found
collect2: ld returned 1 exit status
make: *** [_cgo1_.o] Error 1

I'm on osX 10.6.5
gcc is version i686-apple-darwin10-gcc-4.2.1 (GCC) 4.2.1 (Apple Inc.
build 5664)
go is at

parent: 6980:61b2c52b0d22 tip
release.2010-12-15.1

What's your environment ? This got to be a weird combination of
incompatible versions.

Thanks for all the help though mate.

jnml

unread,
Dec 21, 2010, 5:11:28 AM12/21/10
to golang-nuts
On 21 pro, 10:27, christkv <chris...@gmail.com> wrote:
> go is at
>
> parent: 6980:61b2c52b0d22 tip
>  release.2010-12-15.1
This looks like a release version, not the tip. Try something like:

$ cd ~/go
$ hg pull
$ hg up tip
$ hg id
f490039d1eb7 tip
$

Attila Tajti

unread,
Dec 21, 2010, 5:14:06 AM12/21/10
to christkv, golang-nuts
This might be because of GOARCH=386. So far I have used only GOARCH=amd64. I recompiled go now with GOARCH=386, and I get the same problem, because $(HOST_CC) seems to emit 64-bit code by default. Adding -m32 did not help unfortunately, resulting in the output below.

Btw I have the same gcc build as you (5664). I was able to use Go release.2010-11-02 and release.2010-11-23 with my newly created "compat" branch, with which even make test worked (ie. everything is fine). 

The difference between the current and the older Go releases from our aspect seem to be that the older ones built an extra shared library by default in addition to the library archive. The change is a good thing IMO (I prefer not to have a separate shared object) but does not seem to work with OSX frameworks. I suspect there must be a way to enable the shared object Cgo behavior of old releases somehow.

My GOARCH=386 output with $(HOST_CC) -m32 in Makefile.darwin:

[basicsound.go] (master) $ make

CGOPKGPATH= cgo --  basicsound_darwin.go
8g -o _go_.8  basicsound_darwin.cgo1.go _cgo_gotypes.go
8c -FVw -I"/Users/ata/Scrap/go/src/pkg/runtime" _cgo_defun.c

gcc -m32 -g -fPIC -O2 -o _cgo_main.o -c  _cgo_main.c
gcc -m32 -c -fPIC -o lib_darwin.o lib_darwin.m

gcc -m32 -g -fPIC -O2 -o basicsound_darwin.cgo2.o -c  basicsound_darwin.cgo2.c
gcc -m32 -g -fPIC -O2 -o _cgo_export.o -c  _cgo_export.c
gcc -m32 -g -fPIC -O2 -o _cgo1_.o _cgo_main.o lib_darwin.o basicsound_darwin.cgo2.o _cgo_export.o -framework Cocoa
cgo -dynimport _cgo1_.o >__cgo_import.c && mv -f __cgo_import.c _cgo_import.c
8c -FVw _cgo_import.c
_cgo_import.c:1 usage: #pragma dynimport local remote "path"
_cgo_import.c:2 usage: #pragma dynimport local remote "path"
_cgo_import.c:3 usage: #pragma dynimport local remote "path"
_cgo_import.c:4 usage: #pragma dynimport local remote "path"
_cgo_import.c:5 usage: #pragma dynimport local remote "path"
_cgo_import.c:6 usage: #pragma dynimport local remote "path"
_cgo_import.c:7 usage: #pragma dynimport local remote "path"
make: *** [_cgo_import.8] Error 1
[basicsound.go] (master) $ cat _cgo_import.c 
#pragma dynimport .objc_class_name_NSAutoreleasePool .objc_class_name_NSAutoreleasePool ""
#pragma dynimport .objc_class_name_NSMachPort .objc_class_name_NSMachPort ""
#pragma dynimport .objc_class_name_NSObject .objc_class_name_NSObject ""
#pragma dynimport .objc_class_name_NSRunLoop .objc_class_name_NSRunLoop ""
#pragma dynimport .objc_class_name_NSSound .objc_class_name_NSSound ""
#pragma dynimport .objc_class_name_NSString .objc_class_name_NSString ""
#pragma dynimport .objc_class_name_NSThread .objc_class_name_NSThread ""
#pragma dynimport CFRunLoopRun CFRunLoopRun ""
#pragma dynimport NSDefaultRunLoopMode NSDefaultRunLoopMode ""
#pragma dynimport exit exit ""
#pragma dynimport objc_msgSend objc_msgSend ""
#pragma dynimport objc_msgSendSuper objc_msgSendSuper ""
#pragma dynimport dyld_stub_binder dyld_stub_binder ""
#pragma dynimport _ _ "/System/Library/Frameworks/Cocoa.framework/Versions/A/Cocoa"
#pragma dynimport _ _ "/usr/lib/libSystem.B.dylib"
#pragma dynimport _ _ "/usr/lib/libobjc.A.dylib"
#pragma dynimport _ _ "/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation"
#pragma dynimport _ _ "/System/Library/Frameworks/Foundation.framework/Versions/C/Foundation"
#pragma dynimport _ _ "/System/Library/Frameworks/AppKit.framework/Versions/C/AppKit"
[basicsound.go] (master) $ 



-- Attila

christkv

unread,
Dec 21, 2010, 8:19:30 AM12/21/10
to golang-nuts
Well now I'm getting this (branch compat) (go release.2010-11-23).

make test
gotest
rm -f _test/basicsound.a _gotest_.6
6g -o _gotest_.6 basicsound_darwin.cgo1.go _cgo_gotypes.go
basicsound_test.go
rm -f _test/basicsound.a
gopack grc _test/basicsound.a _gotest_.6 _cgo_defun.6
gcc -m64 -fPIC -O2 -o basicsound_darwin.cgo2.o -c
basicsound_darwin.cgo2.c
gcc -m64 -o cgo_basicsound.so basicsound_darwin.cgo2.o -framework
Cocoa -dynamiclib -Wl,-undefined,dynamic_lookup
dyld: lazy symbol binding failed: Symbol not found: _basicsound_init
Referenced from: /Users/christian.kvalheim/coding/checkouts/
basicsound.go/cgo_basicsound.so
Expected in: flat namespace

dyld: Symbol not found: _basicsound_init
Referenced from: /Users/christian.kvalheim/coding/checkouts/
basicsound.go/cgo_basicsound.so
Expected in: flat namespace

SIGTRAP: trace trap
pc: 0x7fff5fc01065


goroutine 1 [2]:
runtime.entersyscall+0x28 /Users/christian.kvalheim/coding/languages/
go/src/pkg/runtime/proc.c:567
runtime.entersyscall()
runtime.cgocall+0x9c /Users/christian.kvalheim/coding/languages/go/src/
pkg/runtime/cgocall.c:38
runtime.cgocall(0x33cea0, 0x605080, 0x5c610, 0x12df9)
basicsound._Cfunc_basicsound_init+0x32 /Users/christian.kvalheim/
coding/checkouts/basicsound.go/_cgo_defun.c:51
basicsound._Cfunc_basicsound_init(0x12f2d, 0x1ccd)
basicsound.init·1+0x18 /Users/christian.kvalheim/coding/checkouts/
basicsound.go/basicsound_darwin.cgo1.go:-44
basicsound.init·1()
basicsound.Init·+0x4a /Users/christian.kvalheim/coding/checkouts/
basicsound.go/basicsound_darwin.cgo1.go:2
basicsound.Init·()
main.init+0x4d /Users/christian.kvalheim/coding/checkouts/
basicsound.go/_testmain.go:15
main.init()
runtime.mainstart+0x5 /Users/christian.kvalheim/coding/languages/go/
src/pkg/runtime/amd64/asm.s:75
runtime.mainstart()
runtime.goexit /Users/christian.kvalheim/coding/languages/go/src/pkg/
runtime/proc.c:146
runtime.goexit()
rax 0x9f
rbx 0x410d70
rcx 0xfefefefefefefeff
rdx 0x360
rdi 0x7fff5fc400a0
rsi 0x0
rbp 0x7fff5fbff320
rsp 0x7fff5fbff308
r8 0x8080808080808080
r9 0xff096462606f7264
r10 0x7fff5fc400a0
r11 0x8080808080808080
r12 0x33cea0
r13 0x605080
r14 0x0
r15 0x0
rip 0x7fff5fc01065
rflags 0x202
cs 0x27
fs 0x10
gs 0x48
make: *** [test] Error 2

Attila Tajti

unread,
Dec 21, 2010, 8:51:24 AM12/21/10
to christkv, golang-nuts
Does it happens also if you do

make clean && make install && make test

? If it is still broken, then I am sorry, but I have no idea what is still wrong. Also, I found that the LLVM compiler suite may have an Objective-C precompiler that emits C code. Perhaps it could be used to build Go libraries that need to interface with Objective-C code [1].

If you still have problems I would suggest to try gosqlite or an plain C/cgo project, they seem to work with Go tip as well.


-- Attila

christkv

unread,
Dec 21, 2010, 9:08:02 AM12/21/10
to golang-nuts
One last question :) Are you using homebrew or macports ? Could be an
issue with these from my side.

Cheers

Christian

On Dec 21, 2:51 pm, Attila Tajti <attila.ta...@gmail.com> wrote:
> Does it happens also if you do
>
> make clean && make install && make test
>
> ? If it is still broken, then I am sorry, but I have no idea what is still wrong. Also, I found that the LLVM compiler suite may have an Objective-C precompiler that emits C code. Perhaps it could be used to build Go libraries that need to interface with Objective-C code [1].
>
> If you still have problems I would suggest to try gosqlite or an plain C/cgo project, they seem to work with Go tip as well.
>
> [1]http://stackoverflow.com/questions/3357326/objective-c-preprocessor-a...

Attila Tajti

unread,
Dec 21, 2010, 9:14:46 AM12/21/10
to christkv, golang-nuts

On 21 Dec 2010, at 15:08, christkv wrote:

One last question :) Are you using homebrew or macports ? Could be an
issue with these from my side.

Hmm. I have only Homebrew installed, but I do not think I have anything relevant:

$ port
-bash: port: command not found
$ brew list
ack flac lame libvorbis sqlite
atomicparsley gdbm libmikmod pcre synergy
bash-completion git libogg pkg-config wget
cmake glew libpng readline

-- Attila

Russ Cox

unread,
Jan 4, 2011, 5:14:59 PM1/4/11
to Attila Tajti, christkv, golang-nuts
> _cgo_import.c:1 usage: #pragma dynimport local remote "path"

This happens if you are running a new cgo
and and old 8c.

ls -l $(which cgo 8c)

and I think you will find that there is a stale 8c on your $PATH.

Russ

Reply all
Reply to author
Forward
0 new messages