【问题标题】:Compiling a cpp file with vscode, in Ubuntu在 Ubuntu 中使用 vscode 编译 cpp 文件
【发布时间】:2022-06-18 05:55:13
【问题描述】:

我正在尝试关注this link,了解如何在 ubuntu 中开始使用 c++ 和 vscode。

我已经安装了最新版本的 gcc。 运行sudo apt-get install build-essential gdb 给出:

Reading package lists... Done
Building dependency tree       
Reading state information... Done
build-essential is already the newest version (12.8ubuntu1.1).
build-essential set to manually installed.
gdb is already the newest version (9.2-0ubuntu1~20.04.1).
gdb set to manually installed.
0 upgraded, 0 newly installed, 0 to remove and 2 not upgraded.

但是,当我进入创建配置文件的阶段时,我没有选择C/C++: g++ build active file。我只有

所以,我选择 /usr/bin/cpp。然后我构建文件,并获得成功消息。 但是,当运行新创建的可执行文件时,我收到了几条错误消息:

./helloworld: line 17: namespace: command not found
./helloworld: line 23: syntax error near unexpected token `('
./helloworld: line 23: `  typedef decltype(nullptr) nullptr_t;'

奇怪的是helloworld文件中的代码行在第16行结束,所以我认为编译器有问题......

【问题讨论】:

  • 老实说,我总是发现在 Windows 以外的平台上编译时使用 Cmake 或 makefile 更容易。
  • 怀疑编译器有问题,更有可能是vs代码构建系统有问题。我想如果你在命令行上编译它会工作得很好。
  • @Taekahn 你是对的......我只是从教程中复制了 tasks.json,并覆盖了以前的版本。然后我运行了可执行文件,它工作了。

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


【解决方案1】:

最好让 GCC 在你的命令行中运行,然后使用 VS Code 任务让它运行。


我建议您尽可能创建最简单的项目结构。仅使用一个项目目录和一个名为 main.cpp 的文件。

看起来像这样的东西:
    PROJECT (dir)  // path = ./
      │
      └──> main.cpp (file)  // path = ./main.cpp
一旦你有一个带有main.cpp 的目录,做两件事中的一件:
  1. 使用以下命令将 Hello World 示例添加到您的 main.cpp 文件中。
  $> echo -e "\n#include <iostream>\n\nusing namespace std;\n\nint main()\n{\n   cout << \"Hello World\!\" << endl;\n}" > main.cpp


  1. 或者将下面的代码复制粘贴到main.cpp
#include <iostream>

using namespace std;

int main()
{
   cout << "Hello World!" << endl;
}


仅供参考:您应该从命令行而不是 vscode 执行此操作(直到您创建了我将在下面显示的 vscode 任务)

另外需要注意的是,你的命令行应该指向你的项目目录,也就是你创建的带有main.cpp的目录。

在您的项目目录中执行以下命令。

$> g++ ./main.cpp -o build

如果您的文件正确编译并构建了您的可执行文件,您应该能够使用ls 命令在您的项目目录中看到一个名为 build 的新文件。

如果一切顺利,新的build 文件是一个可执行文件。通过输入...执行它

$> ./build

你应该会看到“Hello World!”


此时使用以下命令...

$> code .
VS Code 应该打开到您的项目目录。

现在使用 vscode 创建另一个目录,并将其命名为 ./.vscode

然后在 ./.vscode 目录中添加一个名为 tasks.json

的文件

文件完整路径名将是:./.vscode/tasks.json

那么您将需要将以下内容添加到您的任务文件中

  "version": "2.0.0",
  "tasks": [
    {
      "type": "shell",
      "label": "GCC: My Compilation Task",
      "command": "/usr/bin/g++",
      "args": ["-g", "./main.cpp", "-o", "./build"],
      "options": {
        "cwd": "/usr/bin"
      },
      "problemMatcher": ["$gcc"],
      "group": {
        "kind": "build",
        "isDefault": true
      }
    }
  ]
}

然后您应该可以按 F1 并输入 RUN TASK,当您在快速菜单中看到显示 RUN TASK 的选项时strong> 点击它,然后在你的tasks.json文件中选择与label key同名的任务,即"GCC: My Compilation Task"

【讨论】:

  • @j D3V 任务文件条目的开头似乎缺少一个左大括号 {
  • 使用建议的 tasks.json 条目会出现以下错误(在 Ubuntu 22.04 上):cc1plus: fatal error: ttt.cpp: No such file or directory compilation terminated. The terminal process "/usr/bin/bash '-c', '/usr/bin/g++ -g ttt.cpp -o ./ttt'" failed to launch (exit code: 1). 但是,如果我在“args”中输入完整路径,它可以正常工作,例如:"args": ["-g", "/home/matthew/Repos/TinyTimetablingToy/ttt.cpp", "-o", "/home/matthew/Repos/TinyTimetablingToy/ttt"],
  • @mathew 那是因为我把./main.cpp 分开了,现在就试试吧。
猜你喜欢
  • 2015-07-09
  • 1970-01-01
  • 2017-04-28
  • 1970-01-01
  • 1970-01-01
  • 2016-05-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多