英文:
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 <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()
}
Here is the wrapper
#include <sys/types.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
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 <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()
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论