undefined: syscall.LoadLibrary

1,131 views
Skip to first unread message

an...@webkr.de

unread,
Jun 23, 2016, 10:16:25 PM6/23/16
to golang-nuts
On https://github.com/golang/go/wiki/WindowsDLLs it says:

> First way: Dynamically load a dll, then call a method on it. You can call the method via "syscallXX" (the XX is number of parameters, but if it has few than that, like if you need seven parameter, then syscall9 will still work, you just tell it the number of arguments is 7). *This way also works with Linux shared libraries, as well, if you're targeting linux.*

The page then proceeds to show code that essentially does:

kernel32, _        = syscall.LoadLibrary("kernel32.dll")
getModuleHandle, _ = syscall.GetProcAddress(kernel32, "GetModuleHandleW")
syscall.Syscall(uintptr(getModuleHandle), nargs, 0, 0, 0)

But when I try this on linux I get:

./main.go:9: undefined: syscall.LoadLibrary

So, how exactly does this method work with Linux shared libraries?

Ian Lance Taylor

unread,
Jun 23, 2016, 10:38:24 PM6/23/16
to an...@webkr.de, golang-nuts
Well, it doesn't. I'm really not sure what that sentence was intended
to mean. I just remove it from the wiki page.

On GNU/Linux you can use the cgo method mentioned at the very bottom
of the page. No other mechanism is available at present.

Ian

Andrew Mezoni

unread,
Jun 24, 2016, 6:19:36 AM6/24/16
to golang-nuts
>> But when I try this on linux I get:

Linux does not have a `LoadLibrary`
Also Linux does not have `kernel32.dll` and so on.

P.S.
The `LoadLibrary` also in `kernel32.dll` library which are only on Windows.

Andrew Mezoni

unread,
Jun 24, 2016, 6:22:23 AM6/24/16
to golang-nuts
>> So, how exactly does this method work with Linux shared libraries?

It does not work on Linux.
On Linux you should use the methods:

dlclose, dlopen, dlmopen - open and close a shared object

Konstantin Khomoutov

unread,
Jun 24, 2016, 6:46:19 AM6/24/16
to Andrew Mezoni, golang-nuts
Note that these calls are provided by libc, not by the kernel (that's
why their manual pages are in section 3 BTW) so using them requires
using cgo -- contrary to syscall.LoadLibary() on Windows.

I don't know whether it's at all possible to have something like
dlopen() without touching libc on Linux (and other non-Windows
platforms) because that ultimately requires running the dynamic linker
program (well, at least that's my understanding).
Reply all
Reply to author
Forward
0 new messages