英文:
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 <libslink.h>
*/
import (
"C"
)
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 'SLCD'
37: error: use of undeclared identifier 'sl_newslcd'
答案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 "C"
as the documentation recommends:
> 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.
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 <libslink.h>
import "C"
func main() {
C.sl_newslcd()
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论