如何配置 VS 代码以在发布模式下构建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 并发现 https://github.com/russelljjarvis/jit_hub/blob/neuronunit/jithub/models/backends/adexp.py#L79-L130 中有两个特殊的组VSCode 提供的 {1}}:构建和测试。所以,我想我们需要手动在发布模式下构建它,而我又对我没有任何信心......

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

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

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

tasks.json

它的作用是,基本上 CMake 会扫描您的项目文件夹并通过 { "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","args": [ "--build","${workspaceRoot}/build","--config","Release" ],"isDefault": true } } ] } 任务创建配置文件。之后,当您运行 cmake init 时,该任务将假定您有一个构建文件夹(您可以轻松更改该文件夹)并在 cmake release builder 中创建一个紧凑的可执行文件。

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

CMakeLists.txt

最后,要使这个过程真实,您需要在 VSCode 中执行 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})

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

我希望我以易于理解的方式使用英语而不是混淆。