在 Ubuntu 中用 vscode 编译一个 cpp 文件
Compiling a cpp file with vscode, in Ubuntu
我正在尝试按照 this link 如何开始使用 C++ 和 ubuntu 中的 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 行结束,所以我认为编译器有问题...
最好让 GCC 在命令行中工作,然后使用 VS Code 任务让它工作。
我建议您尽可能创建最简单的项目结构。仅使用一个项目目录和一个名为 main.cpp
.
的文件
看起来像这样的东西:
PROJECT (dir) // path = ./
│
└──> main.cpp (file) // path = ./main.cpp
一旦你有了一个包含 main.cpp
的目录,做两件事中的一件:
- 使用以下命令将 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
- 或将下面的代码复制粘贴到
main.cpp
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!" << endl;
}
FYI: You should be doing this from the command-line not vscode (not until you create the vscode task which I will show bellow)
另一件需要注意的事情是,您的命令行应该指向您的项目目录,即您使用 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
单击它,然后 select 与 tasks.json
文件中的标签键同名的任务,即 "GCC: My Compilation Task"
我正在尝试按照 this link 如何开始使用 C++ 和 ubuntu 中的 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 行结束,所以我认为编译器有问题...
最好让 GCC 在命令行中工作,然后使用 VS Code 任务让它工作。
我建议您尽可能创建最简单的项目结构。仅使用一个项目目录和一个名为 main.cpp
.
看起来像这样的东西:
PROJECT (dir) // path = ./
│
└──> main.cpp (file) // path = ./main.cpp
一旦你有了一个包含 main.cpp
的目录,做两件事中的一件:
- 使用以下命令将 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
- 或将下面的代码复制粘贴到
main.cpp
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!" << endl;
}
FYI: You should be doing this from the command-line not vscode (not until you create the vscode task which I will show bellow)
另一件需要注意的事情是,您的命令行应该指向您的项目目录,即您使用 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
单击它,然后 select 与 tasks.json
文件中的标签键同名的任务,即 "GCC: My Compilation Task"