gpt4 book ai didi

c - 在 Go 程序中使用 C 代码时未声明的标识符

转载 作者:IT王子 更新时间:2023-10-29 01:46:10 26 4
gpt4 key购买 nike

我正在尝试使用从 IRIS 网站下载的库。 makefile 包括创建动态和静态库的选项。我已经尝试了几个教程,将这两种类型的库与 cgo 一起使用,但没有成功。

这是我的代码

package main
/*
#cgo CFLAGS : -I .
#cgo LDFLAGS: -L . -llibslink
#include <libslink.h>
*/

import (
"C"
)

func main() {

C.sl_newslcd()

}

我的目录中有以下文件:

ChangeLog          config.o           globmatch.o        logging.c          slplatform.c       strutils.c
Makefile doc gswap.c logging.o slplatform.h strutils.o
Makefile.wat example gswap.o main.go slplatform.o unpack.c
Makefile.win genutils.c libslink.2.4.dylib msrecord.c slutils.c unpack.h
README genutils.o libslink.a msrecord.o slutils.o unpack.o
README.md globmatch.c libslink.dylib network.c statefile.c
config.c globmatch.h libslink.h network.o statefile.o

我的错误提示如下:go build -v main.go

command-line-arguments
# command-line-arguments
37: error: use of undeclared identifier 'SLCD'
37: error: use of undeclared identifier 'sl_newslcd'

最佳答案

您的主要问题是注释不是紧接在 import "C" as the documentation 之前建议:

If the import of "C" is immediately preceded by a comment, that comment, called the preamble, is used as a header when compiling the C parts of the package.

所以解决办法就是把comment和import之间的空行去掉。但是这不会编译,因为对于 -l 参数,lib 前缀被忽略。您必须指定 -lslink 而不是 -llibslink。最后,我建议将库放在某个子文件夹中,而不是放在与 .go 文件相同的目录中。

slink 的正确子文件夹的工作示例:

package main

// #cgo CFLAGS: -I libslink
// #cgo LDFLAGS: -L libslink -lslink
// #include <libslink.h>
import "C"

func main() {
C.sl_newslcd()
}

关于c - 在 Go 程序中使用 C 代码时未声明的标识符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26309071/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com