【问题标题】:Compile multiple .cpp files in Visual Studio Code with clang++ on macOS在 macOS 上使用 clang++ 在 Visual Studio Code 中编译多个 .cpp 文件
【发布时间】:2020-09-04 15:27:58
【问题描述】:

我正在尝试在 macOS 上使用 clang++ 构建一个包含多个 *.cpp 文件的简单项目。

tasks.json:

    {
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "clang++ build active file",
            "command": "/usr/bin/clang++",
            "args": [
                "-std=c++17",
                "-Wall",
                "-Wextra",
                "-Weffc++",
                "-Wconversion",
                "-pedantic-errors",
                "-stdlib=libc++",
                "-g",
                "${workspaceFolder}/*.cpp",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

生成的命令如下所示:

/usr/bin/clang++ -std=c++17 -Wall -Wextra -Weffc++ -Wconversion -pedantic-errors -stdlib=libc++ -g '/Users/USER/Developer/WORKINGDIR/Lesson 2/Lesson 2.8/*.cpp' -o '/Users/USER/Developer/WORKINGDIR/Lesson 2/Lesson 2.8/main' 

但是编译器会报错:

clang: error: no such file or directory: '/Users/USER/Developer/WORKINGDIR/Lesson 2/Lesson 2.8/*.cpp'

但是如果我改变指向文件的字符串:

'/Users/USER/Developer/WORKINGDIR/Lesson 2/Lesson 2.8/*.cpp'

对此(不带引号并添加“\”):

/Users/USER/Developer/WORKINGDIR/Lesson\ 2/Lesson\ 2.8/*.cpp

一切正常。

但是如何在 tasks.json 中配置相同的字符串?或者我应该在那里改变什么才能正常工作?

【问题讨论】:

  • 试着用引号括起来,比如'\"/Users/USER/Developer/WORKINGDIR/Lesson 2/Lesson 2.8/*.cpp\"'
  • @VladimirNikitin 我正在尝试配置 VSCode tasks.json 文件。按照您建议的方式,它在那里不起作用。不过还是谢谢。
  • @AsteroidsWithWings 这是同一个问题,是的。但是那个错误很旧,我正在使用最新版本的配置。所以,遗憾的是它没有。我向 Microsoft 报告了一个错误。感谢您对这个问题的贡献。

标签: c++ macos visual-studio-code


【解决方案1】:

要在 macOS 上的 VS Code 中使用 clang++ 编译多个 cpp 文件,您需要配置您的 tasks.json 文件并确保 args 具有您项目的正确文件路径。这对我有用:

  1. 我在 Visual Studio 中创建了一个新项目,并将 .cpp 和 .h 文件放在了直接文件夹中。
  2. 然后,我更新了我的 tasks.json,使用 VS Code variables 指向项目中的正确位置。示例:

注意,根据我的项目结构,使用 ${fileDirname}${workspaceFolder} 作为变量。

{       
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "clang++ build active file",
            "command": "/usr/bin/clang++",
            "args": [
                // "${fileDirname}/*.cpp",
                "${workspaceFolder}/*.cpp", 
                "-o",
                //"${fileDirname}/${fileBasenameNoExtension}"               
                "${workspaceFolder}/${fileBasenameNoExtension}"             
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
  }
  1. 在 VS Code 中,我选择了 Terminal > Run Build Task... 输出文件
  2. 我在 VS Code 中打开了一个终端窗口并运行 ./[filename](其中文件名是输出文件的名称)并执行了代码。

【讨论】:

    【解决方案2】:

    不要让tasks.json 复杂化。最好使用诸如 CMake 之类的东西来处理所有这些配置(C++ 版本、编译标志和警告等),然后调用 make(或 ninja 或您使用 CMake 选择的任何其他生成器)。

    然后在您的tasks.json 中,“options”中“cwd”的值将指向构建文件夹(运行 CMake 命令的文件夹)而不是源文件夹,“args”将是CMake 中的目标(如果要构建所有目标,则为空),“命令”将是诸如“/usr/bin/ninja”或“/usr/bin/make”之类的东西,具体取决于您在 CMake 中选择的生成器。

    这还有一个好处,即其他人会从中受益(或会为您创建),无论他们的编辑如何。

    您正在使用的标志的可能CMakeLists.txt 文件类似于

    project(TheProjectName)
    cmake_minimum_required(VERSION 3.10)
    
    set(CMAKE_CXX_STANDARD 17)
    set(CMAKE_CXX_STANDARD_REQUIRED YES)
    
    add_compile_options(-Wall)
    add_compile_options(-Wextra)
    add_compile_options(-pedantic-errors)
    add_compile_options(-Weffc++)
    add_compile_options(-Wconversion)
    add_compile_options(-stdlib=libc++)
    
    # This is the executable target "main" is the name of the executable
    # and you can change to something else
    add_executable(main main.cpp
      other_file.h
      other_file.cpp
      some_other_file.h
      some_other_file.cpp
      )
    

    CMakeLists.txt 中未设置用于指定调试版本的 -g 选项。相反,当您运行 cmake 时,您将其构建类型设置为 Debug

    要使用这个CMakeLists.txt 文件,请在工作区文件夹中创建一个build 文件夹,然后在终端中访问它。然后运行命令 cmake .. -DCMAKE_BUILD_TYPE=Debug 一次。之后,您可以仅使用 build 文件夹中的 make 编译项目。在tasks.json 中设置它只是将“cwd”设置为指向${workspaceFolder}/build,并将命令设置为/usr/bin/make


    最后一点,如果您同时安装了 clang 和 gcc,那么当您运行 cmake 时,它可能会使用 gcc 而不是 clang。具体到 clang 运行 cmake 命令,指定 CXX 和 CC 环境变量以指向 clang。也就是说,cmake 命令应该运行(一次从构建文件夹)作为

    CXX=clang++ CC=clang cmake .. -DCMAKE_BUILD_TYPE=Debug
    

    【讨论】:

    • 感谢您的解决方案。我现在正在寻找一个更简单的解决方案,但你的绝对值得一试。
    猜你喜欢
    • 2020-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-11
    • 2022-07-08
    • 2019-01-14
    • 2021-12-19
    • 1970-01-01
    相关资源
    最近更新 更多