在Golang项目中正确包含C库(按源)

Currently, I have the following source tree:

client
|
|--cryptlib
|  |
|  |--cryptlib.so
|  |--cryptlib.a
|  |--<C sources>
|  |--Makefile
|
|--impl1
|  |--<C sources>
|  |--impl1.go
|  |--impl1_test.go
|
|--impl2
|  |--<C sources>
|  |--impl2.go
|  |--impl1_test.go
|
|--client.go
|--client_test.go

The cryptlib library is used by both impl1 and impl2 and thus, both impl1.go and impl2.go start with the following cgo block:

/*
#cgo CFLAGS: -I. -I${SRCDIR}/../cryptlib -L${SRCDIR}/../cryptlib -lcryptlib -Ofast 
#cgo LDFLAGS: -L${SRCDIR}/../cryptlib -lcryptlib
*/
import "C"

And this works while trying to test impl1_test.go and impl2_test.go.

However, when trying to run the tests situated in client_test.go, I keep getting errors claiming that the cryptlib library could not be found. I think this may have to do with the fact that ${SRCDIR} in this case is ./client which makes the rest of the CFLAGS and LDFLAGS paths incorrect (./client/../cryptolib).

Although it leads to repetition, I have also tried including cryptlib into impl1 and impl2. This approach again causes issues when trying to test client_test.go since the combined binary now sees multiple instances of the same functions defined in cryptlib.

How should I properly structure my source tree and cgo flags such that all my .go source files can find my cryptlib library and compile?

From the cgo Godocs:

When the cgo directives are parsed, any occurrence of the string ${SRCDIR} will be replaced by the absolute path to the directory containing the source file.

So for client_test the SRCDIR is a directory level higher and therefor its CGO directives should look like this:

/*
#cgo CFLAGS: -I. -I${SRCDIR}/cryptlib -L${SRCDIR}/cryptlib -lcryptlib -Ofast 
#cgo LDFLAGS: -L${SRCDIR}/cryptlib -lcryptlib
*/

As far as another approach, what I normally do is install the C library in the system library directory /usr/lib/. That way i do not need to worry about relative references and can just use -lcryptlib.