Reputation: 4207
For simplicity, I created two "Hello World" programs in the same directory, called main.go
and main.c
.
Golang:
package main
import "fmt"
func main() {
fmt.Println("Hello World!")
}
C:
#include <stdio.h>
int main() {
printf("Hello World!\n");
return 0;
}
And both programs compile and run okay:
But as you can see, VSCode is giving me an error that says:
package .: C source files not allowed when not using cgo or SWIG: main.c
According to this answer, I have to remove and reinstall Go, and install the newer version that is above 1.5. So I ran this command to see where it's installed:
$ which go
/usr/local/go/bin/go
And then I removed that directory:
$ sudo rm -rf /usr/local/go
Now the command no longer works:
$ go version
zsh: command not found: go
$ which go
go not found
To make sure, I also deleted the go
directory in $HOME
:
$ sudo rm -rf $HOME/go
I also checked to see if I may have installed it through pacman
, but there's no go
package in the output:
$ pacman -Q | grep go
argon2 20190702-3
go-tools 2:1.14+3923+c00d67ef2-1
haskell-vector-algorithms 0.8.0.3-21
pango 1:1.44.7+11+g73b46b04-1
pangomm 2.42.1-2
Then I downloaded and extracted go1.15.3.linux-amd64.tar.gz
into /usr/local/
:
$ sudo tar -C /usr/local -xzf go1.15.3.linux-amd64.tar.gz
Now I can see that it's installed:
$ go version
go version go1.15.3 linux/amd64
$ which go
/usr/local/go/bin/go
I also have this directory in the $PATH
, as I add this export
command in my ~/.zshrc
file:
export PATH=$PATH:/usr/local/go/bin
Now I close VSCode and reopen it, but the same error is there.
One answer suggests that the $GOROOT
variable must be set to the right directory, currently this variable for me is empty. So I add this export
in my ~/.zshrc
file:
export GOROOT=/usr/local/go
But the error is still there. How can I fix this?
Upvotes: 1
Views: 2164
Reputation: 33
Have you tried adding this as the first line to your .c file:
//go:build ignore
This should tell go build
to ignore the .c files.
Upvotes: 0
Reputation: 629
"C source files not allowed when not using cgo or SWIG" that means you have a main.c file in the same folder of main.go, but you are not using it like cgo
Solution: Put main.c in another folder
Upvotes: 2