Ld Warning Object File Was Built for Newer MacOS Version Than Being Linked

go run和go build会报出一连串下面这样的警告信息,虽然不影响程序运行,但是看着难看。

# command-line-arguments
ld: warning: object file (/var/folders/9p/2x3ls9kn7qb59hf9l9mtfs8s6rbmfg/T/go-link-3994413096/000000.o) was built for newer macOS version (12.0) than being linked (11.0)

解决方法:CGO_CFLAGS=-mmacosx-version-min=10.12 go run main.go,运行前加CGO_CFLAGS=-mmacosx-version-min=10.12参数可以解决。

vscode中可以通过配置launch.json文件实现:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Launch Package",
      "type": "go",
      "request": "launch",
      "env": {
        "CGO_CFLAGS":"-mmacosx-version-min=10.12"
      },
      "program": "${fileDirname}",
      "args": [],
      "cwd": "${fileDirname}"
    }
  ]
}

或者从文件中读取环境变量:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Launch Package",
      "type": "go",
      "request": "launch",
      "envFile": "${workspaceFolder}/.env",
      "program": "${fileDirname}",
      "args": [],
      "cwd": "${fileDirname}"
    }
  ]
}

如果是在vscode中执行测试代码,则需要设置settings.json文件:

{
  "go.testEnvVars": {
    "CGO_CFLAGS":"-mmacosx-version-min=10.12"
  }
}