dpr77335 2017-07-24 13:50
浏览 77


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.

  • 写回答

1条回答 默认 最新

  • douxu5845 2017-07-24 14:02

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥30 关于#python#的问题:我想要的是这79个大特征对于房屋售价的最大的影响前十名(相关搜索:随机森林)
  • ¥15 使用matlab计算自定义特殊函数的二重积分,改变积分顺序所得的结果不同的问题?
  • ¥15 mysql做碎片化处理老是报错怎么办
  • ¥15 如何正确在vs2010中初始化map对象
  • ¥30 mmdet3d模型部署问题
  • ¥15 comsol仿真反射率、吸收率时峰值位置和深度不对!
  • ¥30 Visual Studio找不到sdk,如何解决?
  • ¥15 labview与机器之间网口通信发送指令
  • ¥15 迪杰斯特拉算法,考虑时延,带宽
  • ¥30 C#提交json字符串给http服务器