[英]package .: C source files not allowed when not using cgo or SWIG: main.c
For simplicity, I created two "Hello World" programs in the same directory, called main.go
and main.c
.为简单起见,我在同一目录中创建了两个“Hello World”程序,分别称为
main.go
和main.c
。
Golang:高朗:
package main
import "fmt"
func main() {
fmt.Println("Hello World!")
}
C: 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:但是正如你所看到的,VSCode 给了我一个错误,上面写着:
package .: C source files not allowed when not using cgo or SWIG: main.c
package .: 不使用 cgo 或 SWIG 时不允许使用 C 源文件:main.c
According to this answer , I have to remove and reinstall Go, and install the newer version that is above 1.5.根据这个答案,我必须删除并重新安装Go,并安装 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
:为了确保,我还删除了
$HOME
的go
目录:
$ 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
安装了它,但输出中没有go
包:
$ 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/
:然后我下载并解压
go1.15.3.linux-amd64.tar.gz
到/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:我在
$PATH
也有这个目录,因为我在~/.zshrc
文件中添加了这个export
命令:
export PATH=$PATH:/usr/local/go/bin
Now I close VSCode and reopen it, but the same error is there.现在我关闭 VSCode 并重新打开它,但还是出现了同样的错误。
One answer suggests that the $GOROOT
variable must be set to the right directory, currently this variable for me is empty.一个答案表明
$GOROOT
变量必须设置到正确的目录,目前这个变量对我来说是空的。 So I add this export
in my ~/.zshrc
file:所以我在我的
~/.zshrc
文件中添加了这个export
:
export GOROOT=/usr/local/go
But the error is still there.但是错误仍然存在。 How can I fix this?
我怎样才能解决这个问题?
You need to你需要
package main
import(
"fmt
)
import "C"
func main() {
...
}
and you need to change the c function name from main()
to something else in your C
file to avoid multiple definition of it when compiling.并且您需要将 c 函数名称从
main()
更改为C
文件中的其他名称,以避免在编译时对其进行多次定义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.