Reputation: 7910
When I try to pass a C.int from package main
to a function in a helper package called common
, I get the following error:
main.go:24: cannot use argc (type C.int) as type common.C.int in argument to common.GoStrings
From common.go
:
/*
...
*/
import "C"
...
func GoStrings(argc C.int, argv **C.char) (args []string) {
// do stuff
}
From 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)
...
}
Is there any way to pass these objects back and forth? I've tried type casting to e.g. common.C.int
, but that doesn't seem to be valid syntax. I'd like to be able to call GoStrings
from multiple different main programs, and it seems like that should be allowable.
Upvotes: 0
Views: 484
Reputation: 1798
Unfortunately you can't pass C types between packages. You'll need to perform any necessary type conversions within the package that is importing the C types. As per the documentation:
Cgo translates C types into equivalent unexported Go types. Because the translations are unexported, a Go package should not expose C types in its exported API: a C type used in one Go package is different from the same C type used in another.
If you have common C translation methods that you use, consider using go generate
with a helper script to create these in each package where it is required from a master source file. Not as nice as solution as having a common library but much better than manually updating files in multiple packages.
Upvotes: 3