Golang中如何安装gccgo以测试Protocol Buffers 3
Golang中如何安装gccgo以测试Protocol Buffers 3 大家好,
在阔别几年后,我重新开始接触编程和技术……我正在尝试学习Golang和Protocol Buffers的入门知识。经过一些阅读后,我发现我需要安装gccgo。
这个仓库:GitHub - golang/protobuf: Go support for Google’s protocol buffers 看起来是完成此任务最相关且信息最全面的来源。以下是我遵循的步骤描述。提前感谢您的帮助 🙂
1st bullet point: checked!
我的Mac上安装了最新版本的protocol buffer(根据我的理解是 protobuf-cpp-3.11.4.tar.gz)
$ ls $GOBIN
dlv* gocode* godef* gopkgs* protoc-gen-go*
go-outline* gocode-gomod* golint* goreturns*
2nd bullet point: Here I’ve spent a couple of hours … without success 😕
当然,从xxxxx安装Go编译器和工具。详情请参见xxxxx/doc/install,或者,如果您使用
gccgo,请遵循xxxxx/doc/install/gccgo的说明。
我的理解是我需要安装gccgo,它是gcc编译器的一个分支。然后我读到gccgo实际上只是一个配置了--enable-languages=c,c++,go选项的gcc编译器的自定义构建版本……那么为什么仓库里会有一个特殊的分支,它在哪里呢?
我放弃了尝试从git仓库下载gccgo分支,并找到了一个svn仓库:
$ svn checkout svn://gcc.gnu.org/svn/gcc/branches/gccgo gccgo`
gccgo$ ./configure --enable-languages=c,c++,go
...
configure: error: Building GCC requires GMP 4.2+, MPFR 3.1.0+ and MPC 0.8.0+.
Try the --with-gmp, --with-mpfr and/or --with-mpc options to specify
their locations. Source code for these libraries can be found at
their respective hosting sites as well as at
gcc.gnu.org/pub/gcc/infrastructure/. See also
gcc.gnu.org/install/prerequisites.html for additional info. If
you obtained GMP, MPFR and/or MPC from a vendor distribution package,
make sure that you have installed both the libraries and the header
files. They may be located in separate packages.
所以,我下载了gmp-6.2.0.tar.lz,这导致我需要在解压归档文件之前先安装lzip。
$ brew install lzip
$ lunzip gmp-6.2.0.tar.lz
$ tar - xvzf gmp-6.2.0.tar
$ cd gmp-6.2.0
gmp-6.2.0$ ./configure
gmp-6.2.0$ make
gmp-6.2.0$ make install
gmp-6.2.0$ make check ( a few warnings but every test have been passed successfully )
然后,安装了mpfr-3.1.6.tar.gz
$ tar -xvzf mpfr-3.1.6.tar.gz
$ cd mpfr-3.1.6
mpfr-3.1.6$ ./configure
mpfr-3.1.6$ ./make
mpfr-3.1.6$ ./make install
……然后再试一次
gccgo$ ./configure --enable-languages=c,c++,go
...
The following requested languages could not be built: go
Supported languages are: c,brig,c,c++,d,fortran,lto,objc,obj-c++
Lastly
我不太确定他们在最后一步中提到的目录是哪个……
在此目录中使用“make go”构建Go示例。这将在当前目录中创建以下可执行文件: add_person_go list_people_go
make与gcc配合工作,它引入一个单独的“规则”文件,描述如何从源代码到最终程序,解释这个文件,找出需要编译的内容,并调用gcc。所以,如果gcc没有正确编译,它就无法工作。
protocolbuffer$ ls
add_person.go add_person_test.go addressbook.proto list_people_test.go
add_person.go.txt addressbook.pb.go list_people.go
protocolbuffer$ make go
make: *** No rule to make target `go'. Stop.
如果需要,额外的技术信息:
~$ echo $GOPATH
/Users/me/Dev/Go/golib:/Users/me/Dev/Go/code
$GOBIN is /Users/me/Dev/Go/golib/bin
$ echo $GOBIN
/Users/me/Dev/Go/golib/bin
更多关于Golang中如何安装gccgo以测试Protocol Buffers 3的实战教程也可以访问 https://www.itying.com/category-94-b0.html
更多关于Golang中如何安装gccgo以测试Protocol Buffers 3的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html
在Golang中测试Protocol Buffers 3通常不需要安装gccgo。标准的Go编译器(gc)配合protoc-gen-go插件就能正常工作。以下是正确的安装和使用方法:
1. 安装Protocol Buffers编译器
# 使用Homebrew安装protobuf
brew install protobuf
# 验证安装
protoc --version
2. 安装Go的protobuf插件
# 安装protoc-gen-go
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
# 安装protoc-gen-go-grpc(如果需要gRPC)
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
3. 配置环境变量
# 确保GOBIN在PATH中
export PATH=$PATH:$(go env GOPATH)/bin
4. 使用示例
创建addressbook.proto文件:
syntax = "proto3";
package tutorial;
option go_package = "example.com/project/protos";
message Person {
string name = 1;
int32 id = 2;
string email = 3;
}
message AddressBook {
repeated Person people = 1;
}
生成Go代码:
protoc --go_out=. --go_opt=paths=source_relative addressbook.proto
这会生成addressbook.pb.go文件。
5. 编译和运行示例
创建main.go:
package main
import (
"fmt"
"log"
"example.com/project/protos"
"google.golang.org/protobuf/proto"
)
func main() {
p := &protos.Person{
Name: "John Doe",
Id: 1234,
Email: "john@example.com",
}
// 序列化
data, err := proto.Marshal(p)
if err != nil {
log.Fatal("marshaling error: ", err)
}
// 反序列化
newP := &protos.Person{}
err = proto.Unmarshal(data, newP)
if err != nil {
log.Fatal("unmarshaling error: ", err)
}
fmt.Printf("Name: %s\n", newP.Name)
fmt.Printf("ID: %d\n", newP.Id)
fmt.Printf("Email: %s\n", newP.Email)
}
编译和运行:
go mod init example.com/project
go mod tidy
go run main.go addressbook.pb.go
6. 如果确实需要gccgo
如果你有特殊需求必须使用gccgo,可以通过Homebrew安装:
# 安装gcc(包含gccgo)
brew install gcc
# 验证gccgo安装
gccgo --version
# 设置使用gccgo编译
export CC=gccgo
export GOROOT_BOOTSTRAP=$(go env GOROOT)
# 或者使用go命令的-gccgo标志
go build -compiler=gccgo main.go
但请注意,对于Protocol Buffers的常规使用,标准Go编译器完全足够,且性能通常更好。gccgo主要用于需要与C/C++深度集成或特定平台兼容性的场景。

