Help with cross-compilation and CGO.

287 views
Skip to first unread message

Bruno Albuquerque

unread,
Nov 25, 2015, 5:55:47 AM11/25/15
to golang-nuts
I am trying to cross-compile this code that relies on several libraries with dependencies on C/C++ static libraries. Compaling the project locally on linux works without issues but I wanted to cross-compile the same project from Linux to Windows amd64.

To do that, i installed MXE and used it as the C/C++ cross-compiler. Compilation seems to work without issues but, when linking, I get errors about symbols being redefined:

$ CGO_ENABLED=1 CC=x86_64-w64-mingw32.static-gcc CXX=x86_64-w64-mingw32.static-g++ GOOS=windows GOARCH=amd64 go build -x test.go

[... all packages are compiled without issues but, then ...]

mkdir -p $WORK/command-line-arguments/_obj/
mkdir -p $WORK/command-line-arguments/_obj/exe/
cd /home/bga/development/go-dev/src
/usr/local/go/pkg/tool/linux_amd64/compile -o $WORK/command-line-arguments.a -trimpath $WORK -p main -complete -buildid dec769d8ff9c511dd002d6ccad0f5224169c5685 -D _/home/bga/development/go-dev/src -I $WORK -I /home/bga/development/go-dev/pkg/windows_amd64 -pack ./test.go
cd .
/usr/local/go/pkg/tool/linux_amd64/link -o $WORK/command-line-arguments/_obj/exe/a.out.exe -L $WORK -L /home/bga/development/go-dev/pkg/windows_amd64 -extld=x86_64-w64-mingw32.static-g++ -buildmode=exe -buildid=dec769d8ff9c511dd002d6ccad0f5224169c5685 $WORK/command-line-arguments.a
# command-line-arguments
/usr/local/go/pkg/tool/linux_amd64/link: running x86_64-w64-mingw32.static-g++ failed: exit status 1
/tmp/go-link-901845510/000002.o: In function `_Unwind_GetGR':
/home/bga/mxe/tmp-gcc-x86_64-w64-mingw32.static/gcc-4.9.3/libgcc/unwind-seh.c:81: multiple definition of `_Unwind_GetGR'
/tmp/go-link-901845510/000000.o:/home/bga/mxe/tmp-gcc-x86_64-w64-mingw32.static/gcc-4.9.3/libgcc/unwind-seh.c:81: first defined here
/tmp/go-link-901845510/000002.o: In function `_Unwind_SetGR':
/home/bga/mxe/tmp-gcc-x86_64-w64-mingw32.static/gcc-4.9.3/libgcc/unwind-seh.c:91: multiple definition of `_Unwind_SetGR'
/tmp/go-link-901845510/000000.o:/home/bga/mxe/tmp-gcc-x86_64-w64-mingw32.static/gcc-4.9.3/libgcc/unwind-seh.c:91: first defined here
/tmp/go-link-901845510/000002.o: In function `_Unwind_GetCFA':
/home/bga/mxe/tmp-gcc-x86_64-w64-mingw32.static/gcc-4.9.3/libgcc/unwind-seh.c:102: multiple definition of `_Unwind_GetCFA'
/tmp/go-link-901845510/000000.o:/home/bga/mxe/tmp-gcc-x86_64-w64-mingw32.static/gcc-4.9.3/libgcc/unwind-seh.c:102: first defined here
/tmp/go-link-901845510/000002.o: In function `_Unwind_GetIP':
/home/bga/mxe/tmp-gcc-x86_64-w64-mingw32.static/gcc-4.9.3/libgcc/unwind-seh.c:110: multiple definition of `_Unwind_GetIP'
/tmp/go-link-901845510/000000.o:/home/bga/mxe/tmp-gcc-x86_64-w64-mingw32.static/gcc-4.9.3/libgcc/unwind-seh.c:110: first defined here
/tmp/go-link-901845510/000002.o: In function `_Unwind_GetIPInfo':
/home/bga/mxe/tmp-gcc-x86_64-w64-mingw32.static/gcc-4.9.3/libgcc/unwind-seh.c:123: multiple definition of `_Unwind_GetIPInfo'
/tmp/go-link-901845510/000000.o:/home/bga/mxe/tmp-gcc-x86_64-w64-mingw32.static/gcc-4.9.3/libgcc/unwind-seh.c:123: first defined here
/tmp/go-link-901845510/000002.o: In function `_Unwind_SetIP':
/home/bga/mxe/tmp-gcc-x86_64-w64-mingw32.static/gcc-4.9.3/libgcc/unwind-seh.c:131: multiple definition of `_Unwind_SetIP'
/tmp/go-link-901845510/000000.o:/home/bga/mxe/tmp-gcc-x86_64-w64-mingw32.static/gcc-4.9.3/libgcc/unwind-seh.c:131: first defined here
/tmp/go-link-901845510/000002.o: In function `_Unwind_GetLanguageSpecificData':
/home/bga/mxe/tmp-gcc-x86_64-w64-mingw32.static/gcc-4.9.3/libgcc/unwind-seh.c:137: multiple definition of `_Unwind_GetLanguageSpecificData'
/tmp/go-link-901845510/000000.o:/home/bga/mxe/tmp-gcc-x86_64-w64-mingw32.static/gcc-4.9.3/libgcc/unwind-seh.c:137: first defined here
/tmp/go-link-901845510/000002.o: In function `_Unwind_GetRegionStart':
/home/bga/mxe/tmp-gcc-x86_64-w64-mingw32.static/gcc-4.9.3/libgcc/unwind-seh.c:143: multiple definition of `_Unwind_GetRegionStart'
/tmp/go-link-901845510/000000.o:/home/bga/mxe/tmp-gcc-x86_64-w64-mingw32.static/gcc-4.9.3/libgcc/unwind-seh.c:143: first defined here
/tmp/go-link-901845510/000002.o: In function `_Unwind_FindEnclosingFunction':
/home/bga/mxe/tmp-gcc-x86_64-w64-mingw32.static/gcc-4.9.3/libgcc/unwind-seh.c:148: multiple definition of `_Unwind_FindEnclosingFunction'
/tmp/go-link-901845510/000000.o:/home/bga/mxe/tmp-gcc-x86_64-w64-mingw32.static/gcc-4.9.3/libgcc/unwind-seh.c:148: first defined here
/tmp/go-link-901845510/000002.o: In function `_Unwind_GetDataRelBase':
/home/bga/mxe/tmp-gcc-x86_64-w64-mingw32.static/gcc-4.9.3/libgcc/unwind-seh.c:161: multiple definition of `_Unwind_GetDataRelBase'
/tmp/go-link-901845510/000000.o:/home/bga/mxe/tmp-gcc-x86_64-w64-mingw32.static/gcc-4.9.3/libgcc/unwind-seh.c:161: first defined here
/tmp/go-link-901845510/000002.o: In function `_Unwind_GetTextRelBase':
/home/bga/mxe/tmp-gcc-x86_64-w64-mingw32.static/gcc-4.9.3/libgcc/unwind-seh.c:166: multiple definition of `_Unwind_GetTextRelBase'
/tmp/go-link-901845510/000000.o:/home/bga/mxe/tmp-gcc-x86_64-w64-mingw32.static/gcc-4.9.3/libgcc/unwind-seh.c:166: first defined here
/tmp/go-link-901845510/000002.o: In function `_GCC_specific_handler':
/home/bga/mxe/tmp-gcc-x86_64-w64-mingw32.static/gcc-4.9.3/libgcc/unwind-seh.c:196: multiple definition of `_GCC_specific_handler'
/tmp/go-link-901845510/000000.o:/home/bga/mxe/tmp-gcc-x86_64-w64-mingw32.static/gcc-4.9.3/libgcc/unwind-seh.c:196: first defined here
/tmp/go-link-901845510/000002.o: In function `_Unwind_RaiseException':
/home/bga/mxe/tmp-gcc-x86_64-w64-mingw32.static/gcc-4.9.3/libgcc/unwind-seh.c:330: multiple definition of `_Unwind_RaiseException'
/tmp/go-link-901845510/000000.o:/home/bga/mxe/tmp-gcc-x86_64-w64-mingw32.static/gcc-4.9.3/libgcc/unwind-seh.c:330: first defined here
/tmp/go-link-901845510/000002.o: In function `_Unwind_Resume':
/home/bga/mxe/tmp-gcc-x86_64-w64-mingw32.static/gcc-4.9.3/libgcc/unwind-seh.c:347: multiple definition of `_Unwind_Resume'
/tmp/go-link-901845510/000000.o:/home/bga/mxe/tmp-gcc-x86_64-w64-mingw32.static/gcc-4.9.3/libgcc/unwind-seh.c:347: first defined here
/tmp/go-link-901845510/000002.o: In function `_Unwind_Resume_or_Rethrow':
/home/bga/mxe/tmp-gcc-x86_64-w64-mingw32.static/gcc-4.9.3/libgcc/unwind-seh.c:396: multiple definition of `_Unwind_Resume_or_Rethrow'
/tmp/go-link-901845510/000000.o:/home/bga/mxe/tmp-gcc-x86_64-w64-mingw32.static/gcc-4.9.3/libgcc/unwind-seh.c:396: first defined here
/tmp/go-link-901845510/000002.o: In function `_Unwind_ForcedUnwind':
/home/bga/mxe/tmp-gcc-x86_64-w64-mingw32.static/gcc-4.9.3/libgcc/unwind-seh.c:409: multiple definition of `_Unwind_ForcedUnwind'
/tmp/go-link-901845510/000000.o:/home/bga/mxe/tmp-gcc-x86_64-w64-mingw32.static/gcc-4.9.3/libgcc/unwind-seh.c:409: first defined here
/tmp/go-link-901845510/000002.o: In function `_Unwind_DeleteException':
/home/bga/mxe/tmp-gcc-x86_64-w64-mingw32.static/gcc-4.9.3/libgcc/unwind-seh.c:427: multiple definition of `_Unwind_DeleteException'
/tmp/go-link-901845510/000000.o:/home/bga/mxe/tmp-gcc-x86_64-w64-mingw32.static/gcc-4.9.3/libgcc/unwind-seh.c:427: first defined here
/tmp/go-link-901845510/000002.o: In function `_Unwind_Backtrace':
/home/bga/mxe/tmp-gcc-x86_64-w64-mingw32.static/gcc-4.9.3/libgcc/unwind-seh.c:483: multiple definition of `_Unwind_Backtrace'
/tmp/go-link-901845510/000000.o:/home/bga/mxe/tmp-gcc-x86_64-w64-mingw32.static/gcc-4.9.3/libgcc/unwind-seh.c:483: first defined here
collect2: error: ld returned 1 exit status

Any ideas about what may be causing this or, alternatively, any tips on how to debug this further?

$ go version
go version go1.5.1 linux/amd64

Thanks in advance.

Ian Lance Taylor

unread,
Nov 25, 2015, 11:50:59 AM11/25/15
to Bruno Albuquerque, golang-nuts
On Wed, Nov 25, 2015 at 2:55 AM, Bruno Albuquerque <b...@bug-br.org.br> wrote:
> I am trying to cross-compile this code that relies on several libraries with
> dependencies on C/C++ static libraries. Compaling the project locally on
> linux works without issues but I wanted to cross-compile the same project
> from Linux to Windows amd64.
>
> To do that, i installed MXE and used it as the C/C++ cross-compiler.
> Compilation seems to work without issues but, when linking, I get errors
> about symbols being redefined:

Looks like https://golang.org/issue/9510 . It should be fixed on tip
but unfortunately there may be no good workaround in 1.5.

Ian

Bruno Albuquerque

unread,
Nov 25, 2015, 12:36:32 PM11/25/15
to Ian Lance Taylor, golang-nuts
I guess this is exactly my problem. Thanks.

For now I can use dynamic linking although it would be great to not have to do that to avoid having to distribute other libraries with the binary file.



--
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.
For more options, visit https://groups.google.com/d/optout.

Péter Szilágyi

unread,
Nov 27, 2015, 4:23:19 AM11/27/15
to Bruno Albuquerque, Ian Lance Taylor, golang-nuts
Hey Bruno,

  As you didn't provide any packages you're trying to build I can't promise anything, but you might take a look at xgo for CGO enabled cross compilation. It's a suite of docker file based cross compilers that can currently target windows/linux/darwin/android/ios (latter two with limited experimental support). The only requirement is a valid docker installation (also it will download 1GB+ cross compiler container). It also supports building with various Go releases, including latest 1.5 develop branch and the main develop branch to test out recently fixed issues.

Cheers,
  Peter

Bruno Albuquerque

unread,
Nov 27, 2015, 5:40:36 AM11/27/15
to Péter Szilágyi, Ian Lance Taylor, golang-nuts
I actually tried xgo. It didn't quite work for me because some of the C/C++ dependencies had to be compiled with more than configure/make so it would fail with xgo. A way to provide an script for compiling a specific dependency would be great.

Other than that, xgo does look great.

-Bruno

Péter Szilágyi

unread,
Nov 30, 2015, 7:42:27 AM11/30/15
to Bruno Albuquerque, Ian Lance Taylor, golang-nuts
Hmm, it's something to definitely keep in mind. I had on and off ideas about dependency specific build instructions, just haven't found a clean enough and easy enough solution yet :)
Reply all
Reply to author
Forward
0 new messages