Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linker error when compiling to Windows when importing github.com/andlabs/ui #362

Open
jclc opened this issue Oct 3, 2018 · 19 comments
Open
Labels
build This issues describes a compilation problem

Comments

@jclc
Copy link
Contributor

jclc commented Oct 3, 2018

I'm importing this package alongside github.com/andlabs/ui and I get this error when cross-compiling to Windows using mingw:

/usr/lib/go/pkg/tool/linux_amd64/link: running x86_64-w64-mingw32-g++ failed: exit status 1
/usr/lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib/libSDL2.a(SDL_windows.o):(.text+0x17f): undefined reference to `__imp_CoInitializeEx'
/usr/lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib/libSDL2.a(SDL_windowskeyboard.o):(.text+0xf6): undefined reference to `ImmGetIMEFileNameA'
/usr/lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib/libSDL2.a(SDL_windowskeyboard.o):(.text+0x144): undefined reference to `ImmGetContext'
/usr/lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib/libSDL2.a(SDL_windowskeyboard.o):(.text+0x167): undefined reference to `ImmReleaseContext'
/usr/lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib/libSDL2.a(SDL_windowskeyboard.o):(.text+0x1d4): undefined reference to `ImmGetCompositionStringW'
/usr/lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib/libSDL2.a(SDL_windowskeyboard.o):(.text+0x1ec): undefined reference to `ImmGetCompositionStringW'
/usr/lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib/libSDL2.a(SDL_windowskeyboard.o):(.text+0x3ff): undefined reference to `ImmGetIMEFileNameA'
/usr/lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib/libSDL2.a(SDL_windowskeyboard.o):(.text+0x569): undefined reference to `GetFileVersionInfoSizeA'
/usr/lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib/libSDL2.a(SDL_windowskeyboard.o):(.text+0x595): undefined reference to `GetFileVersionInfoA'
/usr/lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib/libSDL2.a(SDL_windowskeyboard.o):(.text+0x5bf): undefined reference to `VerQueryValueA'
/usr/lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib/libSDL2.a(SDL_windowskeyboard.o):(.text+0x85c): undefined reference to `__imp_SysFreeString'
/usr/lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib/libSDL2.a(SDL_windowskeyboard.o):(.text+0x9ee): undefined reference to `ImmGetContext'
/usr/lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib/libSDL2.a(SDL_windowskeyboard.o):(.text+0xa0c): undefined reference to `ImmNotifyIME'
/usr/lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib/libSDL2.a(SDL_windowskeyboard.o):(.text+0xa29): undefined reference to `ImmNotifyIME'
/usr/lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib/libSDL2.a(SDL_windowskeyboard.o):(.text+0xa35): undefined reference to `ImmReleaseContext'
/usr/lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib/libSDL2.a(SDL_windowskeyboard.o):(.text+0xa83): undefined reference to `ImmSetCompositionStringW'
/usr/lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib/libSDL2.a(SDL_windowskeyboard.o):(.text+0xb13): undefined reference to `ImmAssociateContext'
/usr/lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib/libSDL2.a(SDL_windowskeyboard.o):(.text+0xc2d): undefined reference to `ImmGetContext'
/usr/lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib/libSDL2.a(SDL_windowskeyboard.o):(.text+0xc3c): undefined reference to `ImmReleaseContext'
/usr/lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib/libSDL2.a(SDL_windowskeyboard.o):(.text+0x12bd): undefined reference to `ImmAssociateContext'
/usr/lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib/libSDL2.a(SDL_windowskeyboard.o):(.text+0x14f5): undefined reference to `ImmAssociateContext'
/usr/lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib/libSDL2.a(SDL_windowskeyboard.o):(.text+0x158c): undefined reference to `ImmGetContext'
/usr/lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib/libSDL2.a(SDL_windowskeyboard.o):(.text+0x15bd): undefined reference to `ImmSetCompositionWindow'
/usr/lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib/libSDL2.a(SDL_windowskeyboard.o):(.text+0x15c9): undefined reference to `ImmReleaseContext'
/usr/lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib/libSDL2.a(SDL_windowskeyboard.o):(.text+0x16a1): undefined reference to `ImmGetContext'
/usr/lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib/libSDL2.a(SDL_windowskeyboard.o):(.text+0x16ed): undefined reference to `ImmReleaseContext'
/usr/lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib/libSDL2.a(SDL_windowskeyboard.o):(.text+0x17de): undefined reference to `ImmGetContext'
/usr/lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib/libSDL2.a(SDL_windowskeyboard.o):(.text+0x17ff): undefined reference to `ImmGetCandidateListW'
/usr/lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib/libSDL2.a(SDL_windowskeyboard.o):(.text+0x1814): undefined reference to `ImmReleaseContext'
/usr/lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib/libSDL2.a(SDL_windowskeyboard.o):(.text+0x198b): undefined reference to `ImmGetContext'
/usr/lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib/libSDL2.a(SDL_windowskeyboard.o):(.text+0x1a21): undefined reference to `ImmReleaseContext'
/usr/lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib/libSDL2.a(SDL_windowskeyboard.o):(.text+0x1a6d): undefined reference to `ImmGetCandidateListW'
/usr/lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib/libSDL2.a(SDL_windowskeyboard.o):(.text+0x1dc6): undefined reference to `__imp_SysFreeString'
/usr/lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib/libSDL2.a(SDL_windowskeyboard.o):(.text+0x1ebb): undefined reference to `__imp_SysFreeString'
/usr/lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib/libSDL2.a(SDL_wasapi.o):(.text+0x355): undefined reference to `__imp_PropVariantClear'
collect2: virhe: ld:n paluuarvo oli 1

The package ui uses C++, could this be confusing the Go linker into using C++ linkage for SDL2?

@jclc
Copy link
Contributor Author

jclc commented Oct 4, 2018

andlabs/ui#326 (comment)
Apparently some linker flags are lost in the ether when including both packages. Appending -lole32 -loleaut32 -limm32 -lversion to CGO_LDFLAGS fixes this issue.

@veeableful
Copy link
Contributor

Hi @jclc, does that mean the ui package is the one that requires the extra link libraries?

@jclc
Copy link
Contributor Author

jclc commented Oct 5, 2018

The error message is triggered by SDL2, so I would imagine that it's an SDL2 issue. Adding these linker flags as a CGO directive would probably fix it, but I'd like to know the root cause of it.

@veeableful
Copy link
Contributor

It seems some people experienced it because they were compiling statically or have only static SDL2 library in their paths. Maybe it's because the ui package compiles with -static option?

@jclc
Copy link
Contributor Author

jclc commented Oct 5, 2018

You're right, it seems that SDL2 is now linked statically... that's not good. ui being linked statically shouldn't affect SDL2, so I believe this is an issue with Go.

@veeableful
Copy link
Contributor

Perhaps you could try compiling the program via go get -tags static and go build -tags static? We had support for static build since 0.3 (thanks gen2brain!).

@jclc
Copy link
Contributor Author

jclc commented Oct 5, 2018

Yeah that's the thing though, ui seems to force static linking on SDL2 which I don't want. Also, the error comes from g++ for some reason. ui uses C++ on Windows and it's also forcing SDL2 to use it as well, I don't think that's intended.

@andlabs
Copy link

andlabs commented Oct 6, 2018

It should still be possible to dynamically link SDL2 even with package ui; that's weird. I'll have to look at the build tags of this package...

The C++ linker should not affect anything. If SDL is affected by it, then we have a problem with SDL...

@jclc
Copy link
Contributor Author

jclc commented Feb 26, 2019

Returning to my project after a while, I'm finding more linker flags being lost.

/usr/local/go/pkg/tool/linux_amd64/link: running x86_64-w64-mingw32-g++ failed: exit status 1
/usr/lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/ld: /usr/lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/lib/../lib/libSDL2.a(hid.o):(.text+0x382): undefined reference to `__imp_SetupDiGetClassDevsA'
/usr/lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/ld: /usr/lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/lib/../lib/libSDL2.a(hid.o):(.text+0x391): undefined reference to `__imp_SetupDiGetDeviceRegistryPropertyA'
/usr/lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/ld: /usr/lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/lib/../lib/libSDL2.a(hid.o):(.text+0x3e0): undefined reference to `__imp_SetupDiEnumDeviceInterfaces'
/usr/lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/ld: /usr/lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/lib/../lib/libSDL2.a(hid.o):(.text+0x40d): undefined reference to `__imp_SetupDiGetDeviceInterfaceDetailA'
/usr/lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/ld: /usr/lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/lib/../lib/libSDL2.a(hid.o):(.text+0x445): undefined reference to `__imp_SetupDiGetDeviceInterfaceDetailA'
/usr/lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/ld: /usr/lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/lib/../lib/libSDL2.a(hid.o):(.text+0x456): undefined reference to `__imp_SetupDiEnumDeviceInfo'
/usr/lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/ld: /usr/lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/lib/../lib/libSDL2.a(hid.o):(.text+0x585): undefined reference to `__imp_SetupDiDestroyDeviceInfoList'

Adding -lsetupapi fixes this error. I'm not sure if upgrading mingw or SDL2 triggered this. Again, this only happens when importing both go-sdl2 and ui in the same program.

Edit: Right, it's because Go is forcing static linking but without the static tag, so the LDFLAGS in sdl/sdl_cgo_static.go are being omitted. -lsetupapi was added in the most recent commit. Maybe a more appropriate name for this issue is "importing github.com/andlabs/ui forces static linking".

@andlabs
Copy link

andlabs commented Feb 26, 2019

Right; I've taken steps to allow libui to be statically linkable specifically to make it more Go-like in nature; the interplay between this and that is an interesting question, and one that's in scope for the next release. Please file a bug there so I can keep track of it.

@jclc
Copy link
Contributor Author

jclc commented Feb 27, 2019

Are you sure it's an issue with libui though? I have no issues importing both ui and https://github.com/mattn/go-sqlite3; only go-sdl2 seems to have this issue.

FYI the code I'm using to test this:

package main

import (
	_ "github.com/andlabs/ui"
	_ "github.com/mattn/go-sqlite3"
//	_ "github.com/veandco/go-sdl2/sdl"
	"fmt"
)

func main() {
	fmt.Println("ok")
}

CGO_ENABLED=1 CC=x86_64-w64-mingw32-gcc CXX=x86_64-w64-mingw32-g++ GOOS=windows go build main.go

@veeableful
Copy link
Contributor

Hi @jclc, I'll have a look at this as well during the weekend. I'll keep you posted!

@andlabs
Copy link

andlabs commented Feb 28, 2019

It's not so much a problem with libui but a subtlety with mixing packages that expect different linking requirements. My question is about what's conventional for the rest of the Go open-source world, so I can provide the best possible solution. libui can be used dynamically and it would be great if I could provide that option as well...

@veeableful
Copy link
Contributor

Hi @jclc, do you have problem when compiling with -tags static like this?

CGO_ENABLED=1 CC=x86_64-w64-mingw32-gcc CXX=x86_64-w64-mingw32-g++ GOOS=windows go build -tags static

I can't seem to reproduce the problem on my Linux OS with this code:

package main
  
import (
        _ "github.com/andlabs/ui"
        _ "github.com/mattn/go-sqlite3"
        _ "github.com/veandco/go-sdl2/sdl"
        "fmt"
)

func main() {
        fmt.Println("ok")
}

@jclc
Copy link
Contributor Author

jclc commented Mar 3, 2019

With d969945 that works, but only when using -tags static. The problem was linking ui statically but sdl dynamically.

@veeableful
Copy link
Contributor

@jclc Ahh I see now! You're cross-compiling on Linux to Windows but require SDL2 to be dynamic.

In that case, could you try modifying sdl/sdl_cgo.go by replacing the #cgo windows ... line to this?

//#cgo windows LDFLAGS: -Wl, -Bdynamic -lSDL2

For me, I had to specify include path as well like the following. Perhaps you also need it.

//#cgo windows LDFLAGS: -Wl, -Bdynamic -lSDL2 -L [path-to-lib-dir]

I haven't tried to run it but at least it compiled for me =P

@jclc
Copy link
Contributor Author

jclc commented Mar 17, 2019

@veeableful Hi, sorry for the delay, I've been busy. What version of Go are you using? For me it says those flags are not valid; even adding them to CGO_LDFLAGS_ALLOW doesn't make a difference.

invalid flag in #cgo LDFLAGS: -Wl,

As a sidenote, I've decided it's better to just use static linkage on Windows anyway; maybe even on other platforms. I'll still help out figuring out this issue if needed.

@andlabs
Copy link

andlabs commented Mar 17, 2019

Remove the space after the comma.

@jclc
Copy link
Contributor Author

jclc commented Mar 17, 2019

Right, thanks... It does link SDL2 dynamically but now it also requires libstdc++-6.dll

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
build This issues describes a compilation problem
Projects
None yet
Development

No branches or pull requests

3 participants