当我尝试从包main
传递C.int给函数在一个辅助包叫做common
,我得到以下错误:如何在go包之间传递C对象?
main.go:24: cannot use argc (type C.int) as type common.C.int in argument to common.GoStrings
从common.go
:
/*
...
*/
import "C"
...
func GoStrings(argc C.int, argv **C.char) (args []string) {
// do stuff
}
从main.go
:
/*
#cgo LDFLAGS: -lpam -fPIC
#define PAM_SM_AUTH
#include <security/pam_appl.h>
*/
import "C"
...
func pam_sm_authenticate(pamh *C.pam_handle_t, flags, argc C.int, argv **C.char) C.int {
args := common.GoStrings(argc, argv)
...
}
有什么办法来回传递这些对象吗?我已经尝试过将类型转换为例如common.C.int
,但这似乎不是有效的语法。我希望能够从多个不同的主程序中调用GoStrings
,看起来这应该是可以允许的。
对于这种情况,我通常会传递'unsafe.Pointer'。这有效,但你最好确保做到理智和错误检查! –