【问题标题】:how do I configure VS code to build in Release mode (clang + macOS)如何配置 VS 代码以在发布模式下构建(clang + macOS)
【发布时间】:2021-07-07 16:22:01
【问题描述】:

我是 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
        }
      }
    ]
  }

有什么想法吗?谢谢!

【问题讨论】:

    标签: c++ visual-studio-code build clang release-mode


    【解决方案1】:

    对不起,最后一个答案...

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

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

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

    首先,为了快速回答,我将您的tasks.jsonsn-p 编辑为:

    {
      "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 中创建一个紧凑的可执行文件。

    而且,为了使用 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})
    

    最后,为了使这个过程REAL,你需要在VSCode中去Terminal > Run Build Task。

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

    我希望我以一种可以理解的方式使用英语而不是混淆。

    【讨论】:

    • 啊,我也忘了告诉你可以在 VSCode 中使用更“图形化”的界面,即扩展中的 CMake 和 CMake Tools。您可以从状态栏中选择是Debug还是Release,也可以选择要编译的工具包。
    • 谢谢罗尼。这为我指明了正确的方向
    • 很抱歉我无法给出完整的答案。我希望它可以帮助您采取措施解决您的问题!
    猜你喜欢
    • 2012-11-07
    • 2014-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-12
    • 1970-01-01
    相关资源
    最近更新 更多