指定go build的C ++版本

时间:2018-06-25 15:03:37

标签: c++ go gcc go-build

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

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

如何告诉gocc使用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.

1 个答案:

答案 0 :(得分:2)

  

Command cgo

     

Using cgo with the go command

     

软件包中的所有cgo CPPFLAGS和CFLAGS指令均为   连接并用于编译该程序包中的C文件。所有   程序包中的CPPFLAGS和CXXFLAGS指令是串联的,   用于在该程序包中编译C ++文件。所有CPPFLAGS和FFLAGS   程序包中的指令被连接起来并用于编译Fortran   该软件包中的文件。所有软件包中的所有LDFLAGS指令   该程序在链接时被串联并使用。所有的pkg-config   指令被串联并同时发送到pkg-config   添加到每个适当的命令行标志集。


在Windows上:

set CGO_CXXFLAGS=-std=c++11