You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Go编写OpenVPN插件:cgo导出函数与C头文件定义冲突问题

用Go编写OpenVPN插件时遇到类型冲突编译错误

问题背景

我尝试用Go开发OpenVPN插件,OpenVPN提供的C头文件中定义了核心接口函数:

int openvpn_plugin_open_v3(const int version, struct openvpn_plugin_args_open_in const *arguments, struct openvpn_plugin_args_open_return *retptr)

我不清楚如何在Go中正确实现带有const限定符的C函数,编写代码后编译出现类型冲突错误。

实现的Go代码

package main

/*
#define OPENVPN_WRAPPER_H
#include "./openvpn-plugin.h"
*/
import "C"
import (
    "fmt"
    "os"
)

const OPENVPN_PLUGIN_STRUCTVER_MIN = 5

//export openvpn_plugin_open_v3
func openvpn_plugin_open_v3(
    version C.int,
    arguments *C.struct_openvpn_plugin_args_open_in,
    retptr *C.struct_openvpn_plugin_args_open_return,
) C.int {
    if version < OPENVPN_PLUGIN_STRUCTVER_MIN {
        fmt.Fprintf(os.Stderr, "%d: this plugin is incompatible with the running version of OpenVPN\n", OPENVPN_PLUGIN_STRUCTVER_MIN)
        return C.OPENVPN_PLUGIN_FUNC_ERROR
    }

    return C.OPENVPN_PLUGIN_FUNC_SUCCESS
}

func main() {}

编译错误信息

# github.com/jkroepke/openvpn-auth-oauth2/lib
In file included from _cgo_export.c:4:
cgo-gcc-export-header-prolog:49:12: error: conflicting types for 'openvpn_plugin_open_v3'
In file included from openvpn.go:5,
                 from _cgo_export.c:4:
././openvpn-plugin.h:628:44: note: previous declaration of 'openvpn_plugin_open_v3' was here
  628 | OPENVPN_PLUGIN_DEF int OPENVPN_PLUGIN_FUNC(openvpn_plugin_open_v3)
      |                                            ^~~~~~~~~~~~~~~~~~~~~~
././openvpn-plugin.h:184:35: note: in definition of macro 'OPENVPN_PLUGIN_FUNC'
  184 | #define OPENVPN_PLUGIN_FUNC(name) name
      |                                   ^~~~
_cgo_export.c:24:5: error: conflicting types for 'openvpn_plugin_open_v3'
   24 | int openvpn_plugin_open_v3(int version, struct openvpn_plugin_args_open_in* arguments, struct openvpn_plugin_args_open_return* retptr)
      |     ^~~~~~~~~~~~~~~~~~~~~~
In file included from openvpn.go:5,
                 from _cgo_export.c:4:
././openvpn-plugin.h:628:44: note: previous declaration of 'openvpn_plugin_open_v3' was here
  628 | OPENVPN_PLUGIN_DEF int OPENVPN_PLUGIN_FUNC(openvpn_plugin_open_v3)
      |                                            ^~~~~~~~~~~~~~~~~~~~~~
././openvpn-plugin.h:184:35: note: in definition of macro 'OPENVPN_PLUGIN_FUNC'
  184 | #define OPENVPN_PLUGIN_FUNC(name) name
      |                                   ^~~~

疑问

如何在Go中正确实现带有C语言const限定符的OpenVPN插件函数,解决当前的类型冲突问题?


内容的提问来源于stack exchange,提问作者jkroepke

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 07:06:34