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: Wndows: Linking .dll files compiles fine but throws error during runtime #23897

Closed
datNurd opened this issue Feb 17, 2018 · 8 comments
Closed

Comments

@datNurd
Copy link

datNurd commented Feb 17, 2018

Please answer these questions before submitting your issue. Thanks!

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

go version go1.10 windows/386

Does this issue reproduce with the latest release?

Yes

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

Windows 10, 64 bit

What did you do?

Tried to link a cutom C library using

package main

/*
	#cgo CFLAGS: -I./lib
	#cgo LDFLAGS: -L./lib -lkeyboard
	#include <keyboard.h>
*/
import "C"
import "fmt"

func main() {
	fmt.Printf("Hello word\n")
	C.InitKeyboard()

	fmt.Printf("\nEnter: ")

	for {
		r := C.GetCharacter()

		fmt.Printf("%c", r)

		if r == 'q' {
			break
		}
	}

	C.Closeboard()
}

If possible, provide a recipe for reproducing the error.
A complete runnable program is good.
A link on play.golang.org is best.

What did you expect to see?

The built .exe file to run

What did you see instead?

The program compiles/builds with out any error but throws an error at runtime(i.e while executing the .exe file) "cannot find libkeybaord.dll", but works/runs fine if the libkeybaord.dll is placed in the same directory as the .go file, so the issue here is I want to understand why its unable to search/locate libkeybaord.dll file present in the 'lib folder' during runtime whereas it is able to locate the libkeyboard.dll in 'lib' folder and build successfully during compile time.
Directory structure:
src
|_hello.go
|_lib
----|_keyboard.h
----|_keyboard.c
----|_libkeyboard.dll

@alexbrainman
Copy link
Member

i want to understand why its unable to search/locate libkeybaord.dll file present in the 'lib folder'

I suspect Windows will search your 'lib folder', if you help Windows and add 'lib folder' to you PATH.

Alex

@datNurd
Copy link
Author

datNurd commented Feb 17, 2018

@alexbrainman Yes I figured that out, my concern is, will it not be difficult to add a new path every time I code a new project using some 3rd party .dll file ? Shouldn't the built programs automatically search paths added in LDFLAGS ? because that's what normally happens in other languages I believe. Let's say C for example, when you compile a program using gcc using -L and -l flag it automatically fetches the dynamic library files from path specified using -L during runtime.

@AlexRouSg
Copy link
Contributor

Please read https://msdn.microsoft.com/en-us/library/windows/desktop/ms682586(v=vs.85).aspx for where and the order windows searches for dlls.

I have never seen C/C++/C# look for dlls in the -L path at runtime. It will only look for the import library or static link libraries at the -L path.

@datNurd
Copy link
Author

datNurd commented Feb 17, 2018

@AlexRouSg sorry for the miss quote , I would like to correct, C fetches the static link library from the -L path. So I've also created a static link library keyboard.a in the same lib folder, is there a possibility to make go fetch this static library during runtime ? I am new to go so trying to figure out a few things.

So, I am just trying to figure out a way to link the C libraries with my go code with out need to manually set paths by the user.

@AlexRouSg
Copy link
Contributor

Static link libraries are linked at compile time (C also does this), there is nothing to load at runtime. Just make sure you are setting the right flags to link the static lib and not the dynamic lib.

@datNurd
Copy link
Author

datNurd commented Feb 17, 2018

@AlexRouSg can you help me figure out which flags to use for static linking? or post a link to relevant documentation or tutorial ?

I've tried building using go build -ldflags "-linkmode external -extldflags -static" hello.go build still get the same issue.

@AlexRouSg
Copy link
Contributor

At this point I think it's pretty clear this is not a bug so I suggest you to ask around in the proper place for help as the issue tracker is not normally used for asking questions. https://github.com/golang/go/wiki/Questions

If keyboard is your own project, since you want to static link anyway, you could just put the C source files into the same folder as your go files and they will be compiled together without having to set any import or link options.

@alexbrainman
Copy link
Member

Closing, because it is not a bug.

Alex

@mikioh mikioh changed the title Wndows: Linking .dll files compiles fine but throws error during runtime cmd/go: Wndows: Linking .dll files compiles fine but throws error during runtime Feb 21, 2018
@golang golang locked and limited conversation to collaborators Feb 21, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants