0

I am planning to write a cross-platform app that has most of its functionality shared across all platforms (Linux, OS X, Windows, iOS, Android). These are mostly helper function (calculations, internal lists, networking etc.) so I figured it would be convenient to have those functions in a library I can compile for every platform while still being able to create custom UI for each platform individually.

Dominant languages across those platforms I mentioned are C, Objective-C, C# and Java. All these languages support calling C-API functions from a library either directly or via internal wrappers. Since I don't want to write 80% of my application's code in C/C++, I searched and found Go.

cgo seems to be the solution for my problem.
My current thought is to code the core library in Go and then compile it for each platform, however, invoking go build does not create anything at all.
I import "C".
I have declared a func and added the //export statement before.

I read about gccgo but people keep pointing out that it is outdated and should not be used.

Maybe anyone can point out a flaw in my thoughts or help me bring this library file together. Thanks in advance.

Borgleader
  • 15,826
  • 5
  • 46
  • 62
Sense
  • 181
  • 2
  • 11
  • 1
    `cgo` is how you call into C libraries from Go. It does not create C libraries. – JimB Jun 01 '15 at 18:12
  • 2
    Go will support various build modes in go1.5. If you're using the development version, you need to show an example of what you're trying to do. – JimB Jun 01 '15 at 18:18
  • 2
    `go build -h` says "When the command line specifies a single main package, build writes the resulting executable to output. Otherwise build **compiles the packages but discards the results**, serving only as a check that the packages can be built." You want `go install` (and make sure you've read [How to Write Go Code](https://golang.org/doc/code.html#Library)). – Dave C Jun 01 '15 at 19:49
  • That `cgo` is only for calling `C` from `go` is actually not completely true: https://golang.org/cmd/cgo/#hdr-C_references_to_Go. `go install` did not create anything either, but thanks for the suggestion. **However, Go 1.5 looks really promising.** – Sense Jun 02 '15 at 15:59
  • possible duplicate of [Using Go on existing C project](http://stackoverflow.com/questions/30072281/using-go-on-existing-c-project) – Ainar-G Jun 04 '15 at 12:19
  • I agree that the other question deals with the same topic. – Sense Jun 04 '15 at 12:26

1 Answers1

0

If your aim is to build a library that can be linked into arbitrary C, Objective-C or Java programs, you are out of luck with the currently released standard tool chain. There are plans to change this in the future, but at present the Go runtime is not embeddable in other applications.

While cgo will allow you to export functions to be called from C, this is only really useful for cases when the C code you call from Go needs to call back to Go.

James Henstridge
  • 42,244
  • 6
  • 132
  • 114
  • 1
    "in the future" is in the next release, Go1.5 scheduled for August. The work is done so you could try using "tip" (probably by installing it from source from the head/tip of the repository; see some of the [upcoming build mode documentation](https://tip.golang.org/cmd/go/#hdr-Description_of_build_modes)). – Dave C Jun 02 '15 at 14:25
  • **I will try out Go 1.5.** For others looking: [The State of Go](http://talks.golang.org/2015/state-of-go-may.slide) from May is explaining the plans for the 1.5 release in August. – Sense Jun 02 '15 at 16:02