c++ - 使用 Visual Studio Code 在 Linux 中创建并编译 "hello world"应用程序

标签 c++ linux visual-studio visual-studio-code

我是 Linux 新手,但现在在 Windows 中开发过 C/C++ 有一段时间了。

我安装了 Linux Ubuntu 16.4 和 Visual Studio Code。

我创建了一个文件夹,并在该文件夹中创建了一个名为 main.cpp 的文件,并在该文件中编写了:

#include <iostream>
void main()
{
    std::cout << "Hello World" << std::endl;
}

现在我想编译并运行它,并可能对其进行调试(逐步了解如何调试一个简单的应用程序)。

我该怎么做?

有关于使用 Visual Studio Code 在 Linux 中设置开发系统的教程吗?

我安装了“C/C++ for Visual Studio Code”,但仍然无法编译和运行示例应用程序。

编辑1

我已经安装了编译器并且可以使用编译我的代码

g++ main.cpp

并获取a.out

如何配置 VSC 来自动执行此处理器并生成依赖项,如果出现错误,请在编辑器上打开有错误的文件并显示生成错误的行。另外,在调试期间,当我单步执行代码时,会显示源代码。

这些是我对开发系统的一些基本要求,否则我称其为编辑器而不是开发系统。

最佳答案

首先,你必须安装一个编译器,我推荐GCC(Ubuntu通常没有自带编译器,尽管它主要是用C/C++编写的)

其次,编译你的程序,具体方法如下

To compile the program, open the terminal and move on to the target directory type the command – (where gcc implies compiler name, then it asks for the file name of the source program while -o option specifies the file name of the output program)

gcc hello.c -o hello1

If there is no syntax/semantic error in you program then the compiler will successfully generate an executable file, otherwise fix the problem in your code.

但是,这仅适用于 C,以下是 C++ 的操作方法(仅当扩展名是 .cpp 时)

The steps are almost same as above but you need to install g++ compiler, the file extension should be .cpp and in compilation phase replace gcc with g++. To install G++ compiler, execute the command -

sudo apt-get install g++

现在的编译命令是:

g++ hello.cpp -o hello1

不用说,您应该将“hello.cpp”替换为您的文件名,并将“hello1”替换为您希望“exe”文件具有的名称

关于c++ - 使用 Visual Studio Code 在 Linux 中创建并编译 "hello world"应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42398699/

相关文章:

c++ - 在哪里可以找到用于训练 opencv 人脸识别系统的完整 img 包?

linux - 有没有办法改变 symlink/proc/self/exe 的目标?

linux - Rsync 复制未更改的文件(USB 驱动器、FAT32)

c++ - 由于安装(EXPORT "foo-targets"...)在导出集中多次包含目标 "foo",aws-cpp-sdk 在 Windows 构建期间失败

新的 .NET 6 控制台模板中的 C# 函数重载不起作用

.net - 如何在 Windows 中从非托管 C++ 代码调用托管 .NET 代码,反之亦然?

c++ - Boost.Tests 入口点在哪里?

c++ - 水平对齐动态创建的小部件 qt c++

linux - 重定向和服务器迁移 Windows -> Linux

asp.net - 启用 32 位以在 VIsual Studio 上调试 ASP.NET 项目吗?