I was trying to put together an example of statically linking a cgo binary, and I noticed that I can't use -static in CGO_LDFLAGS. `go build -ldflags -extldflags=-static` works as expected, but if I need to provide other linking information I still need to put that in CGO_LDFLAGS. I was trying to get all the options into the source file so `go build` would "Just Work".If LDFLAGS contains -static, I get:cannot load imported symbols from ELF file $WORK/_/home/jbardin/try/_obj/_cgo_.o: no symbol section
Looking over the -x output, when -static is in LDFLAGS the compilation step for _cgo_.o contains -static, whereas it does not when only using -extldflags
Is this a bug, or just something that can't be handled automatically?
Looking over the -x output, when -static is in LDFLAGS the compilation step for _cgo_.o contains -static, whereas it does not when only using -extldflagsright. CGO_LDFLAGS is meant for both internal linking and external linking mode, whereas -extldflags is meant onlyfor external linking.
Is this a bug, or just something that can't be handled automatically?I don't think it's a bug. Static linking is still an area that needs work.For now, you can embed the static library as a .syso file (use ar x to extract its content, and use ld -r to link them into a singlefile, then rename it to *.syso, put it into the package directory); or you could put the C/C++ source files in the package directoryand let the Go tool build the library for you.I prefer the 2nd solution as it should always work and don't involve checking in binary blobs.
go build -ldflags="-extldflags '-opt1 -opt2'"
Is there a way to configure pkg-config to do this by default? It's cumbersome to have to pass those args on every cli command.
libdir=/usr/lib/x86_64-linux-gnuincludedir=/usr/include/fooName: libfooDescription: Library for fooing stuff.Version: 0.0.1Libs: -L${libdir} -lfoo -opt1 -opt2Cflags: -I${includedir}
And build it:// #cgo pkg-config: libfoo
PKG_CONFIG_PATH="$GOPATH/src/github.com/user/foobind:$PKG_CONFIG_PATH" go build github.com/user/foobind