调用C语言中的Go函数时出现错误。

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

error on calling go function from c

问题

新来这里。尝试从C中调用go函数,但遇到了一些编译问题。

以下是go脚本:

package main
// #cgo CFLAGS: -Wno-error=implicit-function-declaration
// #include <stdlib.h> 
// #include "wrapper.c"
import "C"
//import "unsafe"
import "fmt"
//import "time"

//export dummy
func dummy() int {
    fmt.Println("hi you")
    return 0
}

func main() {
    C.testc()
}

以下是包装器:

#include <sys/types.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>

extern int dummy();

void testc(){
    dummy();
}

运行时,出现错误:

xyz@xyz-HP:~/learn/go$ go run reader.go 
# command-line-arguments
In file included from $WORK/command-line-arguments/_obj/_cgo_export.c:2:0:
reader.go:30:14: error: conflicting types for ‘dummy’
In file included from reader.go:3:0,
                 from $WORK/command-line-arguments/_obj/_cgo_export.c:2:
./wrapper.c:6:12: note: previous declaration of ‘dummy’ was here
/tmp/go-build528677551/command-line-arguments/_obj/_cgo_export.c:8:7: error: conflicting types for ‘dummy’
In file included from reader.go:3:0,
                 from $WORK/command-line-arguments/_obj/_cgo_export.c:2:
./wrapper.c:6:12: note: previous declaration of ‘dummy’ was here
英文:

New to go here. Tried to call go function from C but suffered some compiling issues

Here is the go script

package main
// #cgo CFLAGS: -Wno-error=implicit-function-declaration
// #include &lt;stdlib.h&gt; 
// #include &quot;wrapper.c&quot;
import &quot;C&quot;
//import &quot;unsafe&quot;
import &quot;fmt&quot;
//import &quot;time&quot;

//export dummy
func dummy() int {
	fmt.Println(&quot;hi you&quot;);
	return 0
}

func main() {
	C.testc()
}

Here is the wrapper

#include &lt;sys/types.h&gt;
#include &lt;stdio.h&gt;
#include &lt;errno.h&gt;
#include &lt;string.h&gt;

extern int dummy();

void testc(){
	dummy();
}

When running it, got error

xyz@xyz-HP:~/learn/go$ go run reader.go 
# command-line-arguments
In file included from $WORK/command-line-arguments/_obj/_cgo_export.c:2:0:
reader.go:30:14: error: conflicting types for ‘dummy’
In file included from reader.go:3:0,
                 from $WORK/command-line-arguments/_obj/_cgo_export.c:2:
./wrapper.c:6:12: note: previous declaration of ‘dummy’ was here
/tmp/go-build528677551/command-line-arguments/_obj/_cgo_export.c:8:7: error: conflicting types for ‘dummy’
In file included from reader.go:3:0,
                 from $WORK/command-line-arguments/_obj/_cgo_export.c:2:
./wrapper.c:6:12: note: previous declaration of ‘dummy’ was here

答案1

得分: 1

你不需要在你的C文件中声明dummy。这是我将你的代码拆分使其工作的方式。我将导出C函数放在了一个.h文件中,将函数体放在了一个.c文件中,并且只在Go代码中包含了头文件。

dummy.h:

void testc();

dummy.c:

#include <sys/types.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>


void testc(){
    dummy();
}

main.go:

package main

// #cgo CFLAGS: -Wno-error=implicit-function-declaration
// #include <stdlib.h>
// #include "dummy.h"
import "C"

import "fmt"

//export dummy
func dummy() int {
    fmt.Println("hi you")
    return 0
}

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

You don't need to declare dummy in your C file. Here's how I split your code to make it work. I put the C function exporting in an .h file, and the body itself in a .c file, and included only the h file in the go code.

dummy.h:

void testc();

dummy.c:

#include &lt;sys/types.h&gt;
#include &lt;stdio.h&gt;
#include &lt;errno.h&gt;
#include &lt;string.h&gt;


void testc(){
    dummy();
}

main.go:

package main

// #cgo CFLAGS: -Wno-error=implicit-function-declaration
// #include &lt;stdlib.h&gt;
// #include &quot;dummy.h&quot;
import &quot;C&quot;

import &quot;fmt&quot;

//export dummy
func dummy() int {
	fmt.Println(&quot;hi you&quot;)
	return 0
}

func main() {
	C.testc()
}

huangapple
  • 本文由 发表于 2015年4月13日 21:15:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/29606241.html
匿名

发表评论

匿名网友

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

确定