构建一个简单的cgo模块时遇到的问题

huangapple go评论117阅读模式
英文:

Problems building a simple cgo module

问题

Ubuntu. vscode 1.62.1. go1.17.3. vscode go extension v0.29.0. delve v1.7.1.

我正在尝试使用vscode和vscode-go构建一个使用Cgo的小应用程序。只有一个模块导入了"C"。

我的项目结构中,根目录包含"go.mod"和"main.go"文件,我在子文件夹中有子包。我还有包含C语言构件的"include"和"lib"目录。

这是我在C模块中的代码:

package voltage

// #cgo CFLAGS: -g -Wall -Iinclude
// #cgo LDFLAGS: -Llib/linux -lvibesimple -lcurl -lssl -lvibecrypto -lvibeictk -lvibeserver
// #include <stdio.h>
// #include <errno.h>
// #include "veapi.h"
import "C"

func Encrypt(datatype string, data string) (result string) {
    return
}

func Decrypt(datatype string, data string) (result string) {
    return
}

在"Problems"视图中,显示了以下两个问题:

go list failed to return CompiledGoFiles. This may indicate failure to perform cgo processing; try building at the command line. See https://golang.org/issue/38990.

以及:

could not import C (cgo preprocessing failed) (compile)

我阅读了引用的问题,但我不确定如何处理这些信息。

我不确定如何继续下一步。

英文:

Ubuntu. vscode 1.62.1. go1.17.3. vscode go extension v0.29.0. delve v1.7.1.

I'm trying to a build a small app that uses Cgo, using vscode and vscode-go. Only one module imports "C".

My project structure has the root directory containing the "go.mod" and "main.go" files, and I have subpackages in subfolders. I also have "include" and "lib" directories that contain the C artifacts.

This is what I have so far in the C module:

package voltage

// #cgo CFLAGS: -g -Wall -Iinclude
// #cgo LDFLAGS: -Llib/linux -lvibesimple -lcurl -lssl -lvibecrypto -lvibeictk -lvibeserver
// #include &lt;stdio.h&gt;
// #include &lt;errno.h&gt;
// #include &quot;veapi.h&quot;
import &quot;C&quot;

func Encrypt(datatype string, data string) (result string) {
	return
}

func Decrypt(datatype string, data string) (result string) {
	return
}

In the "Problems" view, it shows the following two issues:

> go list failed to return CompiledGoFiles. This may indicate failure to perform cgo processing; try building at the command line. See https://golang.org/issue/38990.

And:

> could not import C (cgo preprocessing failed) (compile)

I read the cited issue, but I'm not sure what to do with that information.

I'm not sure how to move forward here.

答案1

得分: 4

C编译器不在源目录中执行,而是在一个临时目录中执行,该目录仅包含中间文件,例如将您的go文件编译为静态库(.a)。因此,LDFLAG“-Llib/linux”指向一个不存在的目录。

要解决此问题,只需将该标志替换为“-L${SRCDIR}/lib/linux”。

直接从cgo文档中摘录:

> 解析cgo指令时,任何出现的字符串${SRCDIR}都将被替换为包含源文件的目录的绝对路径。这允许将预编译的静态库包含在包目录中并正确链接。

> cgo工具将始终在包含路径中使用源文件的目录;即-I${SRCDIR}始终被隐含。

英文:

The C compiler is not executed in the source directory but in a temporary directory only containing intermediate files, such as your go files compiled as static libraries (.a). Therefore, the LDFLAG -Llib/linux points to an unexisting directory.

To solve this issue, just replace that flag with -L${SRCDIR}/lib/linux.

Directly from the cgo docs:

> When the cgo directives are parsed, any occurrence of the string ${SRCDIR} will be replaced by the absolute path to the directory containing the source file. This allows pre-compiled static libraries to be included in the package directory and linked properly.

> The cgo tool will always invoke the C compiler with the source file's directory in the include path; i.e. -I${SRCDIR} is always implied.

huangapple
  • 本文由 发表于 2021年11月12日 09:00:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/69936760.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定