Skip to content

doc: for -buildmode=c-shared, document that //export requires import "C" #11955

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
asmaloney opened this issue Jul 31, 2015 · 5 comments
Closed
Labels
Documentation Issues describing a change to documentation. FrozenDueToAge
Milestone

Comments

@asmaloney
Copy link
Contributor

This may just be a misunderstanding of how this is supposed to work - if so I'd appreciate some guidance.

What version of Go are you using (go version)?
go version go1.5beta3 darwin/amd64

What operating system and processor architecture are you using?
Mac OS X 10.10.4 Intel Core i7

What did you do?
I'm trying to build a C archive to link to a C++ app. I read the release notes and the Go Execution Modes doc. The second document states:

The only callable symbols will be those functions marked as exported (by any package), as described in the cgo documentation.

I wrote a small test:

package main

import "log"

//export Foo
func Foo() {
    log.Println("Foo")
}

func main() {
    // unused
}

go build -buildmode=c-archive

What did you expect to see?

I expected it to create foo.a and a header file with Foo exported.

What did you see instead?

It generated foo.a, but no header file.

nm also doesn't show my function being exported:

$ nm -g foo.a

foo.a(000000.o):
                 U ___stack_chk_fail
                 U ___stack_chk_guard
                 U ___stderrp
00000000000001f0 T __cgo_sys_thread_start
0000000000000360 T __cgo_wait_runtime_init_done
                 U _abort
00000000000004e1 T _crosscall_amd64
                 U _fprintf
                 U _fputc
                 U _free
                 U _fwrite
                 U _malloc
                 U _pthread_attr_destroy
                 U _pthread_attr_getstacksize
                 U _pthread_attr_init
                 U _pthread_cond_broadcast
                 U _pthread_cond_wait
                 U _pthread_create
                 U _pthread_key_create
                 U _pthread_key_delete
                 U _pthread_mutex_lock
                 U _pthread_mutex_unlock
                 U _pthread_setspecific
                 U _pthread_sigmask
                 U _setenv
                 U _strerror
                 U _unsetenv
0000000000000470 T _x_cgo_free
0000000000000000 T _x_cgo_init
0000000000000430 T _x_cgo_malloc
00000000000003c0 T _x_cgo_notify_runtime_init_done
0000000000000400 T _x_cgo_setenv
0000000000000300 T _x_cgo_sys_thread_create
0000000000000480 T _x_cgo_thread_start
0000000000000420 T _x_cgo_unsetenv

foo.a(go.o):
00000000000b49f0 T __cgo_panic
0000000000054fe0 T __cgo_topofstack
00000000000b4a20 T _crosscall2
                 U _x_cgo_free
                 U _x_cgo_init
                 U _x_cgo_malloc
                 U _x_cgo_notify_runtime_init_done
                 U _x_cgo_setenv
                 U _x_cgo_sys_thread_create
                 U _x_cgo_thread_start
                 U _x_cgo_unsetenv
@ianlancetaylor
Copy link
Contributor

You need to actually import "C" in the file with the //export comment. Otherwise cgo is not invoked and the //export comment is ignored.

@ianlancetaylor ianlancetaylor changed the title {1.5beta3} c-archive and header file doc: for -buildmode=c-shared, document that //export requires import "C" Jul 31, 2015
@ianlancetaylor ianlancetaylor added this to the Go1.5 milestone Jul 31, 2015
@ianlancetaylor ianlancetaylor added the Documentation Issues describing a change to documentation. label Jul 31, 2015
@ianlancetaylor ianlancetaylor self-assigned this Jul 31, 2015
@asmaloney
Copy link
Contributor Author

Thanks Ian! Misunderstanding on my part - I didn't realize cgo was involved at all in this - I thought your new stuff was just borrowing the export syntax - so I didn't dig into the cgo docs.

I see you added the documentation label - that's great.

Would it be feasible to recognize that no user code is being exported and produce a warning to point them in the right direction? If the developer is using -buildmode=c-archive they are likely expecting something of theirs to be exported.

@ianlancetaylor
Copy link
Contributor

I can't think of a simple way to check for no functions being exported, and I'm not sure it's worth coming up with a complex way. The fact that it doesn't work will be obvious.

@gopherbot
Copy link
Contributor

CL https://golang.org/cl/13080 mentions this issue.

@gopherbot
Copy link
Contributor

CL https://golang.org/cl/13096 mentions this issue.

ianlancetaylor added a commit that referenced this issue Aug 4, 2015
I accidentally submitted https://golang.org/cl/13080 too early.

Update #11955.

Change-Id: I1a5a6860bb46bc4bc6fd278f8a867d2dd9e411e1
Reviewed-on: https://go-review.googlesource.com/13096
Reviewed-by: Andrew Gerrand <[email protected]>
@golang golang locked and limited conversation to collaborators Aug 5, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Documentation Issues describing a change to documentation. FrozenDueToAge
Projects
None yet
Development

No branches or pull requests

3 participants