-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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
cmd/cgo: support clang on Windows #17014
Comments
I don't believe clang is supported yet. Our installation docs only mention mingw. That said, we should probably support clang on Windows at some point. |
I have nothing against clang support. I have never had it installed on my computer. I will try install clang when I have some time. Alex |
I did some digging around and found out that with
With: I guess, it cannot find the C libs, but then when I tried to add
@nadiasvertex I would really appreciate an advice on proper libpath setting. |
Maybe it's related on #16455 ? |
Yes - I created symlink because of that. It solved the second part of the problem, but the first(and main) remains. |
@DmitriyMV I don't have much to tell you. The fundamental problem is that clang for Windows uses Microsoft's LIB as its backend. You do not need to tell LIB where to find the C library, it already knows that. I believe that the problem is that Go does not currently pass paths to clang in a way that LIB knows how to consume. I have not had success getting clang to compile with Go by playing with the commands. I think that the Go backend needs to be modified to send different values in order to make this work. Sorry. I haven't had much time to work on this, but it is something that I would like to get working, or see someone else get working. :-) |
I think core issue is that the target for clang is: x86_64-pc-windows-msvc
i.e. it's meant to be MSVC compatible.
However, Go on windows rely on mingw for cgo.
If we want to do anything here, it's probably to support msvc directly,
rather than clang's emulation of it.
|
I prefer Clang's emulation. It makes the compiler part simpler since MSVC On Tue, Nov 8, 2016, 2:48 AM Minux Ma notifications@github.com wrote: I think core issue is that the target for clang is: x86_64-pc-windows-msvc However, Go on windows rely on mingw for cgo. If we want to do anything here, it's probably to support msvc directly, — |
will this feature be available in GO1.8 release as I saw this issue has been linked to GO1.8Maybe issue list? Any update will be helpful. thanks. |
If an issue remains open, then it's not in any of the releases.
|
I'd like to get an update on this? I might be able to push this forward, if there's interest. |
Nothing new to report here.
Go ahead if you know what to do. Everyone will help, if we can. Alex |
@DmitriyMV
|
I found a bunch of issues. If you could tell me where in the code these things are processed, I might be able to fix them, unless someone else wants to take care of them:
Note: I used the |
OK. What is your question?
I would copy all code into directories without spaces at the start. Once that works, we can deal with paths with spaces.
According to $GOROOT/src/cmd/cgo/doc.go file:
So it seems to me Go creates _cgo_main.c file and then uses gcc to compile it into _cgo_main.o. Does _cgo_main.c gets created? It should be compiled into _cgo_main.o by invoking gcc, but you do not have gcc. Do you use clang to compile _cgo_main.c? Does compilation succeeds or fails?
Searching for "-nostdlib", I find many instances in $GOROOT/src/cmd/go/internal/work/build.go. If clang does not accepts "-nostdlib", maybe try and adjust there?
The "go build -x -work" only controls go command itself. You, probably, want to do the same for the cmd/link as well. See #20982 (comment) for how to access cmd/link temp files. You might also find -v cmd/link flag useful (or "-v -v"). I hope it helps. Alex |
The main issue that I received when using the MSVC linker is a segmentation fault that occurred while attempting to bind stdout. I can help to resolve the unresolved symbols but the segmentation fault issue is a bit trickier. My plan for that is to basically manually link everything with MinGW and then use DLL linking to factor out each object at a time until I receive the segmentation fault. I think then I will need to compare assembly to see what the problem is, although maybe there is someone more talented than I am that has a better idea? I am probably stretching the limits of my capability so I am open to as much help as I can get. |
@xoviat are you taking about issue #20982? Did you manage to build executable successfully? But the executable crashes when it runs? Perhaps if you can provide the steps, I will try and debug it too. Maybe reply on #20982 so we don't confuse things. Thank you. Alex |
I tried to remove the
I can link the files, by manually adding missing object files and modifying linker options, but don't know the next step the build process takes, so I can't move further. |
@blizzardplus Do you have libgo? |
I'm trying use a go library through rust ffi tx5-go-pion-sys A bunch of rust deps will only build on windows through msvc, and go will only build with mingw. So right now we're using dynamic linking, but it'd be nice if go supported either msvc directly, or clang and i could use clang-for-msvc so I could statically link the library. |
Golang positions itself as a "cross-platform language". Why are the Go wrapper libraries which use CGO store its source code as on Unix or Linux O.S. ? Some libraries written in C language have portability issues when imported as Unix-Linux source code and compiled on Windows O.S. This fact does not allow me to say that Golang is a truly cross-platfrom language. At least its CGO feature is not truly cross-platform. Moreover, the GCC compiler is not a best solution for compiling code for Windows O.S. |
The rest is a bit philosophical, but I’m going to disagree on this point. There are certainly codebases where other compilers will produce “better” output on Windows, but I’ve shipped production Go code using MinGW gcc for the Windows cgo dependencies, and it’s been quite reliable, given me performant code, etc. I’m not even positive that LLVM on Windows doesn’t work with cgo, because it certainly does on macOS. |
@vault-thirteen Thanks for the questions. Go is an open source project. As such, the answer to these kinds of questions tends to be "because nobody has fixed it yet." This issue is tagged "help wanted". Would you like to fix it? |
Hello @ianlancetaylor. There are several moments about the Go language.
Open-source means literally source which is published to people.
Yes, I would like to fix this, but I am not a creator of this language. |
@vault-thirteen The Go project does not do GitHub pull requests, it uses Gerrit, as has been pointed out in the PR you seem to be referring to. Also, it was the author who closed it. |
@gophun thank you for correcting me. |
I don't see why not, if someone steps up and pulls through the whole contribution process and does not abandon their change halfway through. |
Does CGO provide a way to configure the process of compiling and linking ? |
Yes, it does. The environment variables are called CC, CXX, CGO_CFLAGS, CGO_CPPFLAGS, CGO_CXXFLAGS, CGO_FFLAGS, CGO_LDFLAGS. https://pkg.go.dev/cmd/cgo#hdr-Using_cgo_with_the_go_command |
Thank you, @gophun. |
Where can I find the source code of the CGO mechanism ? I have found these parts so far:
Are there any other parts available ? |
Why are there a lot of unresolved references in Go's source code ? How do you build it ? |
https://go.dev/doc/install/source appears to be the instructions for building Go from source. |
What are you looking for more precisely? As you've discovered, cmd/cgo is the tool that takes code that transform Go code that uses
There aren't. For questions like this we don't use the issue tracker. Please see https://go.dev/wiki/Questions. Thanks. |
I have tried the patch from Gerrit to enable MSVC's clang to be used, unfortunately the following program crashes package main
/*
#include <stdio.h>
#include <stdlib.h>
void myprint() {
printf("Hello world\n");
}
*/
import "C"
func main() {
C.myprint()
}
|
I've tried cross-compile from macOS to Windows with llvm-mingw (using ucrt) and it works well. Running the output binary on Windows successfully. |
I can confirm the patch in #54811 works against go 1.22.2. Because I didn't document my last experiment properly
HOWEVER: https://www.microsoft.com/en-us/wdsi/submission/89af1110-6167-44d0-918d-d1c440a81b6f If anyone is interested in getting this merged I am going to need help with the Gerrit process, and I'm going to need to find someone from Microsoft interested in fixing whatever's making Defender angry. |
Assuming the ping is about Defender specifically, it looks like https://www.microsoft.com/en-us/wdsi/submission/89af1110-6167-44d0-918d-d1c440a81b6f and https://www.microsoft.com/en-us/wdsi/submission/1b794e3a-f8df-4fbd-bf75-e799296f6a85 have expired. If you upload those again, we may be able to dig deeper into this with the Defender team. (Sorry if it's just a permissions issue and they're still visible on your end, let me know if that's the case.) Windows Defender is well known to have issues with Go programs in general (https://go.dev/doc/faq#virus, microsoft/go#1255), and it seems understandable to me if switching to a not-before-seen cgo compiler is making Defender act up more than usual. I'm a little confused about your comment about not having More broadly, I do think clang (and other) C compiler support for cgo on Windows is something that we'd be interested in helping with, but I can't promise we'll have time for it soon. (For context on the thread: @gdams and I work on the Microsoft fork of Go and try to help out with OS-Windows issues.) |
@dagood thanks for the response!
I think DetectItEasy has a good hint as to what might be triggering Defender - repeating section names? Maybe something has gone wrong with the linker in that case. To be honest, I'm less interested in solving the Defender alert right now, it'd just be good to get some additional help to get this running as it's currently beyond my ability. |
What version of Go are you using (go version)?
go version devel +0cff219 Wed Sep 7 10:43:13 2016 +0000 windows/amd64
What operating system and processor architecture are you using (go env)?
Windows 10 / AMD64
What did you do?
I used CGO with Clang for Windows 3.8.1 (http://llvm.org/releases/3.8.1/LLVM-3.8.1-win64.exe) like this:
set CC=clang
set CGO_CFLAGS="-m64 -Ipath/to/my/includes"
set CGO_LDFLAGS = "-v -Xlinker -libpath:path/to/libs -lwldap32 -ladvapi32 -lws2_32 -ldbghelp -luser32"
go build
What did you expect to see?
Expected to see nothing (ie, successful compile)
What did you see instead?
The text was updated successfully, but these errors were encountered: