Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/cgo: cannot use environment variable in #cgo directive #34303

Closed
xiaofo09 opened this issue Sep 14, 2019 · 2 comments
Closed

cmd/cgo: cannot use environment variable in #cgo directive #34303

xiaofo09 opened this issue Sep 14, 2019 · 2 comments

Comments

@xiaofo09
Copy link

xiaofo09 commented Sep 14, 2019

What version of Go are you using (go version)?

$ go version
go version go1.13 linux/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN="/home/xiaofo/Dropbox/instapay/bin"
GOCACHE="/home/xiaofo/.cache/go-build"
GOENV="/home/xiaofo/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/xiaofo/Dropbox/instapay"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
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-build518041470=/tmp/go-build -gno-record-gcc-switches"

What did you do?

I created an environment variable on my linux

xiaofo@xiaofo-desktop:~/Dropbox/instapay/se$ export SGXSDK=/home/xiaofo/sgxsdk
xiaofo@xiaofo-desktop:~/Dropbox/instapay/se$ echo $SGXSDK
/home/xiaofo/sgxsdk

And I wrote following code and executed it.

xiaofo@xiaofo-desktop:~/Dropbox/instapay/se$ cat main.go
package main

/*
#cgo CPPFLAGS: -I$(SGXSDK)/include
#include "sgx_urts.h"
*/
import "C"

func main() {
}
xiaofo@xiaofo-desktop:~/Dropbox/instapay/se$ go run main.go
package main: /home/xiaofo/Dropbox/instapay/se/main.go: malformed #cgo argument: -I$(SGXSDK)/include

I tried ${SGXSDK}, $(SGXSDK), $SGXSDK, "${SGXSDK}", "$(SGXSDK)" and so on. Everything does not work. But

#cgo CPPFLAGS: -I/home/xiaofo/sgxsdk/include

works

What did you expect to see?

I expected that it runs without any error.

What did you see instead?

package main: /home/xiaofo/Dropbox/instapay/se/main.go: malformed #cgo argument: $(SGXSDK)/include

@xiaofo09
Copy link
Author

or should I use directives in goenv only ?

@odeke-em odeke-em changed the title cgo outputs malformed #cgo argument cmd/cgo: cannot use environment variable in #cgo directive Sep 15, 2019
@odeke-em
Copy link
Member

Thank you for asking this question @xiaofo09!

We usually we use golang-nuts for searching for/asking questions like these and the issue tracker for bugs that might be a problem in Go itself.
Please see https://github.com/golang/go/wiki/Questions for resources.

A similar question and answer is at https://groups.google.com/forum/#!msg/golang-nuts/yteBY_UXsac/1XPNTV7Xe34J;context-place=msg/golang-nuts/Wh_XWeOTGak/XCJKM0U7-lMJ where Minux showed how to use the CGO_FLAGS and CGO_LDFLAGS to pass in variables before go build/install.

cc @ianlancetaylor.

@golang golang locked and limited conversation to collaborators Sep 14, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants