Export a Go function from a shared library when a C header file included for cgo also defines it

143 Views Asked by At

I'm trying to write a OpenVPN plugin in go. OpenVPN provides a interface as C header file.

The header contains a function signature

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

which i'm not able to able to implement.

How to create a function in go with the C dereference operator?

I tried

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() {}

which results into the following errors:

# 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
      |                                   ^~~~
0

There are 0 best solutions below