【问题标题】:Building multiples files in VSCode and WSL and C++在 VSCode 和 WSL 和 C++ 中构建多个文件
【发布时间】:2020-03-24 03:13:39
【问题描述】:

我已经测试过使用 WSL 在 vscode 上对多个文件进行编码

code .

在好文件夹中。

我有三个文件。

这是我得到的错误信息,而代码是正确的:

g++:错误:helloworld.cpp:没有这样的文件或目录

g++:错误:file2.cpp:没有这样的文件或目录 g++:错误:file3.cpp:没有这样的文件或目录

g++:致命错误:没有输入文件

编译终止。

终端进程以退出代码终止:1

终端将被任务重用,按任意键关闭它。

这是我的 tasks.json 文件:

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "g++ build active file",
            "command": "/usr/bin/g++",
            "args": [
                "-g",
                "helloworld.cpp","file2.cpp","file3.cpp",
                "-o",
                "executable.out"
            ],
            "options": {
                "cwd": "/usr/bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

请您帮忙解决这个问题,而这里提到的单个文件非常有效:

https://code.visualstudio.com/docs/cpp/config-wsl#_modifying-tasksjson

我还指出 .cpp 文件不在 .vscode 文件夹中,而是在项目的主文件夹中。 提前致谢

ps:我看到了几个关于这个问题的帖子,但这并没有解决我的问题。

【问题讨论】:

  • 如果你在终端运行你想要的目录下的构建命令,输出是什么?
  • 我在终端手动测试,做 g++ -g helloworld.cpp file2.cpp -o fileexec ,它编译。但我的问题是不同的,你可以看到

标签: c++ visual-studio-code windows-subsystem-for-linux


【解决方案1】:

tasks.json 的原始示例有参数:

            "args": ["-g", "${file}", "-o", "${fileDirname}/${fileBasenameNoExtension}"],
            "options": {
                "cwd": "/usr/bin"
            },

这很好,因为${file} 将包含实际文件的绝对路径,所以cwd 设置在哪里并不重要。如果你有:

            "args": [
                "-g",
                "helloworld.cpp","file2.cpp","file3.cpp",
                "-o",
                "executable.out"
            ],
            "options": {
                "cwd": "/usr/bin"
            },

VSCode 正在寻找文件 /usr/bin/helloworld.cpp/usr/bin/file2.cpp/usr/bin/file3.cpp - 这显然是错误的。

解决方案是将当前目录更改为当前工作区文件夹:

"options": {
    "cwd": "${workspaceFolder}",
},

所有可能的变量请咨询reference

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2023-01-12
  • 1970-01-01
  • 2021-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-04
相关资源
最近更新 更多