4

I am writing a Go wrapper for a C library in Go. The problem is, that the C library is not available on many Linux distributions, so I want a solution where i "go get github.com/me/mylibrary" does not require anybody to have the library installed.

One solution would be to just add the source of the library into a sub directory. Then when my project is build with go get I need to automatically build this library, too. But I have no idea how I can automate this.

Alternatively I could have a script that downloads the source, extracts and builds it

But I have no Idea how to connect these build steps with the go build tool.

linking a static library is also not the easiest.

#cgo linux LDFLAGS: ./MyLib/lib/libMyLib.a -lstdc++ -lm -lX11

works as long as i build from my library, but as soon as I want to build from another project the relative path is from that project and not from my library, so it fails.

Arne
  • 7,206
  • 4
  • 41
  • 60

1 Answers1

0

As per http://golang.org/cmd/cgo/#hdr-Using_cgo_with_the_go_command

When the Go tool sees that one or more Go files use the special import "C", it will look for other non-Go files in the directory and compile them as part of the Go package. Any .c, .s, or .S files will be compiled with the C compiler. Any .cc, .cpp, or .cxx files will be compiled with the C++ compiler.

So you can include the C library source in your repository and go will build it automatically. That page also explains how to pass build flags to the compilers and probably anything else you might need to know.

Evan
  • 5,701
  • 20
  • 26
  • so far I already know this, but the library I would like to wrap is not just a few files. It has it's own makefiles and I would like to use these. Additionally I don't want to mix my code with the code of the library just in case the library changes in the future. – Arne Apr 17 '14 at 23:09
  • 2
    then you have to create your own Makefile that prepares the other project for the final linking with go – fabrizioM Apr 18 '14 at 06:31