mirror of
https://github.com/LCTT/TranslateProject.git
synced 2026-08-29 04:23:33 +08:00
@@ -1,138 +0,0 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (robsean)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (How to Write, Compile and Run a C Program in Ubuntu and Other Linux Distributions [Beginner’s Tip])
|
||||
[#]: via: (https://itsfoss.com/run-c-program-linux/)
|
||||
[#]: author: (Abhishek Prakash https://itsfoss.com/author/abhishek/)
|
||||
|
||||
How to Write, Compile and Run a C Program in Ubuntu and Other Linux Distributions [Beginner’s Tip]
|
||||
======
|
||||
|
||||
How do you program in C on Linux? It is indeed very easy and consists of three simple steps.
|
||||
|
||||
**Step 1**: You write your program and save the file with a .c extension. For example, my_program.c.
|
||||
|
||||
**Step 2**: You compile the program and generate the object file using gcc compiler in a terminal like this:
|
||||
|
||||
```
|
||||
gcc -o my_program my_program.c
|
||||
```
|
||||
|
||||
**Step 3**: You run the generated object file to run your C program in Linux:
|
||||
|
||||
```
|
||||
./my_program
|
||||
```
|
||||
|
||||
![][1]
|
||||
|
||||
This was just the quick summary on how to compile and run C program in Linux. If you are new to either C or Linux, I’ll show these steps in detail so that you feel comfortable coding C program in Linux environment.
|
||||
|
||||
In fact, I’ll discuss how to run C programs in Linux terminal as well as in code editor.
|
||||
|
||||
![][2]
|
||||
|
||||
### Method 1: How to run C programs in Linux terminal
|
||||
|
||||
In order to run a C program in Linux, you need to have a C compiler present on your systems. The most popular compiler is gcc ([GNU Compiler Collection][3]).
|
||||
|
||||
You can install gcc using your distribution’s package manager. In Debian and Ubuntu-based Linux distributions, use the apt command:
|
||||
|
||||
```
|
||||
sudo apt install gcc
|
||||
```
|
||||
|
||||
Switch to directory where you have kept your C program (or provide the path) and then generate the object file by compiling the program:
|
||||
|
||||
```
|
||||
gcc -o my_program my_program.c
|
||||
```
|
||||
|
||||
Keep in mind that it is optional to provide the output object file (-o my_program). If you won’t do that, an object file named a.out will be automatically generated. But this is not good because it will be overwritten for each C program and you won’t be able to know which program the a.out object file belongs to.
|
||||
|
||||
Once you have your object file generated, run it to run the C program. It is already executable. Simple use it like this:
|
||||
|
||||
```
|
||||
./my_program
|
||||
```
|
||||
|
||||
And it will display the desired output, if your program is correct. As you can see, this is not very different from [running C++ programs in Linux][4].
|
||||
|
||||
_**Every time you make a change in your program, you have to compile it first and then run the generated object file to run the C program.**_
|
||||
|
||||
### Method 2: How to run C programs in Linux using a code editor like Visual Studio Code
|
||||
|
||||
Not everyone is comfortable with command line and terminal and I totally understand that.
|
||||
|
||||
You can use a proper C/C++ IDE like Eclipse or Code Blocks but they are often too heavy programs and more suitable for large projects.
|
||||
|
||||
I recommend using an open source code editor like Visual Studio Code or Atom. These are basically text editors and you can install add-ons to compile and run programs directly from the graphical code editor.
|
||||
|
||||
I am using [Visual Studio Code editor][5] in this example. It’s a hugely [popular open source code editor][6] from Microsoft.
|
||||
|
||||
First thing first, [install Visual Studio Code in Ubuntu][7] from the software center. For other distributions, please check your Linux distribution’s package manager or software center. You may also check the official website for more information.
|
||||
|
||||
Start Visual Studio Code and open/create a project and create your C program here. I am using a sample Hello World program.
|
||||
|
||||
![][8]
|
||||
|
||||
You must ensure that you have gcc compiler installed on your Linux system.
|
||||
|
||||
```
|
||||
sudo apt install gcc
|
||||
```
|
||||
|
||||
Next thing you would want is to use an extension that allows you to run the C code. Microsoft may prompt you for installing its own extension for C/C++ program but it is complicated to setup and hence I won’t recommend it.
|
||||
|
||||
Instead, I suggest using the Code Runner extension. It’s a no-nonsense extension and you can run C and C++ code easily without additional configuration.
|
||||
|
||||
Go to the Extensions tab and search for ‘Code Runner’ and install it.
|
||||
|
||||
![Install Code Runner extension for running C/C++ program][9]
|
||||
|
||||
Restart Visual Studio Code. Now, you should be able to run the C code by using one of the following way:
|
||||
|
||||
* Using the shortcut Ctrl+Alt+N.
|
||||
* Press F1 and then select or type Run Code.
|
||||
* Right click the text editor and the click Run code from context menu.
|
||||
|
||||
|
||||
|
||||
![Right click the program file and choose Run Code][10]
|
||||
|
||||
When you run the program, it is compiled automatically and then run. You can see the output in terminal that is opened at the bottom of the editor. What could be better than this?
|
||||
|
||||
![Program output is displayed in the bottom section of the editor][11]
|
||||
|
||||
Which method do you prefer?
|
||||
|
||||
Running a few C programs in Linux command line is okay but using a code editor is much easier and saves time. Won’t you agree?
|
||||
|
||||
I let you decide whichever method you want to use.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://itsfoss.com/run-c-program-linux/
|
||||
|
||||
作者:[Abhishek Prakash][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://itsfoss.com/author/abhishek/
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://i0.wp.com/itsfoss.com/wp-content/uploads/2020/11/running-c-program-linux.png?resize=795%2C399&ssl=1
|
||||
[2]: https://i1.wp.com/itsfoss.com/wp-content/uploads/2020/11/Run-C-Program-Linux.png?resize=800%2C450&ssl=1
|
||||
[3]: https://gcc.gnu.org/
|
||||
[4]: https://itsfoss.com/c-plus-plus-ubuntu/
|
||||
[5]: https://code.visualstudio.com
|
||||
[6]: https://itsfoss.com/best-modern-open-source-code-editors-for-linux/
|
||||
[7]: https://itsfoss.com/install-visual-studio-code-ubuntu/
|
||||
[8]: https://i1.wp.com/itsfoss.com/wp-content/uploads/2020/11/c-program-visual-studio-code-linux.png?resize=800%2C441&ssl=1
|
||||
[9]: https://i2.wp.com/itsfoss.com/wp-content/uploads/2020/11/running-c-program-in-linux-with-visual-studio-code.png?resize=800%2C500&ssl=1
|
||||
[10]: https://i2.wp.com/itsfoss.com/wp-content/uploads/2020/11/running-c-program-in-linux-with-visual-studio-code.jpg?resize=800%2C500&ssl=1
|
||||
[11]: https://i0.wp.com/itsfoss.com/wp-content/uploads/2020/11/run-c-program-in-linux-with-visual-studio-code.jpg?resize=800%2C500&ssl=1
|
||||
@@ -0,0 +1,138 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (robsean)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (How to Write, Compile and Run a C Program in Ubuntu and Other Linux Distributions [Beginner’s Tip])
|
||||
[#]: via: (https://itsfoss.com/run-c-program-linux/)
|
||||
[#]: author: (Abhishek Prakash https://itsfoss.com/author/abhishek/)
|
||||
|
||||
如何在 Ubuntu 和其它的 Linux 发行版中编写,编译和运行一个 C 程序 [Beginner’s Tip]
|
||||
======
|
||||
|
||||
你是如何在 Linux 上使用 C 编写你的程序的?它确实是非常简单的,它由三个简单的步骤组成的。
|
||||
|
||||
**步骤 1**: 你编写你的程序,并使用一个 .c 的扩展名进行保存。例如,my_program.c 。
|
||||
|
||||
**步骤 2**: 你在一个终端中使用 gcc 编译器来编译程序并生成目标文件,像这样:
|
||||
|
||||
```
|
||||
gcc -o my_program my_program.c
|
||||
```
|
||||
|
||||
**步骤 3**: 在 Linux 中,你以运行生成的对象文件的方式来运行你的 C 程序:
|
||||
|
||||
```
|
||||
./my_program
|
||||
```
|
||||
|
||||
![][1]
|
||||
|
||||
这只是如何在 Linux 中编译和运行 C 程序的简要总结。假设你是 C 语言或 Linux 系统的新手,我将仔细演示这些步骤,以便你能在 Linux 环境中舒服地编写 C 程序。
|
||||
|
||||
事实上,我将讨论如何在 Linux 终端中以及在代码编辑器中运行 C 程序。
|
||||
|
||||
![][2]
|
||||
|
||||
### 方法 1: 在 Linux 终端中运行 C 程序
|
||||
|
||||
为了在 Linux 中运行一个 C 程序,你需要在你的系统上有一个 C 编译器。最流行的编译器是 gcc ([GNU 编译器套件][3])。
|
||||
|
||||
你可以使用你发行版的软件包管理器来安装 gcc 。在基于 Debian 和 Ubuntu 的 Linux 发行版中,使用 apt 命令:
|
||||
|
||||
```
|
||||
sudo apt install gcc
|
||||
```
|
||||
|
||||
切换到你保存你的 C 程序的目录(或者提供路径),然后通过编译程序生成对象文件:
|
||||
|
||||
```
|
||||
gcc -o my_program my_program.c
|
||||
```
|
||||
|
||||
记住,提供输出对象文件(-o my_program)是可选的。如果你不提供,那么将自动生成一个名称 a.out 的对象文件。但是这样并不好,因为它将会覆盖每一个已生成的 C 程序,而且你也不知道这个 a.out 对象文件究竟属于哪个程序。
|
||||
|
||||
在你的对象文件生成后,运行它来运行 C 程序。它已经能够可执行了。像这样简单地使用它:
|
||||
|
||||
```
|
||||
./my_program
|
||||
```
|
||||
|
||||
接下来,如果你的程序是正确的,它将显示出你所期望的输出。正如你所能够看到的,[在 Linux 中运行 C++程序][4] 并不是很难。
|
||||
|
||||
_**每更改一次你的程序,你都被必须先重新编译它,然后再次运行生成的对象文件来运行这个新的 C 程序。**_
|
||||
|
||||
### 方法 2: 如何在 Linux 中使用一个诸如 Visual Studio Code 之类的代码编辑器来运行 C 程序
|
||||
|
||||
并不是每一个人都能适应命令行和终端,我完全理解这一点。
|
||||
|
||||
你可以使用一个诸如 Eclipse 或 Code Blocks 之类的真正的 C/C++ IDE ,但是它们是很重量级的程序,通常更适合于大型的工程。
|
||||
|
||||
我建议使用一个开放源码的代码编辑器,像 Visual Studio Code 或 Atom 。总体来说它们是文本编辑器,但是你可以通过安装附加组件来直接在图形化代码编辑器中编译和运行程序。
|
||||
|
||||
在这个示例中,我使用 [Visual Studio Code ][5] 编辑器。它是来自微软的一个非常 [流行的开放源码的代码编辑器][6] 。
|
||||
|
||||
首先,在 Ubuntu 的 [软件中心中安装 Visual Studio Code ][7] 。对于其它发行版来说,请检查你的 Linux 发行版的软件包管理器或软件中心。你可以检查它的官方网站来查看更多的信息。
|
||||
|
||||
启动 Visual Studio Code ,打开/创建一个工程,接下来创建你的 C 程序。我使用一个简单的 Hello World 程序作为示例。
|
||||
|
||||
![][8]
|
||||
|
||||
你必须确保你已经在你的 Linux 系统上安装了 gcc 编译器。
|
||||
|
||||
```
|
||||
sudo apt install gcc
|
||||
```
|
||||
|
||||
接下来你想做事是使用一个允许你运行 C 代码的扩展。微软可能会提示你安装它自己的 C/C++ 程序扩展,但是安装是很复杂的,因此我不建议你使用它。
|
||||
|
||||
作为替换,我建议你使用 Code Runner 扩展。它是一个简单直接的扩展,你可以在不使用额外配置的情况下轻松地运行 C 和 C++ 代码。
|
||||
|
||||
转到扩展标签页,在其中搜索和安装 ‘Code Runner’ 。
|
||||
|
||||
![安装 Code Runner 扩展来运行 C/C++ 程序][9]
|
||||
|
||||
重新启动 Visual Studio Code 。现在,你能够使用下面方法中的其中一个来运行 C 代码:
|
||||
|
||||
* 使用快捷键 Ctrl+Alt+N 。
|
||||
* 按下 F1 ,接下来选择或输入 Run Code 。
|
||||
* 在文本编辑器中右键单击,从上下文菜单中单击 Run code 。
|
||||
|
||||
|
||||
|
||||
![右键单击程序文件,然后选择 Run Code][10]
|
||||
|
||||
当你运行这个 C 程序时,它将会被自动编译和运行。你可以在编辑器底部打开的终端中看到输出。还有比这更好的吗?
|
||||
|
||||
![程序输出显示在编辑器的底部][11]
|
||||
|
||||
你更喜欢哪一种方法?
|
||||
|
||||
在 Linux 命令行中运行一些 C 程序是没有问题的,但是使用一个代码编辑器会更容易一些,而且会节省时间。你不同意吗?
|
||||
|
||||
我让你来决定你想使用哪一种方法。
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://itsfoss.com/run-c-program-linux/
|
||||
|
||||
作者:[Abhishek Prakash][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[robsean](https://github.com/robsean)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://itsfoss.com/author/abhishek/
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://i0.wp.com/itsfoss.com/wp-content/uploads/2020/11/running-c-program-linux.png?resize=795%2C399&ssl=1
|
||||
[2]: https://i1.wp.com/itsfoss.com/wp-content/uploads/2020/11/Run-C-Program-Linux.png?resize=800%2C450&ssl=1
|
||||
[3]: https://gcc.gnu.org/
|
||||
[4]: https://itsfoss.com/c-plus-plus-ubuntu/
|
||||
[5]: https://code.visualstudio.com
|
||||
[6]: https://itsfoss.com/best-modern-open-source-code-editors-for-linux/
|
||||
[7]: https://itsfoss.com/install-visual-studio-code-ubuntu/
|
||||
[8]: https://i1.wp.com/itsfoss.com/wp-content/uploads/2020/11/c-program-visual-studio-code-linux.png?resize=800%2C441&ssl=1
|
||||
[9]: https://i2.wp.com/itsfoss.com/wp-content/uploads/2020/11/running-c-program-in-linux-with-visual-studio-code.png?resize=800%2C500&ssl=1
|
||||
[10]: https://i2.wp.com/itsfoss.com/wp-content/uploads/2020/11/running-c-program-in-linux-with-visual-studio-code.jpg?resize=800%2C500&ssl=1
|
||||
[11]: https://i0.wp.com/itsfoss.com/wp-content/uploads/2020/11/run-c-program-in-linux-with-visual-studio-code.jpg?resize=800%2C500&ssl=1
|
||||
Reference in New Issue
Block a user