I am trying to build a golang program which uses a static lib (.a file)
the directory struct for my project as below
└─testserver
├─bin
├─pkg
└─src
├─logging
└─testserver
├─libtest.a
└─test.go
the flags for cgo in test.go as below
// #cgo LDFLAGS: -L /home/test/testserver/src/testserver -ltest
// #include "test.h"
import "C"
when I am using absolute path for LDFLAGS -L, it works fines, but when I change the path to a relative path, eg
// #cgo LDFLAGS: -L ./testserver -ltest
and then run the command
go install testserver
it returns an error to me, and says "cannot find -ltest"
my question is how can I use a relative path in LDFLAGS ? , so that I can build the project in any path.
You currently can't. The directory changes between the time the command is built, and linking. For now you either need to link to an absolute path, or use the CGO_LDFLAGS
environment variable.
There was a commit just after go1.4 which added a ${SRCDIR}
variable which is replaced by the absolute path to the directory containing the source file at build time. https://github.com/golang/go/issues/7891. This will be in go1.5, and you can easily use it now by building Go from source.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With