I have golang code like:
import (
"github.com/bobertlo/go-mpg123/mpg123"
)
func main() {
...
}
From terminal, to build this code. I must set below env variable:
export C_INCLUDE_PATH=$C_INCLUDE_PATH:/opt/homebrew/Cellar/mpg123/1.32.3/include
export LIBRARY_PATH=$LIBRARY_PATH:/opt/homebrew/Cellar/mpg123/1.32.3/lib
Now I want build using vscode. I configure the launch.json, tasks.json below:
// launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch Package",
"type": "go",
"request": "launch",
"mode": "auto",
"preLaunchTask": "Set CGO_CFLAGS",
"program": "${fileDirname}"
}
]
}
// tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "Set C_INCLUDE_PATH",
"type": "shell",
"command": "export C_INCLUDE_PATH=$C_INCLUDE_PATH:/opt/homebrew/Cellar/mpg123/1.32.3/include",
"problemMatcher": []
},
{
"label": "Set LIBRARY_PATH",
"type": "shell",
"command": "export LIBRARY_PATH=$LIBRARY_PATH:/opt/homebrew/Cellar/mpg123/1.32.3/lib",
},
{
"label": "Set CGO_CFLAGS",
"type": "shell",
"dependsOn":["Set C_INCLUDE_PATH", "Set LIBRARY_PATH"],
"dependsOrder": "sequence",
},
]
}
But it didn’t work. It reports:
Build Error: go build -o /Users/xxx/Projects/portauio-go/examples/__debug_bin2329559837 -gcflags all=-N -l .
# github.com/bobertlo/go-mpg123/mpg123
../../../../go/pkg/mod/github.com/bobertlo/[email protected]/mpg123/mpg123.go:8:10: fatal error: 'mpg123.h' file not found
#include <mpg123.h>
^~~~~~~~~~
1 error generated. (exit status 1)
How can I make it work?
2
Answers
I suppose it's because the task type is set go "shell". That means it will execute in another shell, and the env it set won't take effect in current shell. I set env in launch.json, and now it works
Can
${env:ENV_VAR}
variable references be what you are looking for? This is workspace specific.However there are specific settings for go extention as well.