无法运行包含cgo内容的简单Go程序。

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

Can't run simple go program contains cgo stuff

问题

我正在尝试运行以下简单的Go代码,参考了简单的C代码(所有文件都位于同一个文件夹中):

main.go

  1. package main
  2. /*
  3. #include <stdlib.h>
  4. #include "lib.h"
  5. */
  6. import "C"
  7. import "unsafe"
  8. func Print(s string) {
  9. cs := C.CString(s)
  10. C.print_str(cs)
  11. C.free(unsafe.Pointer(cs))
  12. }
  13. func main() {
  14. str1 := "hi how are you\n"
  15. Print(str1)
  16. }

C代码:

lib.h

  1. void print_str(char *s);

lib.c

  1. #include "stdio.h"
  2. void print_str(char *s)
  3. {
  4. printf("%s\n", s?s:"nil");
  5. }

尝试在Windows和Linux上使用最新版本的Go(1.16.5)运行go build main.go,结果如下:

  1. # command-line-arguments
  2. /tmp/go-build3048423534/b001/_x002.o: In function `_cgo_976ac389362d_Cfunc_print_str':
  3. /tmp/go-build/cgo-gcc-prolog:61: undefined reference to `print_str'
  4. collect2: error: ld returned 1 exit status

我在这里漏掉了什么?

英文:

I'm trying to run the following simple go code with reference to simple C code (all files located in the same folder):

main.go

  1. package main
  2. /*
  3. #include &lt;stdlib.h&gt;
  4. #include &quot;lib.h&quot;
  5. */
  6. import &quot;C&quot;
  7. import &quot;unsafe&quot;
  8. func Print(s string) {
  9. cs := C.CString(s)
  10. C.print_str(cs)
  11. C.free(unsafe.Pointer(cs))
  12. }
  13. func main() {
  14. str1 := &quot;hi how are you\n&quot;
  15. Print(str1)
  16. }

The C code:

lib.h

  1. void print_str(char *s);

lib.c

  1. #include &quot;stdio.h&quot;
  2. void print_str(char *s)
  3. {
  4. printf(&quot;%s\n&quot;, s?s:&quot;nil&quot;);
  5. }

Tried to run go build main.go, both in Windows and Linux, with the latest version of go (1.16.5) and got:

  1. # command-line-arguments
  2. /tmp/go-build3048423534/b001/_x002.o: In function `_cgo_976ac389362d_Cfunc_print_str&#39;:
  3. /tmp/go-build/cgo-gcc-prolog:61: undefined reference to `print_str&#39;
  4. collect2: error: ld returned 1 exit status

What am I missing here?

答案1

得分: 3

首先,我编译了共享库:

  1. gcc -fPIC -shared lib.c -o lib.so

然后,在你的Go文件中添加了对该.so文件的引用:

  1. #cgo LDFLAGS: -L${SRCDIR} lib.so

参考链接:https://golang.org/cmd/cgo/

最终代码:

  1. package main
  2. /*
  3. #include <stdlib.h>
  4. #include <lib.h>
  5. #cgo LDFLAGS: -L${SRCDIR} lib.so
  6. */
  7. import "C"
  8. import "unsafe"
  9. func Print(s string) {
  10. cs := C.CString(s)
  11. C.print_str(cs)
  12. C.free(unsafe.Pointer(cs))
  13. }
  14. func main() {
  15. str1 := "hi how are you\n"
  16. Print(str1)
  17. }

以上是翻译好的内容。

英文:

First I compiled the shared lib
gcc -fPIC -shared lib.c -o lib.so

Then I added a reference to that .so in your go file
#cgo LDFLAGS: -L${SRCDIR} lib.so
(Ref: https://golang.org/cmd/cgo/)

  1. $ go run main.go
  2. hi how are you

Final code:

  1. package main
  2. /*
  3. #include &lt;stdlib.h&gt;
  4. #include &lt;lib.h&gt;
  5. #cgo LDFLAGS: -L${SRCDIR} lib.so
  6. */
  7. import &quot;C&quot;
  8. import &quot;unsafe&quot;
  9. func Print(s string) {
  10. cs := C.CString(s)
  11. C.print_str(cs)
  12. C.free(unsafe.Pointer(cs))
  13. }
  14. func main() {
  15. str1 := &quot;hi how are you\n&quot;
  16. Print(str1)
  17. }

huangapple
  • 本文由 发表于 2021年6月17日 19:40:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/68018602.html
匿名

发表评论

匿名网友

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

确定