Skip to content

import/path: gcc_linux_amd64.c: In function '_cgo_sys_thread_start' #68478

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
zhuzaiye opened this issue Jul 17, 2024 · 4 comments
Closed

import/path: gcc_linux_amd64.c: In function '_cgo_sys_thread_start' #68478

zhuzaiye opened this issue Jul 17, 2024 · 4 comments

Comments

@zhuzaiye
Copy link

Go version

go version go1.22.2 windows/amd64

Output of go env in your module/workspace:

$ go env
set GO111MODULE=on
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\hzzhu2\AppData\Local\go-build
set GOENV=C:\Users\hzzhu2\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Golang\goproject\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Golang\goproject
set GOPRIVATE=
set GOPROXY=
set GOROOT=C:/Golang/go1.22
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLCHAIN=auto
set GOTOOLDIR=C:\Golang\go1.22\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.22.2
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=C:\awsomeworks\awsomework_golang\sinewave_go\elst-api\go.mod
set GOWORK=
set CGO_CFLAGS=-O2 -g
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-O2 -g
set CGO_FFLAGS=-O2 -g
set CGO_LDFLAGS=-O2 -g
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=C:\Users\hzzhu2\AppData\Local\Temp\go-build1743840678=/tmp/go-build -gno-record-gcc-switches

What did you do?

When I build go linux script on Windows env, the build bat script is the following:

@echo off
set GOARCH=amd64
set GOOS=linux
go build -v -o rebuild_resource_title ../controllers/tools/rebuild_resource_title.go
pause

Because I build go program to Android sdk using GoMobile, so i change the go env args CGO_ENABLED=0 to CGO_ENABLED=1.

What did you see happen?

When I run the build bat, the error is the flowwing:

runtime/cgo
# runtime/cgo
gcc_linux_amd64.c: In function '_cgo_sys_thread_start':
gcc_linux_amd64.c:57:2: error: unknown type name 'sigset_t'; did you mean '_sigset_t'?
   57 |  sigset_t ign, oset;
      |  ^~~~~~~~
      |  _sigset_t
gcc_linux_amd64.c:62:2: error: implicit declaration of function 'sigfillset' [-Werror=implicit-function-declaration]
   62 |  sigfillset(&ign);
      |  ^~~~~~~~~~
gcc_linux_amd64.c:57:16: error: unused variable 'oset' [-Werror=unused-variable]
   57 |  sigset_t ign, oset;
      |                ^~~~
cc1.exe: all warnings being treated as errors

When I change CGO_ENABLED=1 to CGO_ENABLED=0, compiling successfully.

What did you expect to see?

  1. Why? Changing CGO_ENABLED=1 to CGO_ENABLED=0, Compilling successfully
  2. Is the first step the only method, changing the value of CGO_ENABLED to switch between different compilation requirements, is very troublesome!!
  3. Will this part be optimized in future releases?
@Zxilly
Copy link
Member

Zxilly commented Jul 17, 2024

You should set CC to a cross compiler for linux. Enabling CGO causes gc to compile C-based implementations using an external compiler, and I don't see what's so confusing about that, it's working as expected.

@zhuzaiye
Copy link
Author

You should set CC to a cross compiler for linux. Enabling CGO causes gc to compile C-based implementations using an external compiler, and I don't see what's so confusing about that, it's working as expected.

set GOARCH=amd64
set GOOS=linux
set CGO_ENABLED=0

Thanks! I add the set CGO_ENABLED=0 in bat script, compile succesful!
I learned a lot from that sentence: Enabling CGO causes gc to compile C-based implementations using an external compiler.

Thanks again.

@zhuzaiye
Copy link
Author

zhuzaiye commented Jul 18, 2024

To cross-compile a Go program for Linux on a Windows environment with CGO enabled, follow these steps:

  1. Install the Required Cross-Compiler:
  • You need to have a cross-compiler that targets the desired architecture (e.g., gcc for Linux).
  • For example, you can use MinGW for Windows to Linux cross-compiling: MinGW-w64.
  1. Set Environment Variables:
  • Set the GOOS and GOARCH environment variables to target the desired operating system and architecture.
  • Set CGO_ENABLED=1 to enable CGO.
  • Set the CC environment variable to point to the cross-compiler.
  1. Build the Go Binary:
  • Use the go build command to compile your program.
  1. example
set GOOS=linux
set GOARCH=amd64
set CGO_ENABLED=1
# set `CC` to a cross compiler for linux
set CC=x86_64-linux-gnu-gcc

go build -o your_program_name

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants