在Go程序中使用C代码时出现未声明的标识符。

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

Undeclared Identifiers when using C code in a Go Program

问题

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

这是我的Go代码:

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'
英文:

I am trying to use a library downloaded from the IRIS website. The makefile includes options to create dynamic and static libraries. I have tried a couple tutorials out there using both types of library with cgo and I have been unsuccessful.

Here is my go code

package main
/*
#cgo CFLAGS : -I .
#cgo LDFLAGS: -L . -llibslink
#include &lt;libslink.h&gt;
*/

import (
	&quot;C&quot;
)

func main() {

	C.sl_newslcd()

}

And I have the following files in the directory:

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

My error messages are as follows on the command: go build -v main.go

command-line-arguments
# command-line-arguments
37: error: use of undeclared identifier &#39;SLCD&#39;
37: error: use of undeclared identifier &#39;sl_newslcd&#39;

答案1

得分: 1

你的主要问题是注释没有紧跟在import "C"之前,正如文档所建议的那样:

如果"C"的导入紧跟在注释之后,那么该注释被称为前导,将在编译包的C部分时用作头部。

因此,解决方案是删除注释和导入之间的空行。不过,这样还不能编译,因为对于-l参数,lib前缀会被忽略。你需要指定-lslink而不是-llibslink。最后,我建议将库放在某个子文件夹中,而不是与你的.go文件放在同一个目录中。

以下是使用正确子文件夹libslink的工作示例:

package main

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

func main() {
    C.sl_newslcd()
}
英文:

Your main problem is that the comment is not immediately preceding the import &quot;C&quot; as the documentation recommends:

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

So the solution is to remove the blank line between the comment and the import. This will not compile though, since for the -l parameter the lib prefix is ignored. You have to specify -lslink instead of -llibslink. At last, I recommend to have the library in some sub-folder rather than in the same directory as your .go files.

Working example with proper sub-folder for slink:

package main

// #cgo CFLAGS: -I libslink
// #cgo LDFLAGS: -L libslink -lslink
// #include &lt;libslink.h&gt;
import &quot;C&quot;

func main() {
    C.sl_newslcd()
}

huangapple
  • 本文由 发表于 2014年10月11日 06:41:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/26309071.html
匿名

发表评论

匿名网友

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

确定