英文:
How to wrap a C "hello world" with argv using cgo?
问题
考虑以下的hello.c
代码:
#include <stdio.h>
int main(int argc, char* argv[]) {
printf("Hello, world! argv[0]=%s\n", argv[0]);
}
Makefile文件只包含一行代码:all: hello
。我该如何将这个函数包装(并编译)到一个Go程序中,就像这个(不工作的)示例中一样?
package main
// int main(int argc, char* argv[]);
import "C"
func main() {
argc := C.int(1)
argv := make([]*C.char, 1)
argv[0] = C.CString("hai")
C.main(argc, &argv[0])
}
我得到的错误是:
(virtualenv)[14:16:21][/tmp/cgo]$ ./main
fatal error: unexpected signal during runtime execution
[signal 0xb code=0x1 addr=0x1c000000000 pc=0x41d563]
runtime stack:
runtime.throw(0x48b400, 0x2a)
/usr/lib/golang/src/runtime/panic.go:527 +0x90
runtime.sigpanic()
/usr/lib/golang/src/runtime/sigpanic_unix.go:12 +0x5a
runtime.mHeap_Grow(0x6c7c20, 0x8, 0x0)
/usr/lib/golang/src/runtime/mheap.go:647 +0x243
runtime.mHeap_AllocSpanLocked(0x6c7c20, 0x1, 0x0)
/usr/lib/golang/src/runtime/mheap.go:532 +0x5f1
runtime.mHeap_Alloc_m(0x6c7c20, 0x1, 0x15, 0x0)
/usr/lib/golang/src/runtime/mheap.go:425 +0x1ac
runtime.mHeap_Alloc.func1()
/usr/lib/golang/src/runtime/mheap.go:484 +0x41
runtime.systemstack(0x7ffcb3aab958)
/usr/lib/golang/src/runtime/asm_amd64.s:278 +0xab
runtime.mHeap_Alloc(0x6c7c20, 0x1, 0x10000000015, 0x40a954)
/usr/lib/golang/src/runtime/mheap.go:485 +0x63
runtime.mCentral_Grow(0x6d0898, 0x0)
/usr/lib/golang/src/runtime/mcentral.go:190 +0x93
runtime.mCentral_CacheSpan(0x6d0898, 0x6c1180)
/usr/lib/golang/src/runtime/mcentral.go:86 +0x4d4
runtime.mCache_Refill(0x7fbc9971d050, 0x15, 0x40ca28)
/usr/lib/golang/src/runtime/mcache.go:118 +0xcf
runtime.mallocgc.func2()
/usr/lib/golang/src/runtime/malloc.go:611 +0x2b
runtime.systemstack(0x7ffcb3aabb28)
/usr/lib/golang/src/runtime/asm_amd64.s:278 +0xab
runtime.mallocgc(0x180, 0x476220, 0x0, 0x800000000)
/usr/lib/golang/src/runtime/malloc.go:612 +0x842
runtime.newobject(0x476220, 0x6c0c40)
/usr/lib/golang/src/runtime/malloc.go:760 +0x42
runtime.malg(0x8000, 0x6c0e40)
/usr/lib/golang/src/runtime/proc1.go:2188 +0x27
runtime.mpreinit(0x6c1180)
/usr/lib/golang/src/runtime/os1_linux.go:197 +0x1f
runtime.mcommoninit(0x6c1180)
/usr/lib/golang/src/runtime/proc1.go:114 +0x100
runtime.schedinit()
/usr/lib/golang/src/runtime/proc1.go:57 +0x79
runtime.rt0_go(0xc82002e008, 0x1, 0xc82002e008, 0x1, 0x40122a, 0xc82004fef8, 0x0, 0x8, 0x44fbaa, 0xc82004fe80, ...)
/usr/lib/golang/src/runtime/asm_amd64.s:109 +0x132
goroutine 1 [syscall, locked to thread]:
runtime.cgocall(0x401210, 0xc82004fef8, 0x0)
/usr/lib/golang/src/runtime/cgocall.go:120 +0x11b fp=0xc82004feb0 sp=0xc82004fe80
main._Cfunc_main(0x1, 0xc82002e008, 0x0)
command-line-arguments/_obj/_cgo_gotypes.go:49 +0x39 fp=0xc82004fef8 sp=0xc82004feb0
main.main()
/tmp/cgo/main.go:8 +0xb4 fp=0xc82004ff50 sp=0xc82004fef8
runtime.main()
/usr/lib/golang/src/runtime/proc.go:111 +0x2b0 fp=0xc82004ffa0 sp=0xc82004ff50
runtime.goexit()
/usr/lib/golang/src/runtime/asm_amd64.s:1696 +0x1 fp=0xc82004ffa8 sp=0xc82004ffa0
goroutine 17 [syscall, locked to thread]:
runtime.goexit()
/usr/lib/golang/src/runtime/asm_amd64.s:1696 +0x1
英文:
Consider the following hello.c
:
#include <stdio.h>
int main(int argc, char* argv[]) {
printf("Hello, world! argv[0]=%s\n", argv[0]);
}
Makefile only contains one line: all: hello
. How can I wrap (and compile) this function around a Go program, like in this (non-working) example?
package main
// int main(int argc, char* argv[]);
import "C"
func main() {
argc := C.int(1);
argv := make([]*C.char, 1)
argv[0] = C.CString("hai");
C.main(argc, &argv[0])
}
The error I'm getting is:
(virtualenv)[14:16:21][/tmp/cgo]$ ./main
fatal error: unexpected signal during runtime execution
[signal 0xb code=0x1 addr=0x1c000000000 pc=0x41d563]
runtime stack:
runtime.throw(0x48b400, 0x2a)
/usr/lib/golang/src/runtime/panic.go:527 +0x90
runtime.sigpanic()
/usr/lib/golang/src/runtime/sigpanic_unix.go:12 +0x5a
runtime.mHeap_Grow(0x6c7c20, 0x8, 0x0)
/usr/lib/golang/src/runtime/mheap.go:647 +0x243
runtime.mHeap_AllocSpanLocked(0x6c7c20, 0x1, 0x0)
/usr/lib/golang/src/runtime/mheap.go:532 +0x5f1
runtime.mHeap_Alloc_m(0x6c7c20, 0x1, 0x15, 0x0)
/usr/lib/golang/src/runtime/mheap.go:425 +0x1ac
runtime.mHeap_Alloc.func1()
/usr/lib/golang/src/runtime/mheap.go:484 +0x41
runtime.systemstack(0x7ffcb3aab958)
/usr/lib/golang/src/runtime/asm_amd64.s:278 +0xab
runtime.mHeap_Alloc(0x6c7c20, 0x1, 0x10000000015, 0x40a954)
/usr/lib/golang/src/runtime/mheap.go:485 +0x63
runtime.mCentral_Grow(0x6d0898, 0x0)
/usr/lib/golang/src/runtime/mcentral.go:190 +0x93
runtime.mCentral_CacheSpan(0x6d0898, 0x6c1180)
/usr/lib/golang/src/runtime/mcentral.go:86 +0x4d4
runtime.mCache_Refill(0x7fbc9971d050, 0x15, 0x40ca28)
/usr/lib/golang/src/runtime/mcache.go:118 +0xcf
runtime.mallocgc.func2()
/usr/lib/golang/src/runtime/malloc.go:611 +0x2b
runtime.systemstack(0x7ffcb3aabb28)
/usr/lib/golang/src/runtime/asm_amd64.s:278 +0xab
runtime.mallocgc(0x180, 0x476220, 0x0, 0x800000000)
/usr/lib/golang/src/runtime/malloc.go:612 +0x842
runtime.newobject(0x476220, 0x6c0c40)
/usr/lib/golang/src/runtime/malloc.go:760 +0x42
runtime.malg(0x8000, 0x6c0e40)
/usr/lib/golang/src/runtime/proc1.go:2188 +0x27
runtime.mpreinit(0x6c1180)
/usr/lib/golang/src/runtime/os1_linux.go:197 +0x1f
runtime.mcommoninit(0x6c1180)
/usr/lib/golang/src/runtime/proc1.go:114 +0x100
runtime.schedinit()
/usr/lib/golang/src/runtime/proc1.go:57 +0x79
runtime.rt0_go(0xc82002e008, 0x1, 0xc82002e008, 0x1, 0x40122a, 0xc82004fef8, 0x0, 0x8, 0x44fbaa, 0xc82004fe80, ...)
/usr/lib/golang/src/runtime/asm_amd64.s:109 +0x132
goroutine 1 [syscall, locked to thread]:
runtime.cgocall(0x401210, 0xc82004fef8, 0x0)
/usr/lib/golang/src/runtime/cgocall.go:120 +0x11b fp=0xc82004feb0 sp=0xc82004fe80
main._Cfunc_main(0x1, 0xc82002e008, 0x0)
command-line-arguments/_obj/_cgo_gotypes.go:49 +0x39 fp=0xc82004fef8 sp=0xc82004feb0
main.main()
/tmp/cgo/main.go:8 +0xb4 fp=0xc82004ff50 sp=0xc82004fef8
runtime.main()
/usr/lib/golang/src/runtime/proc.go:111 +0x2b0 fp=0xc82004ffa0 sp=0xc82004ff50
runtime.goexit()
/usr/lib/golang/src/runtime/asm_amd64.s:1696 +0x1 fp=0xc82004ffa8 sp=0xc82004ffa0
goroutine 17 [syscall, locked to thread]:
runtime.goexit()
/usr/lib/golang/src/runtime/asm_amd64.s:1696 +0x1
答案1
得分: 8
当我使用go run main.go
时,得到的结果与你一样,但是当我使用go build .
时,得到了一个更好的错误信息:
/tmp/go-build887090083/cgoSO/_obj/hello.o: In function `main':
/usr/include/x86_64-linux-gnu/bits/stdio2.h:104: multiple definition of `main'
/tmp/go-build887090083/cgoSO/_obj/_cgo_main.o:/tmp/go-build887090083/cgoSO/_obj/_cgo_main.c:1: first defined here
collect2: error: ld returned 1 exit status
然后我在go build
命令中添加了-work
标志,以保留临时工作空间,并且这是/tmp/go-build465152107/cgoSO/_obj/_cgo_main.c
的内容:
int main() { return 0; }
void crosscall2(void(*fn)(void*, int), void *a, int c) { }
void _cgo_wait_runtime_init_done() { }
char* _cgo_topofstack(void) { return (char*)0; }
void _cgo_allocate(void *a, int c) { }
void _cgo_panic(void *a, int c) { }
void _cgo_reginit(void) { }
第一行是在这里写的:https://github.com/golang/go/blob/f2e4c8b5fb3660d793b2c545ef207153db0a34b1/src/cmd/cgo/out.go#L49
并且没有条件,所以无法避免。
结论:你不能导入一个C程序,你只能导入库(这实际上是有道理的)。
如果这个C程序是你自己的代码,那么只需将main
改为其他任何名称,它就会工作。
英文:
I get the same result when using go run main.go
, but when using go build .
, you get a better error:
/tmp/go-build887090083/cgoSO/_obj/hello.o: In function `main':
/usr/include/x86_64-linux-gnu/bits/stdio2.h:104: multiple definition of `main'
/tmp/go-build887090083/cgoSO/_obj/_cgo_main.o:/tmp/go-build887090083/cgoSO/_obj/_cgo_main.c:1: first defined here
collect2: error: ld returned 1 exit status
I then added the -work
flag to go build
to persist the tmp workspace, and here is the content of /tmp/go-build465152107/cgoSO/_obj/_cgo_main.c
:
int main() { return 0; }
void crosscall2(void(*fn)(void*, int), void *a, int c) { }
void _cgo_wait_runtime_init_done() { }
char* _cgo_topofstack(void) { return (char*)0; }
void _cgo_allocate(void *a, int c) { }
void _cgo_panic(void *a, int c) { }
void _cgo_reginit(void) { }
That first line is written there: https://github.com/golang/go/blob/f2e4c8b5fb3660d793b2c545ef207153db0a34b1/src/cmd/cgo/out.go#L49
And there is no conditions, so it cannot be avoided.
Conclusion: You cannot import a C program, you can only import libraries (it actually makes sense).
If the C is your code, then just change main
for anything else and it will work.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论