$ go version
go version go1.10.3 linux/amd64
$ go env
GOARCH="amd64"
GOBIN="/home/sintan1071/gopath/bin"
GOCACHE="/root/.cache/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/sintan1071/gopath"
GORACE=""
GOROOT="/usr/local/go1.10.3"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go1.10.3/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build260986162=/tmp/go-build -gno-record-gcc-switches"
Well, I'm trying to develop some cgo package which needs "libprocps.a", here is the code snippet:
package mypkg
/*
#cgo CFLAGS: -I./src
#cgo LDFLAGS: -lm -lff -lstdc++ -lgmpxx -lgmp -lprocps -L./lib -lmylib
#include "interface.hpp"
#include <stdlib.h>
*/
import "C"
// some function
func Some() {
...
}
that code works perfectly fine if you just use "go install" without any "ldflags", but because of the project need, we have to compile this code statically, so we have to use "ldflags" with "-linkmode external -extldflags -static", of course we already build our "lmylib" statically and get the "libmylib.a" in the "./lib" folder, so here are some logs
#$go install -ldflags "-linkmode external -extldflags -static" mypkg
/usr/local/go1.10.3/pkg/tool/linux_amd64/link: running gcc failed: exit status 1
/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/libprocps.a(pwcache.o): In function `pwcache_get_group':
(.text+0x1c8): warning: Using 'getgrgid' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/libprocps.a(pwcache.o): In function `pwcache_get_user':
(.text+0x58): warning: Using 'getpwuid' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/libprocps.a(readproc.o): In function `sd2proc':
(.text+0x1233): undefined reference to `sd_pid_get_machine_name'
(.text+0x1247): undefined reference to `sd_pid_get_owner_uid'
(.text+0x1296): undefined reference to `sd_pid_get_session'
(.text+0x12b1): undefined reference to `sd_session_get_seat'
(.text+0x12c7): undefined reference to `sd_pid_get_slice'
(.text+0x12dd): undefined reference to `sd_pid_get_unit'
(.text+0x12f3): undefined reference to `sd_pid_get_user_unit'
collect2: error: ld returned 1 exit status
any idea guys?
I hope there is a way to solve this problem, maybe not the "go" way, the "gcc " way or "ar -r" way or an alternative to “libprocps.a” is fine to me, I just want to know how can I compile successfully with “ldflags ‘-linkmode external -extldflags -static’ ", thank you all