简体   繁体   English

package .: 不使用 cgo 或 SWIG 时不允许使用 C 源文件:main.c

[英]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.gomain.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:两个程序都可以编译并运行:

Golang 和 C 中的你好世界

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 :为了确保,我还删除了$HOMEgo目录:

$ 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.

相关问题 Golang - 不使用cgo时不允许使用C源文件 - Golang - C source files not allowed when not using cgo 将库(来自程序集文件)与Makefile中的main.c链接 - Linking library (from assembly files) with main.c in Makefile 如何解决 GNU make 产生“循环 main.c”错误 - How to resolve GNU make producing a “circular main.c” error 如何将我的 main.c 文件与我的 library.a 文件链接? - How to link my main.c file with my library .a file? 如何在脚本中运行gcc命令到exe main.c - how to run gcc command in script to exe main.c sccz80:&quot;../lib/main.c&quot; L:16 警告:#14:预期的 &#39;,&#39; sccz80:&quot;../lib/main.c&quot; L:16 错误:#28:分段错误 - sccz80:"../lib/main.c" L:16 Warning:#14:Expected ',' sccz80:"../lib/main.c" L:16 Error:#28:Segmentation fault 使用SWIG为perl创建C ++接口吗? - Create C++ interface for perl using SWIG? 使用wxWidgets,Linux,C ++构建源文件时出错 - Error when building source files with wxWidgets , Linux, C++ 使用C ++在Linux上锁定/阻止对源文件的编辑 - Lock / Prevent edit of source files on Linux using C++ 使用 dpkg 安装时“未安装软件包 swig”,但在终端中使用 swig -version 可见? - 'Package swig is not installed' when installing with dpkg, but visible with swig -version in terminal?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM