go 环境搭建 以及 linux 下go环境交叉编辑链(arm)的搭建,error: invalid conversion from 'void*' to 'unsigned char*' [-fpermissive]
1,下载
Golang官网下载地址:https://golang.google.cn/dl/
2,解压到指定目录
3,运行go
获取环境变量修改方法
go help env usage: go env [-json] [-u] [-w] [var ...] Env prints Go environment information. By default env prints information as a shell script (on Windows, a batch file). If one or more variable names is given as arguments, env prints the value of each named variable on its own line. The -json flag prints the environment in JSON format instead of as a shell script. The -u flag requires one or more arguments and unsets the default setting for the named environment variables, if one has been set with 'go env -w'. The -w flag requires one or more arguments of the form NAME=VALUE and changes the default settings of the named environment variables to the given values. For more about environment variables, see 'go help environment'.
windows现在环境变量
C:\Users\Administrator>go env set GO111MODULE=on set GOARCH=amd64 set GOBIN= set GOCACHE=C:\Users\Administrator\AppData\Local\go-build set GOENV=C:\Users\Administrator\AppData\Roaming\go\env set GOEXE=.exe set GOFLAGS= set GOHOSTARCH=amd64 set GOHOSTOS=windows set GOINSECURE= set GONOPROXY= set GONOSUMDB= set GOOS=windows set GOPATH=C:\Go\networkpkg; set GOPRIVATE= set GOPROXY=https://goproxy.io,direct set GOROOT=c:\go set GOSUMDB=sum.golang.org set GOTMPDIR= set GOTOOLDIR=c:\go\pkg\tool\windows_amd64 set GCCGO=gccgo set AR=ar set CC=gcc set CXX=g++ set CGO_ENABLED=1 set GOMOD=NUL set CGO_CFLAGS=-g -O2 set CGO_CPPFLAGS= set CGO_CXXFLAGS=-g -O2 set CGO_FFLAGS=-g -O2 set CGO_LDFLAGS=-g -O2 set PKG_CONFIG=pkg-config set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\ADMINI~1\AppData\Local\Temp\go-build644129466=/tmp/go-build -gno-record-gcc-switches C:\Users\Administrator>
linux 现在环境变量
root@ubuntu:~# go env GO111MODULE="on" GOARCH="amd64" GOBIN="" GOCACHE="/root/.cache/go-build" GOENV="/root/.config/go/env" GOEXE="" GOFLAGS="" GOHOSTARCH="amd64" GOHOSTOS="linux" GOINSECURE="" GONOPROXY="" GONOSUMDB="" GOOS="linux" GOPATH="/root/go" GOPRIVATE="" GOPROXY="https://goproxy.cn/" GOROOT="/home/goenv/go" GOSUMDB="sum.golang.org" GOTMPDIR="" GOTOOLDIR="/home/goenv/go/pkg/tool/linux_amd64" GCCGO="gccgo" AR="ar" CC="gcc" CXX="g++" CGO_ENABLED="1" GOMOD="/dev/null" 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-build869956040=/tmp/go-build -gno-record-gcc-switches" root@ubuntu:~#
设置/etc/profile环境变量
设置GOPATH环境变量 修改/etc/profile系统环境变量文件,写入GOPATH信息以及go sdk路径 #for go env export GOROOT=/home/goenv/go export PATH=$PATH:$GOROOT/bin 读取/etc/profile,立即生效 source /etc/profile #读取环境变量
修改方法
go env -w GOPROXY=https://goproxy.cn/
go env -w GO111MODULE=on
想在linux 同时设置 x64,x86的环境失败了.(当年是失败了,现在被b成功了.请看最下面说明 )
GOCACHE="/root/.cache/go-build" GOENV="/root/.config/go/env"
GOROOT="/home/goenv/go"
会同时修改.只有用2台机器来配置环境变量
安装mingw ,gcc
安装mingw,想用g++\gcc编译器。 网上很多教程都是指引下载安装器的,但是下载后在线安装时却都出现下载的错误,所以无法安装成功。 参考mingw贴吧:https://tieba.baidu.com/p/5487544851?red_tag=1967781305 可以到页面:https://sourceforge.net/projects/mingw-w64/files/?source=navbar 下载离线安装包
go 交叉编译 arm 32 的程序
由于项目原因,需要把go的程序编译成so,供C使用.无奈板子空间小,运行慢.
被bi de 需要用交叉编译链,以前都没有弄对,这次整对了.
#拷贝下面的话到linux中执行. #指定gcc程序目录 export PATH=$PATH:/opt/poky/1.7/sysroots/i686-pokysdk-linux/usr/bin/arm-poky-linux-gnueabi/ export PATH=$PATH:$GOROOT/bin export GOPROXY="https://goproxy.io,direct" export CGO_ENABLED=1 export GO111MODULE=on export GOROOT=/home/goenv/go # 这些是修改 编译架构,x86,x64 , gcc ,gcc FLAGS export GOARCH=arm export GOHOSTARCH=386 export CGO_CFLAGS="-g -O2 " export CGO_CPPFLAGS=" " export CGO_CXXFLAGS="-g -O2 " export CGO_FFLAGS="-g -O2 " export CGO_LDFLAGS="-g -O2 " export PKG_CONFIG="pkg-config" export GOGCCFLAGS=" -fPIC -marm -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build964815022=/tmp/go-build -gno-record-gcc-switches" export AR="arm-poky-linux-gnueabi-gcc-ar" export CC="arm-poky-linux-gnueabi-gcc" export CXX="arm-poky-linux-gnueabi-g++"
#拷贝下面的话到linux中执行. #指定gcc程序目录 export PATH=$PATH:/opt/poky/1.7/sysroots/i686-pokysdk-linux/usr/bin/arm-poky-linux-gnueabi/ export PATH=$PATH:$GOROOT/bin export GOPROXY="https://goproxy.io,direct" export CGO_ENABLED=1 export GO111MODULE=on export GOROOT=/home/goenv/go # 这些是修改 编译架构,x86,x64 , gcc ,gcc FLAGS export GOARCH=arm export GOHOSTARCH=386 export CGO_CFLAGS="-g -O2 -mfloat-abi=hard" export CGO_CPPFLAGS="-mfloat-abi=hard" export CGO_CXXFLAGS="-g -O2 -mfloat-abi=hard" export CGO_FFLAGS="-g -O2 -mfloat-abi=hard" export CGO_LDFLAGS="-g -O2 -mfloat-abi=hard" export PKG_CONFIG="pkg-config" export GOGCCFLAGS="-mfloat-abi=hard -fPIC -marm -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build964815022=/tmp/go-build -gno-record-gcc-switches" export AR="arm-poky-linux-gnueabi-gcc-ar" export CC="arm-poky-linux-gnueabi-gcc" export CXX="arm-poky-linux-gnueabi-g++"
export GOARCH=arm
使用go build编译的程序即可在arm平台运行
切换回linux平台
export GOARCH=amd64
然后执行自己需要的go编译命令就可以了
go build -o /xxx.exe /run.go #生成exe
go build -ldflags xxxxxxxxx #生成so
go 交叉编译 arm 64 的程序
export PATH=$PATH:/home/armgcc/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/ export GOROOT=/home/go/ export PATH=$PATH:$GOROOT/bin export GOPROXY="https://goproxy.io,direct" export CGO_ENABLED=1 export GO111MODULE=on # 这些是修改 编译架构,x86,x64 , gcc ,gcc FLAGS amd64 arm64 export GOOS=linux export GOARCH=arm64 export GOHOSTARCH=arm64 export GOFLAGS=" " export CGO_CFLAGS="-g -O2 " export CGO_CPPFLAGS=" " export CGO_CXXFLAGS="-g -O2 " export CGO_FFLAGS="-g -O2 " export CGO_LDFLAGS="-g -O2 " export PKG_CONFIG="pkg-config" GOGCCFLAGS="-fPIC -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build504536815=/tmp/go-build -gno-record-gcc-switches" export AR="aarch64-linux-gnu-gcc-ar" export CC="aarch64-linux-gnu-gcc" export CXX="aarch64-linux-gnu-g++"
当时
export CC="aarch64-linux-gnu-gcc" 写成了
export CC="aarch64-linux-gnu-g++"
导致编译出错
error: invalid conversion from 'void*' to 'unsigned char*' [-fpermissive]
其实是应该用gcc编译的用的g++ ,,,wo cao,cao
我正在尝试用g ++编译一些c代码(是的,故意的)。我收到的错误就像(例如): error: invalid conversion from 'void*' to 'unsigned char*' [-fpermissive] 在编译选项中添加-fpermissive让我: error: invalid conversion from 'void*' to 'unsigned char*' [-Werror=permissive] 由于-Werror似乎是错误,但是添加-Wno-error=permissive -Wno-permissive导致: error: -Werror=permissive: no option -Wpermissive error: unrecognized command line option "-Wno-permissive" [-Werror] 如何禁用从void *到其他指针类型的转换警告(全局)?