0

I'm currently testing the Go-SDL2 lib, just for fun. I gave the binary to one of my friend but he doesn't have the SDL installed on his machine. So all I want to do (is dance) is to distribute the 4 .so libs with the binary so that it will work fine on other Linux machines. It's pretty easy actually, I just have to set the LD_LIBRARY_PATH to point to the current folder. This is for testing purpose.

The problem is, I have to set this environment variable before I can import the go-sdl2 lib. For now I have a single source file (main.go obviously).

How can I achieve this ? (Is it even possible ?)

Depado
  • 4,811
  • 3
  • 41
  • 63

2 Answers2

1

One option is to have a script which sets the LD_LIBRARY_PATH environment variable before calling go-sdl2 (in the same script).

The other more interesting option is to use a Docker image, make a Dockerfile based on that image, and install SDL and go in it (like in didstopia/sdl2 ad its Dockerfile, combined with a Golang Dockerfile).

You then have a reproducible standard environment, where you don't need to change LD_LIBRARY_PATH. And you can export that image in order for your friend to experiment with it.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Yeah I already saw the bash script method. I wish there would be a pure go-way of doing that because Go offers the ability to modify environement variables. The docker technique is interesting although I'm not sure it's really the most portable way of doing things (since the user don't have to install SDL, but he'll have to install docker to make it work) – Depado Jun 23 '15 at 11:40
  • @Depado Docker seems very portable (provided, indeed, that you have Docker installed). The resulting container is reproducible at will. – VonC Jun 23 '15 at 11:49
  • Yes that's right. But, on the other hand, the user still have something to install. I was hoping for a pure Go solution though. – Depado Jun 23 '15 at 11:57
0

You could, in main, check to see if LD_LIBRARY_PATH is set, and if not, relaunch (using: os.exec)yourself adding the environment variable.

you basically want to use os.Args as your arguments to exec, but also take your environment and add in LD_LIBRARY_PATH

Bash script is definitely a less funky way to do it. But if you really want the go app to do this, you can.

You want something like: (untested)

cmd := os.Command(os.Args[0],os.Args[1:]...)
cmd.Env = append(os.Environ, "LD_LIBRARY_PATH=./wherever")
cmd.StdErr = os.StdErr // repeat for StdIn/StdOut
err := cmd.Run() //blocks until sub process is complete
if err != nil {
   os.Exit(1)
}

Something like that

David Budworth
  • 11,248
  • 1
  • 36
  • 45
  • Basically it's the same technique than using bash. I can't fix it in main because the main.go files imports the go-sdl2 lib, therefore it can't run if said libs aren't found (doesn't even enter in the main method) – Depado Jun 23 '15 at 12:34
  • 1
    @Depado, "because the main.go files imports …"; No, `main.go` does not do any importing. The compiler imports things at compile time. The resulting binary is just a chunk of code, either staticly linked so that it includes all code it needs, or dynamically linked with references to external libraries which the [operating system loads](https://en.wikipedia.org/wiki/Dynamic_linking#ELF-based_Unix-like_systems) **before** the program itself starts running. – Dave C Jun 23 '15 at 15:27
  • Maybe you can static link in sdl2, so no .so files are needed? http://stackoverflow.com/questions/17620884/static-linking-of-sdl2-libraries for reference – David Budworth Jun 23 '15 at 16:03
  • According to the issue I opened on the repository of go-sdl2, there is no way to static-link a Go program with a C library. See here : https://github.com/veandco/go-sdl2/issues/129 – Depado Jun 24 '15 at 14:06
  • Update, see the above issue : github.com/veandco/go-sdl2/issues/129 There is actually a way to static-link the SDL2 to a Go binary :) – Depado Jun 29 '15 at 16:00