Skip to content

use case: using zig cc to cross compile a cgo program for windows #7874

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

Closed
andrewrk opened this issue Jan 24, 2021 · 4 comments
Closed

use case: using zig cc to cross compile a cgo program for windows #7874

andrewrk opened this issue Jan 24, 2021 · 4 comments
Labels
downstream An issue with a third party project that uses Zig. enhancement Solving this issue will likely involve adding new logic or components to the codebase. os-windows zig cc Zig as a drop-in C compiler feature
Milestone

Comments

@andrewrk
Copy link
Member

andrewrk commented Jan 24, 2021

Dowstream issue: golang/go#43886

main.go

package main

//int Add(int a, int b){
//    return a+b;
//}
import "C"
import "fmt"

func main() {
	a := C.int(10)
	b := C.int(20)
	c := C.Add(a, b)
	fmt.Println(c) // 30
}

CGO_ENABLED=1 GOOS=windows GOARCH=amd64 CC="zcc" CXX="zc++" go build main.go

zcc

#!/bin/sh
zig cc -target x86_64-windows-gnu $@

zc++

#!/bin/sh
zig c++ -target x86_64-windows-gnu $@

output:

# command-line-arguments
warning: unsupported linker arg: --compress-debug-sections=zlib-gnu

It produces main.exe but when run it segfaults in runtime.rt0_go() (called from main()). This is probably an issue with the go runtime, and the next step here will be to file a downstream error report with go.

@andrewrk andrewrk added enhancement Solving this issue will likely involve adding new logic or components to the codebase. os-windows downstream An issue with a third party project that uses Zig. zig cc Zig as a drop-in C compiler feature labels Jan 24, 2021
@andrewrk andrewrk added this to the 0.9.0 milestone Jan 24, 2021
andrewrk added a commit that referenced this issue Jan 24, 2021
@LaPingvino
Copy link

as mentioned in the Go issue, this is probably fixed by @zx2c4's fixes for Windows ARM64, which necessitates LLVM support. it seems like a "did never work before" issue.

dgbuckley pushed a commit to dgbuckley/zig that referenced this issue Mar 9, 2021
@andrewrk andrewrk modified the milestones: 0.9.0, 0.10.0 May 19, 2021
@antonmedv
Copy link

Still not possible?

@LaPingvino
Copy link

Probably fixed with Go 1.18, which is not released yet. This also fixes the need for a workaround for invoking zig cc / zig c++.

@motiejus
Copy link
Contributor

motiejus commented Dec 21, 2021

The test program works for me with go1.17 and zig 0.9.0 and when run under wine:

motiejus ~/x/go $ go version
go version go1.17 linux/amd64
motiejus ~/x/go $ zig version
0.9.0
motiejus ~/x/go $ wine ./main.exe 
30
motiejus ~/x/go $ 

Is behavior different on windows? Unfortunately, I don't have a machine to test it with.

@andrewrk andrewrk modified the milestones: 0.11.0, 0.9.0 Dec 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
downstream An issue with a third party project that uses Zig. enhancement Solving this issue will likely involve adding new logic or components to the codebase. os-windows zig cc Zig as a drop-in C compiler feature
Projects
None yet
Development

No branches or pull requests

4 participants