Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/go: missing entry in c-shared .dll #42409

SamuelYvon opened this issue Nov 5, 2020 · 3 comments

cmd/go: missing entry in c-shared .dll #42409

SamuelYvon opened this issue Nov 5, 2020 · 3 comments


Copy link

@SamuelYvon SamuelYvon commented Nov 5, 2020

What version of Go are you using (go version)?

$ go version
go version go1.15.3 windows/amd64

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\samue\AppData\Local\go-build
set GOENV=C:\Users\samue\AppData\Roaming\go\env
set GOEXE=.exe
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOMODCACHE=C:\Users\samue\go\gopath\pkg\mod
set GOOS=windows
set GOPATH=C:\Users\samue\go\gopath
set GOPROXY=,direct
set GOROOT=C:\Users\samue\sdk\go1.15.3
set GOTOOLDIR=C:\Users\samue\sdk\go1.15.3\pkg\tool\windows_amd64
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set GOMOD=
set CGO_CFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\samue\AppData\Local\Temp\go-build596418074=/tmp/go-build -gno-record-gcc-switches

What did you do?

I am trying to compile my code to a shared library (specifically a dll on Windows; there is no issue on Linux).

Here is what I found to be a minimally reproducible example.

package main

import "C"
import "fmt"
import ""

//export HelloWorld
func HelloWorld() *C.char {
	var err error
	fmt.Print("Hello world!")

	devices := hid.Enumerate(0, 0)

	for _ = range devices {
		fmt.Printf("Found a device\n")

	if nil != err {
		return C.CString(err.Error())
	} else {
		return C.CString("Hello World!")

func main() {

I then compile the shared library using the following commands:

go build -buildmode=c-shared -o main.dll main.go

I get the following files:

  • main.dll
  • main.h

Here is the header file:

/* Code generated by cmd/cgo; DO NOT EDIT. */

/* package command-line-arguments */

#line 1 "cgo-builtin-export-prolog"

#include <stddef.h> /* for ptrdiff_t below */


typedef struct { const char *p; ptrdiff_t n; } _GoString_;


/* Start of preamble from import "C" comments.  */

/* End of preamble from import "C" comments.  */

/* Start of boilerplate cgo prologue.  */
#line 1 "cgo-gcc-export-header-prolog"


typedef signed char GoInt8;
typedef unsigned char GoUint8;
typedef short GoInt16;
typedef unsigned short GoUint16;
typedef int GoInt32;
typedef unsigned int GoUint32;
typedef long long GoInt64;
typedef unsigned long long GoUint64;
typedef GoInt64 GoInt;
typedef GoUint64 GoUint;
typedef __SIZE_TYPE__ GoUintptr;
typedef float GoFloat32;
typedef double GoFloat64;
typedef float _Complex GoComplex64;
typedef double _Complex GoComplex128;

  static assertion to make sure the file is being used on architecture
  at least with matching size of GoInt.
typedef char _check_for_64_bit_pointer_matching_GoInt[sizeof(void*)==64/8 ? 1:-1];

typedef _GoString_ GoString;
typedef void *GoMap;
typedef void *GoChan;
typedef struct { void *t; void *v; } GoInterface;
typedef struct { void *data; GoInt len; GoInt cap; } GoSlice;


/* End of boilerplate cgo prologue.  */

#ifdef __cplusplus
extern "C" {

extern char* HelloWorld();

#ifdef __cplusplus

The function is declared in the header, as expected. However,

rundll32.exe main.dll,HelloWorld

I get an ErrorBox:

Expectations / things that would show it works

I would expect this error box not to show up basically. If I remove the dependency to the HID library it works, the call is made. I believe this might be a bug with Go and not the library because it works on Linux and I get an otherwise valid header file, and basically no relevant warning while compiling.

I would have expected that if no entry was to be created, I would get a compilation error / warning / advisory. I do get a warning while compiling but it's from the included library. The same warning occurs while building the app (which works). I am including it here just in case, but I do not believe it has anything to do with the issue; it's a fairly standard warning when using strncpy

In file included from C:\Users\samue\go\gopath\src\\karalabe\hid\hid_enabled.go:40:
C:\Users\samue\go\gopath\src\\karalabe\hid/hidapi/windows/hid.c: In function 'hid_enumerate':
C:\Users\samue\go\gopath\src\\karalabe\hid/hidapi/windows/hid.c:431:5: warning: 'strncpy' specified bound depends on the length of the source argument [-Wstringop-overflow=]
     strncpy(cur_dev->path, str, len+1);
C:\Users\samue\go\gopath\src\\karalabe\hid/hidapi/windows/hid.c:429:11: note: length computed here
     len = strlen(str);
Copy link

@AlexRouSg AlexRouSg commented Nov 6, 2020

Loads and runs 32-bit dynamic-link libraries (DLLs).
Rundll32 can only call functions from a DLL explicitly written to be called by Rundll32.

As you are on windows/amd64 Go by default builds a 64 bit dll.
Try building a 32 bit dll (set GOARCH=386) and/or calling the dll from a C program you wrote.

Copy link

@SamuelYvon SamuelYvon commented Nov 6, 2020

Calling the DLL from an external program does not work, I get a java.lang.UnsatisfiedLinkError: Error looking up function '...': The specified procedure could not be found. using JNA. I can try a simpler C program later if we want to rule out JNA.

While rundll32 does not correctly run the function, it does seem to detect the missing entry regardless. I aIso first tried with Dependency Walker which does not find anything either:

(without the HID library)

(with the HID library)

I did not try 32 bit, my toolchain seems to be broken somewhere.

Copy link

@AlexRouSg AlexRouSg commented Nov 6, 2020

@toothrot toothrot added the OS-Windows label Nov 6, 2020
@toothrot toothrot changed the title Build: Missing entry in c-shared .dll cm/dgo: missing entry in c-shared .dll Nov 6, 2020
@toothrot toothrot added this to the Backlog milestone Nov 6, 2020
@toothrot toothrot changed the title cm/dgo: missing entry in c-shared .dll cmd/go: missing entry in c-shared .dll Nov 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.