Generate static library written in golang

2,717 views
Skip to first unread message

sari...@gmail.com

unread,
Jun 3, 2015, 8:34:38 AM6/3/15
to golan...@googlegroups.com
Hello,

go version go1.4 darwin/amd64

I'm trying to create a static library written in golang, then using that static library in a c/obj/xcode project. 

== gittu.go ==

package gittu

import "C"

//export getGittu
func getGittu() *C.char {
s := "Hello Gittu"
return C.CString(s)
}

==

After that i use "go install" to generate a gittu.a in $GOPATH/pkg/darwin_amd64/..............

But when i include that gittu.a in xcode, i get this error,

ignoring file /usr/local/Cellar/go/1.4/packages/pkg/darwin_amd64/github.com/sarim/gittu.a, file was built for archive which is not the architecture being linked (x86_64)

I manually tried to run cc, but still the same error. Any idea how to generate x86_64 static lib?

David Crawshaw

unread,
Jun 3, 2015, 10:08:07 AM6/3/15
to sari...@gmail.com, golang-nuts
You cannot do that with Go 1.4.

In Go 1.5 there will be -buildmode=c-archive which will generate an
archive you can link into a C program as you've described. Details are
in the Go execution modes doc:
https://docs.google.com/document/d/1nr-TQHw_er6GOQRsF6T43GGhFDelrAP0NqSS_00RgZQ/edit?pli=1

You may also be interested in
http://tip.golang.org/misc/cgo/testcarchive/ as an example.
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Ian Lance Taylor

unread,
Jun 3, 2015, 10:09:25 AM6/3/15
to sari...@gmail.com, golang-nuts
In Go 1.4, you can't.

In Go 1.5, you should be able to use go build -buildmode=c-archive.
It might be worth trying that now with the development version of Go,
to see if it works for you.

Ian

Carlos Castillo

unread,
Jun 3, 2015, 5:13:19 PM6/3/15
to golan...@googlegroups.com, sari...@gmail.com
Will the alternate build/link modes be supported for darwin in 1.5? AFAIK they haven't been implemented in OSX yet, and the OP is using OSX (according to his message).

Ian Lance Taylor

unread,
Jun 3, 2015, 5:43:00 PM6/3/15
to Carlos Castillo, golang-nuts, sari...@gmail.com
On Wed, Jun 3, 2015 at 2:13 PM, Carlos Castillo <cook...@gmail.com> wrote:
> Will the alternate build/link modes be supported for darwin in 1.5? AFAIK
> they haven't been implemented in OSX yet, and the OP is using OSX (according
> to his message).

The c-archive mode, which is the one I mentioned, is already supported
and working on Darwin.

The c-shared and shared modes do not yet work on Darwin. They are
more complicated than c-archive. I don't know if anybody is working
on them, but it would be great if they could.

Ian

Alexandre Fiori

unread,
Jun 4, 2015, 12:36:53 AM6/4/15
to golan...@googlegroups.com, cook...@gmail.com, sari...@gmail.com
This is cool stuff, I was playing with it a few days ago after watching Andrew's presentation.
Uploaded to GitHub so it may serve as an example for others: https://github.com/fiorix/gocp

Only tested on Linux c-shared and c-archive, and OS X c-archive. Tried FreeBSD but none are supported, and I really wanted that.

Pieter Droogendijk

unread,
Jun 4, 2015, 7:53:26 AM6/4/15
to golan...@googlegroups.com, sari...@gmail.com, cook...@gmail.com


On Thursday, June 4, 2015 at 6:36:53 AM UTC+2, Alexandre Fiori wrote:
This is cool stuff, I was playing with it a few days ago after watching Andrew's presentation.
Uploaded to GitHub so it may serve as an example for others: https://github.com/fiorix/gocp


 Select isn't that challenging, but you'll have to construct the select expression using reflect.

hui zhang

unread,
Apr 12, 2017, 2:08:10 AM4/12/17
to golang-nuts
I am using go1.8   arm/ darwin not work ,  for both bin and archive .how  to solve that

 GOOS=darwin GOARCH=arm GOARM=7  go build -buildmode=c-archive -o libmug.a
can't load package: package .: no buildable Go source files in ....

GOOS=darwin GOARCH=arm  go build -v -o hello.ios main.go
runtime/internal/sys
runtime/internal/atomic
runtime
math
command-line-arguments
# command-line-arguments
warning: unable to find runtime/cgo.a
/usr/local/Cellar/go/1.8.1/libexec/pkg/tool/darwin_amd64/link: running clang failed: exit status 1
ld: warning: ignoring file /var/folders/cp/561_gl9j1wzd8dgv_fn5mk7c0000gn/T/go-link-691505992/go.o, file was built for armv7 which is not the architecture being linked (x86_64): /var/folders/cp/561_gl9j1wzd8dgv_fn5mk7c0000gn/T/go-link-691505992/go.o
Undefined symbols for architecture x86_64:
  "_main", referenced from:
     implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)


在 2015年6月3日星期三 UTC+8下午8:34:38,Sarim Khan写道:
Reply all
Reply to author
Forward
0 new messages