c++ - 指定 go build 的 C++ 版本

标签 c++ go gcc go-build

我正在尝试构建一个使用用 C 和 C++ 编写的第三方库 (GDAL) 的 go 项目。我遇到了这个错误:

In file included from contour.cpp:31:0:
cpl_port.h:187:6: error: #error Must have C++11 or newer.
 #    error Must have C++11 or newer.
      ^
In file included from C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/type_traits:35:0,
                 from cpl_conv.h:372,
                 from contour.cpp:39:
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
 #error This file requires compiler and library support for the \
  ^

一些搜索告诉我这是预期的,解决方法是简单地在编译器上设置一个标志(not go related, but still pertinent)。

我已通读 go's documentation关于构建,虽然它建议可以为各个编译器指定选项,但它没有明确说明如何,也没有提供任何示例。

我试过以下只是猜测,虽然 go build 命令接受它们,但它们会产生相同的错误,因此它们不起作用。

go build -gcflags -std=gnu++11 -ldflags -std=gnu++11
go build -gcflags -std=c++11 -ldflags -std=c++11
go build -gcflags -std=c++11
go build -gcflags -std=all=gnu++11 -ldflags -std=all=gnu++11

我如何让 go 告诉 gcc 使用 C++11 或更新版本进行编译?

编辑:根据 PeterSO 的要求:

H:\>go version
go version go1.10.2 windows/amd64

H:\>go env
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\ksexton\AppData\Local\go-build
set GOEXE=.exe
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=H:\ksexton\Go;
set GORACE=
set GOROOT=C:\Go
set GOTMPDIR=
set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64
set GCCGO=gccgo
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
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\ksexton\AppData\Local\Temp\go-build937852322=/tmp/go-build -gno-record-gcc-switches

H:\>gcc --version
gcc (tdm64-1) 5.1.0
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

H:\>g++ --version
g++ (tdm64-1) 5.1.0
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

最佳答案

Command cgo

Using cgo with the go command

All the cgo CPPFLAGS and CFLAGS directives in a package are concatenated and used to compile C files in that package. All the CPPFLAGS and CXXFLAGS directives in a package are concatenated and used to compile C++ files in that package. All the CPPFLAGS and FFLAGS directives in a package are concatenated and used to compile Fortran files in that package. All the LDFLAGS directives in any package in the program are concatenated and used at link time. All the pkg-config directives are concatenated and sent to pkg-config simultaneously to add to each appropriate set of command-line flags.


在 Windows 上:

set CGO_CXXFLAGS=-std=c++11

关于c++ - 指定 go build 的 C++ 版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51026558/

相关文章:

c++ - 需要管理一 block 'theoretical'内存的slab

json - 如何为扫描 json 对象的 bufio.Scaner 编写自定义 splitFunc

c - 如何在代码块中向编译器添加标志?

c - 升级到 Windows 10 后不再具有通过 gcc 编译的权限

C++:对象已删除(用 new 创建)但成员函数仍在工作(?)

c++ - 从 C++ 调用 Delphi DLL IStream 参数的问题

go - 打印项目之间没有空格

c - GCC 优化可能产生的副作用是什么?

c++ - 这是对C++ 20概念的正确使用吗?

go - 上下文包 vs 完成 channel 以避免 goroutine 泄漏