Closed NewUserHa closed 1 year ago
Do you have a sample program that reproduces this issue?
any code that causes go-sqlite3 to compile is enough. maybe https://github.com/mattn/go-sqlite3/blob/master/_example/simple/simple.go, and the problem compiler is "GCC 11.2.0 + LLVM/Clang/LLD/LLDB 13.0.0 + MinGW-w64 9.0.0 (UCRT) - release 2" in that homepage within the original post
I haven't dug in too far, but I suspect that the problem here is skew between the precompiled runtime/cgo
object file and the C compiler that you are using. (The C compiler we use to build the Go distribution is very old; see #35006.)
When we stop shipping precompiled object files (#47257) then version skew should no longer be an issue.
@NewUserHa, in the meantime you may be able to work around the problem by installing Go from source. Could you give that a try and post the results?
I don't have any toolchain for that except the only pre-build gcc compiler. so probably needs others to test
You shouldn't need much else on top of the gcc
toolchain — just git
to clone the Go repo, really.
Should follow which part of https://go.dev/doc/install/source? it says "There are two official Go compiler toolchains."
Thanks!
Perhaps not surprisingly, many of those test failures are the same tests (but with different failure modes) as what @thanm observed in https://github.com/golang/go/issues/35006#issuecomment-1022473787.
The failure mode for cmd/link/internal/ld.TestWindowsBuildmodeCSharedASLR
is identical with both updated C toolchains.
The link errors in debug/pe.TestInternalLinkerDWARF
, runtime/cgo.test
, and cmd/nm.TestInternalLinkerCgoExec
are different, but the correlation between those tests is a bit uncanny. 😅
Intriguingly, the string "__imp___acrt_iob_func"
is hard-coded into the Go linker:
https://cs.opensource.google/go/go/+/master:src/cmd/link/internal/loadpe/ldpe.go;l=505;drc=69262d48717771cedb1da86563eb3f1b094b4e92
ok. will that issue get fixed anytime soon? will go support MSVC?
will that issue get fixed anytime soon?
No idea, but I would really like it to be fixed before Go 1.19 because I want to be able to test cgo
build reproducibility on Windows. 😅
will go support MSVC?
That is tracked in #20982. It is currently milestoned Unplanned
, so I would not expect it to happen unless a contributor outside the Go project steps up to implement it.
thanks.
I have a CL that should (in theory) take care of this problem, https://go-review.googlesource.com/c/go/+/382837. When I get that stack checked in (other parts still need work) I'll be sure to test this scenario (winlib + gosqlite). Thanks.
This message seems to happen in any program that uses featurs such as import "C" and import '
Here is a simple go file which will cause go build to fail with this error:
` package main
import "C"
import (
"log"
"golang.org/x/sys/windows"
)
var hModule windows.Handle
func init() {
err := windows.GetModuleHandleEx(0x01, nil, &hModule)
if err != nil {
panic(err)
}
}
func main() {
log.Println("Hello")
}
`
go build
C:\Program Files\Go\pkg\tool\windows_amd64\link.exe: running gcc failed: exit status 1
c:/program files/winlibs-x86_64-posix-seh-gcc-12.2.0-llvm-14.0.6-mingw-w64ucrt-10.0.0-r2/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\steve\AppData\Local\Temp\go-link-2891155275\000005.o: in function _cgo_preinit_init': \\_\_\runtime\cgo/gcc_libinit_windows.c:40: undefined reference to
imp___iob_func'
c:/program files/winlibs-x86_64-posix-seh-gcc-12.2.0-llvm-14.0.6-mingw-w64ucrt-10.0.0-r2/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\steve\AppData\Local\Temp\go-link-2891155275\000005.o: in function x_cgo_notify_runtime_init_done': \\_\_\runtime\cgo/gcc_libinit_windows.c:105: undefined reference to
imp_iob_func'
c:/program files/winlibs-x86_64-posix-seh-gcc-12.2.0-llvm-14.0.6-mingw-w64ucrt-10.0.0-r2/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\steve\AppData\Local\Temp\go-link-2891155275\000005.o: in function _cgo_beginthread': \\_\_\runtime\cgo/gcc_libinit_windows.c:149: undefined reference to
imp_iob_func'
c:/program files/winlibs-x86_64-posix-seh-gcc-12.2.0-llvm-14.0.6-mingw-w64ucrt-10.0.0-r2/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\steve\AppData\Local\Temp\go-link-2891155275\000006.o: in function x_cgo_thread_start': \\_\_\runtime\cgo/gcc_util.c:18: undefined reference to
_impiob_func'
collect2.exe: error: ld returned 1 exit status
@steowens thanks for the note.
Much work has been done on the Go linker's windows host object loading phase since this issue was filed last February. Things are better now on Go tip (e.g. the version about to be released as Go 1.20). I tested your example by hand using Go 1.20 RC1 and these compilers from winlibs.com, and things work ok for me.
If you get a change, perhaps you can download the release candidate and try it yourself, see this email.
note 1.20rc1 doesn't have precompiled object files, which appeared to be the issue with @steowens / #57455
@seankhliao Ah thanks, yes, now that I read a little more carefully that does indeed look like the underlying cause.
May helpful ...
NOK (go version go1.19.4 windows/amd64):
C:\Users\ABCDEFG\go-dev\src\lssc>go build -o build/windows-amd64/lssc.exe
# develop.vwg/lssc
C:\Users\ABCDEFG\go1.19.4\go\pkg\tool\windows_amd64\link.exe: running gcc failed: exit status 1
c:/users/ABCDEFG/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\ABCDEFG\AppData\Local\Temp\go-link-2746559253\000007.o: in function `_cgo_preinit_init':
\\_\_\runtime\cgo/gcc_libinit_windows.c:40: undefined reference to `__imp___iob_func'
c:/users/ABCDEFG/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\ABCDEFG\AppData\Local\Temp\go-link-2746559253\000007.o: in function `x_cgo_notify_runtime_init_done':
\\_\_\runtime\cgo/gcc_libinit_windows.c:105: undefined reference to `__imp___iob_func'
c:/users/ABCDEFG/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\ABCDEFG\AppData\Local\Temp\go-link-2746559253\000007.o: in function `_cgo_beginthread':
\\_\_\runtime\cgo/gcc_libinit_windows.c:149: undefined reference to `__imp___iob_func'
c:/users/ABCDEFG/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\ABCDEFG\AppData\Local\Temp\go-link-2746559253\000008.o: in function `x_cgo_thread_start':
\\_\_\runtime\cgo/gcc_util.c:18: undefined reference to `__imp___iob_func'
collect2.exe: error: ld returned 1 exit status
C:\Users\ABCDEFG\go-dev\src\lssc>go version
go version go1.19.4 windows/amd64
OK (go1.20rc1 windows/amd64):
C:\Users\ABCDEFG\go-dev\src\lssc>go build -o build/windows-amd64/lssc.exe
C:\Users\ABCDEFG\go-dev\src\lssc>go version
go version go1.20rc1 windows/amd64
Hello @thanm and all.
I can confirm that I have the same issue as the OP with 1.19.4 and @Klaus-Tockloth,
and resolved with go1.20rc1
This is my example file https://github.com/YOU54F/hello_ffi/blob/main/go/helloFfi.go
I am just going through my examples and getting them working on windows.
note, my system deps were
choco install --yes golang mingw
This page was useful for me in working out how to install the release candidate
go install golang.org/dl/go1.20rc1@latest
go1.20rc1 download
Thanks. I am going to go ahead and close out this issue.
as a hot fix to keep working in the meantime this worked:
go install golang.org/dl/go1.20rc1@latest
go1.20rc1 download
go1.20rc1 get .
go1.20rc1 build .
go1.20rc1 run ....
I was reaching the same issue, and it got fixed with go 1.20 too. But I have now new related error, the linker cannot find __mingw_vfprintf
symbol. I'm not sure if I should open new issue for it or not.
You can reproduce it with the following files:
Invoking build.ps1
on my machine results in the following output:
Please let me know if you need more information, or if I should open new issue.
@AMDG2
Your compile/link recipe doesn't look right. In general for C++ programs it is not a viable strategy to invoke the linker directly -- one needs to invoke the C++ driver during the link stage, and then the C++ driver will run the linker passing the right flags and libraries.
Here is an example (linux machine):
thanm@cetina:~$ cat himom.cpp
#include <iostream>
int main(int argc, char **argv) {
std::cout << "hi mom\n";
return 0;
}
$ clang++ -c himom.cpp
$ ld.lld -o himom.exe himom.o
ld.lld: error: undefined symbol: std::cout
ld.lld: error: undefined symbol: std::basic_ostream<char, std::char_traits<char> >& std::operator<<<std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)
ld.lld: error: undefined symbol: std::ios_base::Init::Init()
...
$ clang++ -o himom.exe himom.o
$ ./himom.exe
hi mom
Try running the last command above with "-v" and you can see all of the various libraries being passed to the linker by the driver.
Also want to add that the use of MSVC (e.g. cl.exe, link.exe) is something that is not supported/test by the Go team at the moment; I would recommend instead sticking with mingw-based compilers and linkers.
@thanm Thank you for the quick reply! I tried to make it simpler, but you are right, we usually link through the compiler.
I changed my build script for this repro to build and link with the compiler in one step, and I also added building with GCC (I also fixed the Go code, so it actually prints the string instead of the address, but it does not matter).
I get the following output:
So, it still does not work with MSVC and Clang, but it works with GCC as one would expect. Is there any plan to extend the support to at least Clang & LLVM toolchain, if not MSVC?
@AMDG2 you are using clang-cl.exe, which is the MSVC-compatible version of clang. Please use a mingw-based version of clang (such as https://github.com/mstorsjo/llvm-mingw)
Original post: https://github.com/mattn/go-sqlite3/issues/1012
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
maybe
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
import it
andgo build
orgo run .
What did you expect to see?
no output
What did you see instead?