Skip to content
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

cmd/cgo: cgo fails to find static library when in Go package on linux/amd64 #6454

Closed
slimsag opened this issue Sep 23, 2013 · 3 comments

Comments

@slimsag
Copy link

commented Sep 23, 2013

What steps will reproduce the problem?
If possible, include a link to a program on play.golang.org.
1. Create simple static library in C.
    libwrapper.a
2. Create Go package 'wrapper':
    $GOPATH/src/wrapper
3. Tell CGO to statically link against lib:
    #cgo LDFLAGS: libwrapper.a
4. Create exported Go function to call the export C function in static library.
5. Create Go program 'test' that imports 'wrapper' package, and invokes Go function.
    $GOPATH/wrapper/test
6. go install wrapper
    Compiles OK.
7. go install wrapper/test
    Fails to compile; "libwrapper.a: No such file or directory"

Note: attached is zip of sources needed to reproduce


What is the expected output?
The program should compile / find libwrapper.a inside package directory. I suspect this
is a bug because the program compiles on windows/amd64 fine.

What do you see instead?

go install -x wrapper/test

WORK=/tmp/go-build592762736
mkdir -p $WORK/wrapper/test/_obj/
mkdir -p $WORK/wrapper/test/_obj/exe/
cd /home/stephen/Desktop/godev/src/wrapper/test
/home/stephen/go/pkg/tool/linux_amd64/6g -o $WORK/wrapper/test/_obj/_go_.6 -p
wrapper/test -complete -D _/home/stephen/Desktop/godev/src/wrapper/test -I $WORK -I
/home/stephen/Desktop/godev/pkg/linux_amd64 ./test.go
/home/stephen/go/pkg/tool/linux_amd64/pack grcP $WORK $WORK/wrapper/test.a
$WORK/wrapper/test/_obj/_go_.6
cd .
/home/stephen/go/pkg/tool/linux_amd64/6l -o $WORK/wrapper/test/_obj/exe/a.out -L $WORK
-L /home/stephen/Desktop/godev/pkg/linux_amd64 $WORK/wrapper/test.a
# wrapper/test
gcc: error: libwrapper.a: No such file or directory
/home/stephen/go/pkg/tool/linux_amd64/6l: running gcc failed: unsuccessful exit status
0x100

Which compiler are you using (5g, 6g, 8g, gccgo)?
6g


Which operating system are you using?
linux/amd64

Which version are you using?  (run 'go version')
tip
go version devel +bc30dedffb3d Sun Sep 22 22:31:08 2013 -0700 linux/amd64

Attachments:

  1. sources.zip (3093 bytes)
@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

commented Sep 23, 2013

Comment 1:

Labels changed: added go1.2maybe.

@rsc

This comment has been minimized.

Copy link
Contributor

commented Oct 7, 2013

Comment 3:

The difference here between windows/amd64 and linux/amd64 is that the latter is using
the new external linking mode, in which the final link and LDFLAGS happen once per final
binary. At that point the link is no longer in any particular package directory, and in
particular it is not in the package directory where libwrapper.a sits. You need to use
an absolute path to libwrapper.a or else install it somewhere standard and use
-lwrapper. Since there is only one link but many packages, we can't support this
behavior.
Note that you can put your code into a wrapper_linux_amd64.syso file and that will be
passed to the final linker command automatically.

Status changed to Unfortunate.

@slimsag

This comment has been minimized.

Copy link
Author

commented Oct 8, 2013

Comment 4:

Russ,
I previously avoided the syso approach because I need to statically link against several
libraries (in my case libfreetype, libpng, libz, and libbz2), if I rename them all to
.syso files, they aren't linked in the correct order it seems (because libfreetype
depends on libpng, etc).
I found I can use GCC's ar tool to extract the .o files and re-link all the .o files
into a single .syso file, like so:
ld -r *.o -o freetype_windows_amd64.syso
I have only tried this on windows/amd64 thus far, and I receive some errors during the
final link:
__mingw_vfprintf(0): not defined
modf(0): not defined
floor(0): not defined
pow(0): not defined
close(0): not defined
open(0): not defined
__ms_vsnprintf(0): not defined
___chkstk_ms(0): not defined
write(0): not defined
read(0): not defined
Perhaps I should link against the static .a files on windows/amd64, but on linux/amd64 I
should use .syso files? (I haven't tried yet to see if the above errors also occur on
Linux, but I would guess that they probably don't).
I apologize if this is the wrong place for this discussion; but I feel it is related
somewhat.

@rsc rsc added this to the Go1.2 milestone Apr 14, 2015

@rsc rsc removed the go1.2maybe label Apr 14, 2015

@golang golang locked and limited conversation to collaborators Jun 25, 2016

This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
4 participants
You can’t perform that action at this time.