如何在 vscode 启动代码之前设置 cgo 环境变量?

问题描述 投票:0回答:1

我有 golang 代码,例如:


import (
    "github.com/bobertlo/go-mpg123/mpg123"
)

func main() {
    ...
}

从终端构建此代码。我必须设置以下环境变量:

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

现在我想使用 vscode 进行构建。我在下面配置launch.json、tasks.json:

// 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",
        },
    ]
}

但是没有成功。据报道:

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)

我怎样才能让它发挥作用?

go visual-studio-code clang cgo
1个回答
0
投票

${env:ENV_VAR}
变量引用可以是您正在寻找的吗?这是特定于工作空间的。

但是,还有 go 扩展的特定设置

© www.soinside.com 2019 - 2024. All rights reserved.