Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write, compile and run a C program on Ubuntu/Debian Linux

2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article is about how to write, compile and run a C program on Ubuntu/Debian Linux. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Step 1: write your C program and save it with a .c extension. For example, my_program.c.

Step 2: use the gcc compiler in a terminal to compile the program and generate the object file, like this:

Gcc-o my_program my_program.c

Step 3: in Linux, you can run your C program by running the generated object file:

. / my_program

This is just a brief summary of how to compile and run C programs in Linux. Assuming you are new to the C language or Linux system, I will demonstrate these steps carefully so that you can write C programs comfortably in the Linux environment.

In fact, I'll discuss two ways to run a C program in a Linux terminal and in a code editor.

Method 1: run the C program in the Linux terminal

To run a C program in Linux, you need to have a C compiler on your system. The most popular compiler is gcc (GNU compiler suite GNU Compiler Collection).

You can use your distribution's package manager to install gcc. In Debian and Ubuntu-based Linux distributions, use the apt command:

Sudo apt install gcc

Change to the directory where your C program is saved (or provide a path), and then generate the object file through the compiler:

Gcc-o my_program my_program.c

Remember that providing the output object file (- o my_program) is optional. If you do not provide it, an object file named a.out will be automatically generated. But this is not good, because compiling every C program will overwrite it, and you don't know which program the a.out object file belongs to.

After your object file is generated, run it to run the C program. It can already be executed. Simply use it like this:

. / my_program

Next, if your program is correct, it will show the output you expect. As you can see, this is no different from running C++ programs in Linux.

Every time you change your program, you must first recompile it, and then run the resulting object file again to run the new C program.

Method 2: how to use a code editor such as VSCode to run a C program in Linux

I fully understand that not everyone can adapt to the command line and terminals.

You can use a real Cmax Cure + IDE such as Eclipse or Code Blocks, but they are very heavyweight programs and are usually more suitable for large projects.

I recommend using an open source code editor like VSCode or Atom. They are basically text editors, but you can compile and run programs directly in a graphical code editor by installing add-ons.

In this example, I use the VSCode editor. It is a very popular open source code editor from Microsoft.

First, install VSCode in the software center of Ubuntu. For other distributions, please check the package manager or software center of your Linux distribution. You can check its official website for more information.

Start VSCode, open or create a project, and create your C program here. I use a simple Hello World program as an example.

You must make sure that you have installed the gcc compiler on your Linux system.

Sudo apt install gcc

The next thing you need to do is to use an extension that allows you to run C code. Microsoft may prompt you to install its CumberCraft + extension, but its setup is complex, so I don't recommend it.

Instead, I suggest you use the Code Runner extension. It is a simple and straightforward extension that allows you to easily run C and C++ code without additional configuration.

Go to the extension tab, where you will search for and install "Code Runner".

Install the Code Runner extension to run the Candlestick + program

Restart VSCode. Now, you can run the C code using one of the following methods:

Use the shortcut key Ctrl+Alt+N.

Press F1 and then select or enter "Run Code".

Right-click in the text editor and click Run code from the context menu.

Right-click the program file and select Run Code

When you run this C program, it will be compiled and run automatically. You can see the output in the terminal that opens at the bottom of the editor. Is there anything better than this?

The program output is displayed at the bottom of the editor

Thank you for reading! On "how to write, compile and run a C program on Ubuntu/Debian Linux" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, you can share it out for more people to see it!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report