gcc编译器和g++编译器_C ++编译器说明:什么是编译器,以及如何使用它?

gcc编译器和g++编译器

C ++编译器简介 (Intro to C++ Compilers)

In order to get started with C++, you will need to learn a little about compilers and how C++ runs on your computer.

为了开始使用C ++,您将需要了解一些有关编译器以及C ++如何在计算机上运行的知识。

When all is said and done, computers only understand one language, machine language. Machine language is entirely made up of binary bits, or 0s and 1s.

说完所有的话,计算机只能理解一种语言,机器语言。 机器语言完全由二进制位(即0和1)组成。

While it would be possible to program in binary, it would be incredibly tedious and time consuming. So, we humans developed programming languages to make it easier to develop software.

尽管可以用二进制进行编程,但是这将是非常繁琐且耗时的。 因此,我们人类开发了编程语言,以便更轻松地开发软件。

Assembly language is a direct 1 to 1 with machine language. Languages like C, C++, and COBOL are a little higher and need to be compiled down. It goes even higher. Languages like JavaScript and Python have components that get translated into C++ or other low level languages before they get compiled, effectively making them “higher” languages than C or C++.

汇编语言与机器语言是直接一对一的关系。 诸如C,C ++和COBOL之类的语言稍高一些,需要进行编译。 甚至更高。 像JavaScript和Python这样的语言,其组件在编译之前就已翻译成C ++或其他低级语言,从而有效地使它们成为比C或C ++更高的语言。

Because computer architecture is made up of electronic switches and cables that can only work with binary 1s and 0s, you need a compiler to translate your code from high level C++ to machine language that the CPU can understand.

由于计算机体系结构由只能与二进制1和0配合使用的电子开关和电缆组成,因此需要编译器将代码从高级C ++转换为CPU可以理解的机器语言。

编译器如何工作 (How compilers work)

Compilers are utility programs that take your code and transform it into executable machine code files.

编译器是实用程序,用于接收您的代码并将其转换为可执行的机器代码文件。

When you run a compiler on your code, first, the preprocessor reads the source code (the C++ file you just wrote). The preprocessor searches for any preprocessor directives (lines of code starting with a #). Preprocessor directives cause the preprocessor to change your code in some way (by usually adding some library or another C++ file).

在代码上运行编译器时,首先,预处理器读取源代码(您刚编写的C ++文件)。 预处理程序搜索任何预处理程序指令(以#开头的代码行)。 预处理器指令使预处理器以某种方式(通常通过添加一些库或另一个C ++文件)更改代码。

Next, the compiler works through the preprocessed code line by line translating each line into the appropriate machine language instruction. This will also uncover any syntax errors that are present in your source code and will throw an error to the command line.

接下来,编译器通过将每行转换为适当的机器语言指令来完成预处理的代码行。 这还将发现源代码中存在的任何语法错误,并将错误引发给命令行。

Finally, if no errors are present, the compiler creates an object file with the machine language binary necessary to run on your machine. While the object file that the compiler just created is likely enough to do something on your computer, it still isn’t a working executable of your C++ program. There is a final important step to reach an executable program.

最后,如果没有错误,编译器将使用在您的计算机上运行所必需的机器语言二进制文件创建一个目标文件。 尽管编译器刚刚创建的目标文件可能足以在您的计算机上执行某些操作,但它仍然不是C ++程序的有效可执行文件。 获得可执行程序是最后一个重要步骤。

C++ contains a vast library to aid in performing difficult tasks like I/O and hardware manipulation. You can include these libraries with preprocessor directives, but the preprocessor doesn’t automatically add them to your code.

C ++包含一个庞大的库,可以帮助执行诸如I / O和硬件操纵之类的困难任务。 您可以在预处理程序指令中包含这些库,但是预处理程序不会自动将它们添加到您的代码中。

In order for you to have a final executable program, another utility known as the linker must combine your object files with the library functions necessary to run the code.

为了使您拥有最终的可执行程序,另一个称为链接器的实用程序必须将您的目标文件与运行代码所需的库函数结合在一起。

Think of it as having all the necessary blocks to build a house. The compiler made all the blocks but the linker is the one that sticks them all together to finally create a house. Once this is done, you now have a functioning executable file!

认为它具有建造房屋的所有必要块。 编译器创建了所有块,但链接器是将所有块粘贴在一起以最终创建房屋的链接器。 完成此操作后,您将拥有一个可运行的可执行文件!

如何编译文件 (How to Compile a file)

Let’s say you have a C++ file called helloWorld.cpp

假设您有一个名为helloWorld.cpp的C ++文件...

如果您使用的是Windows (If you are on Windows)

使用和IDE一样的CodeBlocks (Using and IDE like CodeBlocks)

It is as simple as clicking the build and run buttons, they will create a file in the project folder.

就像单击“生成”和“运行”按钮一样简单,它们将在项目文件夹中创建一个文件。

使用命令提示符 (Using Command Prompt)

  1. Open a Developer Command Prompt - For this step, you will need to have Microsoft Visual Studio or some other IDE that enables you to compile your program from the command line. You can also search online for C++ compilers.

    打开开发人员命令提示符-对于此步骤,您将需要具有Microsoft Visual Studio或其他使您能够从命令行编译程序的IDE。 您也可以在线搜索C ++编译器。
  2. Navigate to the source code directly

    直接导航到源代码
  3. Run the Compiler on your source code (assuming you are using the Microsoft Visual Studio compiler) cl /EHsc helloWorld.cpp

    在源代码上运行编译器(假设您正在使用Microsoft Visual Studio编译器) cl /EHsc helloWorld.cpp

This will now create an object file and automatically link it for you. If you look in that same folder, you will see a hellWorld.exe executable file (note the exe extension) is now present.

现在,这将创建一个目标文件并自动为您链接。 如果您在同一文件夹中查找,将会看到一个hellWorld.exe可执行文件(注意exe扩展名)。

  1. Type helloWorld into the prompt to run the executable

    在提示符下键入helloWorld以运行可执行文件

Alternatively, many IDEs allow for quick building and viewing of your program. This may be easier since your version of windows may not come pre packaged with a compiler utility.

另外,许多IDE允许快速构建和查看程序。 这可能会更容易,因为您的Windows版本可能未随编译器实用程序预先打包在一起。

如果您使用的是Linux或OSX (If you are on Linux or OSX)

  1. Open up a terminal window and navigate to the source code directory

    打开一个终端窗口并导航到源代码目录
  2. Run the Compiler on your source code g++ helloWorld.cpp -o helloWorld

    在您的源代码上运行编译器g++ helloWorld.cpp -o helloWorld

This will create an object file and automatically link it for you. Look in the folder and you will see a helloWorld.exe executable file (note the exe extension).

这将创建一个目标文件并自动为您链接。 在文件夹中查找,您将看到helloWorld.exe可执行文件(请注意exe扩展名)。

  1. Type ./helloWorld in the terminal window to run the executable file

    在终端窗口中键入./helloWorld以运行可执行文件

g++ is the standard Linux compiler and is a great utility. It comes packaged with the operating system.

g ++是标准的Linux编译器,是一个很棒的实用程序。 它与操作系统一起提供。

NOTE: to compile and execute your code directly, run g++ -o helloWorld helloWorld.cpp; ./helloWorld so when you need to compile and run your code multiple times, up arrow-enter.

注意:要直接编译和执行代码,请运行g++ -o helloWorld helloWorld.cpp; ./helloWorld g++ -o helloWorld helloWorld.cpp; ./helloWorld因此当您需要多次编译和运行代码时,请向上箭头输入。

There are a number of different types of compilers. The two listed are the two that are usually packaged with Windows or Linux/OSX.

有许多不同类型的编译器。 列出的两个是通常与Windows或Linux / OSX打包在一起的两个。

有关C ++的更多信息 (More info on C++)

  • Intro to C++ programming

    C ++编程简介

  • Object-oriented programming in C++

    C ++中的面向对象编程

  • Graph algorithms and data structures explained in C++ and Java

    C ++和Java中解释的图形算法和数据结构

  • How to compile your C++ code in VS Code

    如何在VS Code中编译C ++代码

翻译自: https://www.freecodecamp.org/news/c-compiler-explained-what-is-the-compiler-and-how-do-you-use-it/

gcc编译器和g++编译器

你可能感兴趣的:(编译器,c++,数据结构,java,linux)