Cross-Compile Windows 10 -> FreeBSD

523 views
Skip to first unread message

B Carr

unread,
Jun 11, 2019, 9:07:16 PM6/11/19
to golang-nuts
Newb here. I'm in over my head. Using a Win10/amd64 PowerShell with Go v1.12.6. Trying to cross-compile
to FreeBSD/amd64. go env = GOARCH=amd64 & GOOS=freebsd. Source includes calls to SQLite3. Compiles
without errors.

Resulting binary fails on FreeBSD with: "Can't connect to the SQLite DB Binary was compiled with 'CGO_ENABLED=0', go-sqlite3 requires cgo to work."

Added CGO_ENABLED=1 to the go env.  Recompile fails to build a binary and yields this error stream:

"# runtime/cgo
_cgo_export.c:1:0: error: -fPIC ignored for target (all code is position independent) [-Werror]
 /* Code generated by cmd/cgo; DO NOT EDIT. */
 ^
cc1.exe: all warnings being treated as errors"

I have no idea what that means.

Same source compiled for the Win10 machine runs perfectly. If I copy the source code over to a
FreeBSD box with Go installed there and compile, it works fine. It is the cross-compilation that
doesn't seem to work.

This isn't a Go problem, it is something I'm not understanding about cross-compiling

Any thoughts?

B Carr

unread,
Jun 11, 2019, 9:28:48 PM6/11/19
to golang-nuts
To reply to myself, I just found this comment under ADDENDUM:

"Cgo has issues when cross compiling. If you depend on cgo then you’re probably going to be better off building on the native platforms. The go tool will disable cgo support by default. To explicitly enable cgo, set CGO_ENABLED=1."


Does this mean cross-compiling with Go when cgo is required is hopeless?

Tamás Gulácsi

unread,
Jun 12, 2019, 12:09:49 AM6/12/19
to golang-nuts
Not hopeless, just hard. You need a cross-C-compiler, and use that.

It's easier to compile on native en.
But you can try github.com/karalabe/xgo if you have Docker.

B Carr

unread,
Jun 12, 2019, 12:04:09 PM6/12/19
to golang-nuts

Thank you. Probably too hard for me.

Tom Mitchell

unread,
Jun 12, 2019, 5:22:16 PM6/12/19
to B Carr, golang-nuts
Try a VMWARE virtual machine.  
Install a FreeBSD distro and as long as the hardware (CPU) is the same you should be good.
Even targeting a different hardware platform might be easier inside a VM.  

Inside the VM you can save temp files and capture compiler, assembler and linker settings to 
Chip away at it all. ABI, API, Libraries, magic numbers.. paths.
https://www.freebsd.org/doc/handbook/linuxemu-advanced.html
ABI and API updates are difficult.
https://www.cyberciti.biz/open-source/freebsd-12-released-here-is-how-to-upgrade-freebsd/

Look with care (including with a debugger) to see what FreeBSD thinks the new binary is.
Simple things like CR/LF can confuse things.

I see in a FreeBSD (11.2)  to newer FreeBSD (12.0) version update notes. 
Installed packages to be REINSTALLED:
	sudo-1.8.25p1 (ABI changed: 'freebsd:11:x86:64' -> 'freebsd:12:x86:64')
	sqlite3-3.25.1 (ABI changed: 'freebsd:11:x86:64' -> 'freebsd:12:x86:64')
	readline-7.0.3_1 (ABI changed: 'freebsd:11:x86:64' -> 'freebsd:12:x86:64')

Strip nothing,
Minimum optimizations.



--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/ebe9fe0d-7727-40b9-bc53-a9d237faecf7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
   T o m    M i t c h e l l
Reply all
Reply to author
Forward
0 new messages