如何配置 VS 代码以在发布模式下构建 (clang + macOS)

how do I configure VS code to build in Release mode (clang + macOS)

我是 VS 代码的新手。我使用提供的 [VS 代码文档] (https://code.visualstudio.com/docs/cpp/config-clang-mac)

在 macOS 上使用 Clang 配置它

它有效,我可以构建和调试。太棒了!

我的问题是:如何配置 VS Code 以发布模式构建?我似乎无法在文档或教程中找到有关如何操作的任何信息

如果有帮助,这就是 tasks.json 现在的样子

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
      {
        "type": "shell",
        "label": "clang++ build active file",
        "command": "/usr/bin/clang++",
        "args": [
          "-std=c++17",
          "-stdlib=libc++",
          "-g",
          "-v",
          "${file}",
          "${workspaceFolder}/source/*.cpp",
          "${workspaceFolder}/FFTreal/*.cpp",

           "-I.",       

          "-L",
          "${workspaceFolder}/BassLibrary",
          "-lbass",

          "-o",
          "${workspaceFolder}/final.out",

        ],
        "options": {
          "cwd": "${workspaceFolder}"
        },
        "problemMatcher": ["$gcc"],
        "group": {
          "kind": "build",
          "isDefault": true
        }
      }
    ]
  }

有什么想法吗?谢谢!

抱歉,最后一个答案...

所以首先,我需要声明我对 Clang Family 不是很熟悉,但我做了一些 research 发现有两个VSCode 提供的 tasks.json 中的特殊组:构建和测试。所以,我想我们需要在发布模式下手动构建它,我再次对我没有任何信心...

我发现您可以使用 CMake 构建您的 Clang 脚本。 CMake 只是一个构建器工具,可帮助您重新组装整个代码,包括库、依赖项、模块等。

所以,我不确定这是否可行,但您可以将 tasks.json 配置从 Clang++ 更改为 CMake,也许吧?

首先,为了快速回答,我将您的 tasks.json 片段编辑为:

{
  "tasks": [
    {
      "label": "cmake init",
      "type": "shell",
      "options": {
        "cwd": "${workspaceRoot}"
      },
      "command": "/path/to/cmake",
      "args": [
        "-S",
        "${cwd}",
        "-B",
        "${cwd}/build"
      ],
      "problemMatcher": [
        "$gcc"
      ],
      "group": {
        "kind": "build",
        "isDefault": true
      }
    },
    {
      "label": "cmake release builder",
      "type": "shell",
      "options": {
        "cwd": "${workspaceRoot}"
      },
      "command": "/path/to/cmake",
      "args": [
        "--build",
        "${workspaceRoot}/build",
        "--config",
        "Release"
      ],
      "problemMatcher": [
        "$gcc"
      ],
      "group": {
        "kind": "build",
        "isDefault": true
      }
    }
  ]
}

它的作用是,基本上 CMake 会扫描您的项目文件夹并通过 cmake init 任务创建配置文件。之后,当您 运行 cmake release builder 时,任务将假定您有一个构建文件夹 - 您可以轻松更改 - 并在 build/Release.[=21 中创建一个紧凑的可执行文件=]

而且,为了使用 CMake,您需要创建一个 CMakeLists.txt 文件,其 baseName 区分大小写。所以,我创建了一个非常简单的 CMake 配置文件,您可以手动添加源文件,不幸的是,像这样:

cmake_minimum_required(VERSION 3.13)  # CMake version check
project(simple_example)               # Create project "simple_example"
set(CMAKE_CXX_STANDARD 14)            # Enable c++14 standard
set(SOURCE_FILES test.cpp add.cpp)    # Append source files

# Add executable target with source files listed in SOURCE_FILES variable
add_executable(simple_example ${SOURCE_FILES})

最后,要使这个过程真实,您需要在VSCode.Terminal > Run Build Task

我不太确定您是否可以使用 Clang 编译器构建整个应用程序,例如 Windows 中的 .exe 文件。在研究过程中,我非常关注编译器和构建器之间的区别,我建议查看一下!

我希望我用的英语是可以理解的,而不是被混淆了。