5

I am fairly new to go and even Linux in general.

I have built an app in a Linux environment which makes use of a gtk lib based on cgo (https://github.com/mattn/go-gtk/). The application builds fine in its native environment (linux 64bit) but when I try to compile for darwin 64bit I get the following result:

# net
could not determine kind of name for C.AI_MASK
# net
could not determine kind of name for C.AI_MASK

The command line I use to build:

env GOOS=$1 GOARCH=$2 CGO_ENABLED=1 go build $3

Where $1 is darwin and $2 amd64 (and $3 the path to my app).

As the error seems to come from the lib I import, I am not sure what to do to fix it. I have also read that cross compiling cgo does not really work as it relies on native macos stuff so it would need to be built on a mac. Is this true or is there something I can do to make it work in my environment?

I am also slightly confused as it seems most people discussing this subject are talking of go pre 1.5 which was entirely different when it comes to cross-compiling if I understand correctly.

Thanks

Nicolas
  • 403
  • 1
  • 6
  • 18
  • 1
    Do you have clang or gcc setup properly to cross-compile? – JimB Dec 04 '15 at 15:49
  • 1
    I am not sure, what does "setup properly" mean and how can I check? – Nicolas Dec 07 '15 at 09:19
  • 2
    You need to be able to cross-compile a C program in order to cross-compile a go program that uses cgo; go can't do that for you. Once you have the setup, you can set the proper env variables to tell go which compiler to call for the requested target: https://golang.org/cmd/cgo/ – JimB Dec 07 '15 at 15:26
  • 1
    I don't get it. I have gcc and clang installed (not sure what "setup" is required) and I am able to compile my program for linux 386 ("native") so doesn't that mean it compiles the C code successfully? I don't understand why the compilation of the C code is different (and fails) when the target is darwin. – Nicolas Dec 21 '15 at 08:59
  • 3
    Simply having a C compiler isn't enough to target another OS/architecture; you need all the associated headers and libraries compiled for the target as well. Compiling to darwin from Linux isn't a very common task, but I did see a GH repo someone built with tooling to help: github.com/tpoechtrager/osxcross. – JimB Dec 21 '15 at 16:25
  • Thanks for this - it seems to be what I need. I have successfully installed the toolchain following the instructions (phew!) but when I try to compile my program even for linux 64 ("native") I get the following error: `/usr/local/go/pkg/tool/linux_amd64/link: running o64-clang failed: exit status 1 ld: library not found for -lgobject-2.0 clang: error: linker command failed with exit code 1 (use -v to see invocation)` Not sure what that means... :/ – Nicolas Dec 24 '15 at 09:30
  • Ok I realized maybe it doesn't make sense to try to use that to compile for linux so now when I target darwin amd64 I have the following errors: `# github.com/mattn/go-gtk/pango ld: library not found for -lpango-1.0 clang: error: linker command failed with exit code 1 (use -v to see invocation)` and `# github.com/mattn/go-gtk/glib ld: library not found for -lgobject-2.0 clang: error: linker command failed with exit code 1 (use -v to see invocation)` ; what does that mean? – Nicolas Dec 24 '15 at 09:43

1 Answers1

6

I am now able to compile successfully my code on linux for darwin thanks to the comments by JimB.

What I needed was a osx toolchain such as github.com/tpoechtrager/osxcross.

Then I compiled my code by doing env OSXCROSS_NO_INCLUDE_PATH_WARNINGS=1 MACOSX_DEPLOYMENT_TARGET=10.6 CC=o64-clang CXX=o64-clang++ GOOS=darwin GOARCH=amd64 CGO_ENABLED=1 go build -v mywork/myprogram.

Some of my programs compile successfully, some throw errors at linking time but I guess that's another issue so I'll mark this question as solved as far as the cross-compilation goes.

Aditya Kresna Permana
  • 11,869
  • 8
  • 42
  • 48
Nicolas
  • 403
  • 1
  • 6
  • 18