5

I'm trying to build a go project that uses a third party library (GDAL) that's written in C and C++. I've run into this error:

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 \ ^ 

Some searching tells me this is expected, and the fix is to simply set a flag on the compiler (not go related, but still pertinent).

I've read through go's documentation on building, and while it suggests it's possible to specify options to individual compilers, it doesn't explicitly spell out how, nor does it provide any examples.

I've tried the following just guessing, and while the go build command accepts them, they produce the same error, so they don't work.

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 

How can I tell go to tell gcc to compile with C++11 or newer?

Edit: As requested by 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. 
2
  • Have you tried using the environment variables listed in the documentation: CGO_CFLAGS, CGO_CPPFLAGS, CGO_CXXFLAGS, CGO_FFLAGS and CGO_LDFLAGS Commented Jun 25, 2018 at 15:18
  • No; I saw those in the page I linked as environment variables, but again didn't see any documentation on how to set them. The page you linked seems to have some documentation on how to set them, so I'll be trying that. Thanks. Commented Jun 25, 2018 at 15:25

1 Answer 1

3

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.


On Windows:

set CGO_CXXFLAGS=-std=c++11 
Sign up to request clarification or add additional context in comments.

4 Comments

That produces: go build # runtime/cgo gcc: error: "-std=c++11": Invalid argument
If I run that without the quotes and then run go build, it produces a different error that makes more sense: go build # runtime/cgo cc1.exe: error: command line option '-std=c++11' is valid for C++/ObjC++ but not for C [-Werror] cc1.exe: all warnings being treated as errors
I tried set CGO_CXXFLAGS=-std=c++11 and then go build and that works. After reading some more documentation, it seems that CPP is for the C preprocessor, and CXX is for C++. Thanks for getting me there. If you change your answer to CXX I'll accept it.
Works for me. I added #cgo CXXFLAGS: -std=c++11 in the Go file alongside the #include statement so I won't have to set that environment variable.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.