从C语言调用带有字符串参数的Go函数?

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

Call Go function with string parameter from C?

问题

我可以从C中调用一个没有参数的Go函数,如下所示。通过go build编译并打印输出:

  1. Hello from Golang main function!
  2. CFunction says: Hello World from CFunction!
  3. Hello from GoFunction!

main.go

  1. package main
  2. //extern int CFunction();
  3. import "C"
  4. import "fmt"
  5. func main() {
  6. fmt.Println("Hello from Golang main function!")
  7. //调用CFunction以便C调用GoFunction
  8. C.CFunction()
  9. }
  10. //export GoFunction
  11. func GoFunction() {
  12. fmt.Println("Hello from GoFunction!")
  13. }

file1.c

  1. #include <stdio.h>
  2. #include "_cgo_export.h"
  3. int CFunction() {
  4. char message[] = "Hello World from CFunction!";
  5. printf("CFunction says: %s\n", message);
  6. GoFunction();
  7. return 0;
  8. }

现在,我想将一个字符串/字符数组从C传递给GoFunction。

根据cgo文档中的"C references to Go"部分,这是可能的,所以我给GoFunction添加了一个字符串参数,并将字符数组message传递给GoFunction:

main.go

  1. package main
  2. //extern int CFunction();
  3. import "C"
  4. import "fmt"
  5. func main() {
  6. fmt.Println("Hello from Golang main function!")
  7. //调用CFunction以便C调用GoFunction
  8. C.CFunction()
  9. }
  10. //export GoFunction
  11. func GoFunction(str string) {
  12. fmt.Println("Hello from GoFunction!")
  13. }

file1.c

  1. #include <stdio.h>
  2. #include "_cgo_export.h"
  3. int CFunction() {
  4. char message[] = "Hello World from CFunction!";
  5. printf("CFunction says: %s\n", message);
  6. GoFunction(message);
  7. return 0;
  8. }

在执行go build时,我收到以下错误:

  1. ./file1.c:7:14: error: passing 'char [28]' to parameter of incompatible type 'GoString'
  2. ./main.go:50:33: note: passing argument to parameter 'p0' here

根据上述"C? Go? Cgo!"博客文章中的"strings and things"部分:
> 在Go和C字符串之间的转换是通过C.CString、C.GoString和C.GoStringN函数完成的。

但是这些函数是用于在Go中使用的,如果我想将字符串数据传递到Go中,它们并不是很有用。

英文:

I can call a Go function without parameters from C, per below. This compiles via go build and prints

  1. Hello from Golang main function!
  2. CFunction says: Hello World from CFunction!
  3. Hello from GoFunction!

main.go

  1. package main
  2. //extern int CFunction();
  3. import &quot;C&quot;
  4. import &quot;fmt&quot;
  5. func main() {
  6. fmt.Println(&quot;Hello from Golang main function!&quot;)
  7. //Calling a CFunction in order to have C call the GoFunction
  8. C.CFunction();
  9. }
  10. //export GoFunction
  11. func GoFunction() {
  12. fmt.Println(&quot;Hello from GoFunction!&quot;)
  13. }

file1.c

  1. #include &lt;stdio.h&gt;
  2. #include &quot;_cgo_export.h&quot;
  3. int CFunction() {
  4. char message[] = &quot;Hello World from CFunction!&quot;;
  5. printf(&quot;CFunction says: %s\n&quot;, message);
  6. GoFunction();
  7. return 0;
  8. }

Now, I want to pass a string/char array from C to GoFunction.

According to "C references to Go" in the cgo documentation this is possible, so I add a string parameter to GoFunction and pass the char array message to GoFunction:

main.go

  1. package main
  2. //extern int CFunction();
  3. import &quot;C&quot;
  4. import &quot;fmt&quot;
  5. func main() {
  6. fmt.Println(&quot;Hello from Golang main function!&quot;)
  7. //Calling a CFunction in order to have C call the GoFunction
  8. C.CFunction();
  9. }
  10. //export GoFunction
  11. func GoFunction(str string) {
  12. fmt.Println(&quot;Hello from GoFunction!&quot;)
  13. }

file1.c

  1. #include &lt;stdio.h&gt;
  2. #include &quot;_cgo_export.h&quot;
  3. int CFunction() {
  4. char message[] = &quot;Hello World from CFunction!&quot;;
  5. printf(&quot;CFunction says: %s\n&quot;, message);
  6. GoFunction(message);
  7. return 0;
  8. }

Upon go build I receive this error:

  1. ./file1.c:7:14: error: passing &#39;char [28]&#39; to parameter of incompatible type &#39;GoString&#39;
  2. ./main.go:50:33: note: passing argument to parameter &#39;p0&#39; here

According to the "strings and things" section of the above "C? Go? Cgo!" blog post:
> Conversion between Go and C strings is done with the C.CString, C.GoString, and C.GoStringN functions.

But these are for use in Go, and not helpful if I want to pass string data into Go.

答案1

得分: 6

在C中,字符串是*C.char类型,而不是Go中的string类型。请确保你的导出函数接受正确的C类型,并在Go中进行必要的转换:

  1. //export GoFunction
  2. func GoFunction(str *C.char) {
  3. fmt.Println("Hello from GoFunction!")
  4. fmt.Println(C.GoString(str))
  5. }
英文:

A string in C is a *C.char, not a Go string.
Have your exported function accept the correct C type, and convert it as necessary in Go:

  1. //export GoFunction
  2. func GoFunction(str *C.char) {
  3. fmt.Println(&quot;Hello from GoFunction!&quot;)
  4. fmt.Println(C.GoString(str))
  5. }

答案2

得分: 4

如果你想将一个只接受Go字符串的函数传递给一个C字符串,你可以在C端使用GoString类型:

  1. char message[] = "Hello World from CFunction!";
  2. printf("CFunction says: %s\n", message);
  3. GoString go_str = {p: message, n: sizeof(message)}; // 注意:sizeof(message)只适用于数组,不适用于指针。
  4. GoFunction(go_str);
  5. return 0;
英文:

If you want to pass a C string to a function that accepts only Go strings, you can use GoString type on the C side:

  1. char message[] = &quot;Hello World from CFunction!&quot;;
  2. printf(&quot;CFunction says: %s\n&quot;, message);
  3. GoString go_str = {p: message, n: sizeof(message)}; // N.B. sizeof(message) will
  4. // only work for arrays, not
  5. // pointers.
  6. GoFunction(go_str);
  7. return 0;

huangapple
  • 本文由 发表于 2016年9月27日 01:26:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/39708874.html
匿名

发表评论

匿名网友

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

确定