How to compile files in linux

How to Write and Run a C Program in Linux

Linux C Programming

C is one of the world’s oldest, most widely-used programming languages. It has been used to develop countless applications, from operating systems to embedded devices. Even today, many developers still rely on C for its flexibility and reliability as a programming language.

The C Programming Language

C is a highly portable language. This means that programs written in C are easily transferable from one computer system to another without having to be rewritten from scratch. This allows developers to quickly port their applications across different platforms without too much extra effort or hassle.

Also, C offers an outstanding balance between high-level abstractions and low-level programming techniques. In other words, you can use pre-defined functions and libraries that handle more complex tasks like memory management or string manipulation while still being able to access the machine’s hardware directly if needed. This makes it suitable for almost any application ranging from large commercial projects to smaller hobby projects!

C has been around for over 40 years, so plenty of resources are available online and offline that provide helpful tutorials and detailed documentation on how best to use the language. Whether you’re just starting with programming or already an experienced programmer looking for some tips – you should have no trouble finding what you need when looking into C development!

Finally, unlike many other modern programming languages, C isn’t tied to just one platform or software framework – it can be used with almost every major Operating System (OS), including Windows, macOS, Linux, and even Android! This universality makes it an extremely versatile option compared to other languages, which may only be ideal for certain OSes or environments.
C is truly one of the most significant programming languages ever created, and its popularity doesn’t seem like it will diminish anytime soon! With its flexibility, portability, and ease of use – it’s no wonder why so many people choose this powerful language when building their software projects!

Install C Compiler (GCC) on Linux

We have run the steps and commands mentioned in this article on an Ubuntu 22.04 LTS system, but it works the same on other versions like Ubuntu 20.04 or Debian 11.

To compile a simple C program, we use the Linux command-line tool, the terminal. To open the terminal, you can use the Ubuntu Dash or the key combination Ctrl+Alt+T.

Install the build-essential packages

In order to compile and execute a C program, you need to have the essential packages installed on your system. Enter the following command as root in your Linux Terminal:

$ sudo apt install build-essential

Install gcc compiler that is part of build-essential package with apt

You will be asked to enter the root password; the installation will begin after that. Please make sure that you are connected to the internet.

Читайте также:  Команда gzip on linux

Write a simple C program

After installing the essential packages, let us write a simple C program.

Open Ubuntu’s graphical Text Editor and write or copy the following sample program into it:

Then save the file with .c extension. In this example, I am naming my C program as sampleProgram.c

Example C program

Alternatively, you can write the C program through the Terminal in gedit as follows:

This will create a .c file to write and save a program.

Compile the C program with GCC Compiler

In your Terminal, enter the following command to make an executable version of the program you have written:

$ gcc [programName].c -o programName
$ gcc sampleProgram.c -o sampleProgram

Compile source code with gcc

Make sure your program is located in your Home folder. Otherwise, you will need to specify appropriate paths in this command.

Run the program

The final step is to run the compiled C program. Use the following syntax to do so:

Start our compiled C program

You can see how the program is executed in the above example, displaying the text we wrote to print through it.

This article taught you how to write, compile and run a simple C program in Linux. All you need is the essential packages and skills to make you a programming guru in Linux!

About This Site

Vitux.com aims to become a Linux compendium with lots of unique and up to date tutorials.

Latest Tutorials

Источник

How to Write, Compile and Run a C Program in Ubuntu and Other Linux Distributions [Beginner’s Tip]

Running C program in Linux command line is not that difficult. Running it in a code editor like Visual Studio Code is even easier. Learn both methods.

How do you program in C on Linux? It is indeed very easy and consists of three simple steps. Step 1: 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

Running a C Program in Linux using gcc

This was just a quick summary of how to compile and run a C program in Linux. If you are new to either C or Linux, I’ll show these steps in detail to make you comfortable coding C programs in a Linux environment. I’ll discuss how to run C programs in a Linux terminal and a code editor.

Method 1: How to run C programs in a 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). You can install gcc using your distribution package manager. In Debian and Ubuntu-based Linux distributions, use the apt command:

Switch to the 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 your object file is generated, run it to run the C program. It is already executable. Simply use it like this:

Читайте также:  Linux command and example

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.

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

C program editing in Visual Studio Code

Not everyone is comfortable with the 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 in this example. It’s a hugely popular open-source code editor from Microsoft. First thing first, install Visual Studio Code in Ubuntu from the software centre. For other distributions, please check your Linux distribution’s package manager or software centre. 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. You must ensure the gcc compiler is installed on your Linux system.

Next, you would want to use an extension that allows you to run the C code. Microsoft may prompt you for installing its own extension for the C/C++ programs but it is complicated to set up 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, search for ‘Code Runner,’ and install it. Installing Code Runner extension in Visual Studio CodeRestart Visual Studio Code. Now, you should be able to run the C code by using one of the following ways:

  • Using the shortcut Ctrl+Alt+N.
  • Press F1 and then select or type Run Code.
  • Right-click the text editor and click Run code from the context menu.

Right -click the program file that you want to run and then press

When you run the program, it is compiled automatically and then run. You can see the output in the 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 Visual Studio Code editor

Which method do you prefer?

If you prefer a video to learn about installing and running C programs in Linx, check out the below Video tutorial.

Источник

How to Compile a C++ file in Linux

In programming languages, writing code is the most basic skill in creating programs. There exist numerous languages have existed/evolved over time. Out of these languages, C++ is no doubt extremely commonly used in the world of programming. Unlike other languages, such as HTML and CSS as C++ must be compiled manually. The compilation takes the human-written code and turns it into a form that can be read and executed by the system.

Читайте также:  Установка разрешения экрана astra linux

In this article, knowledge will be provided on how you can compile a C++ file on your Linux operating system.

How to Compile a C++ File Linux?

There exist various methods through which a C++ file can be compiled and executed on the Linux operating system. As an example, we use the following C++ file in this article:

Now, let’s head over to the methods to compile the C++ file on Linux.

Method 1: Use the G++ Compiler

The “G++” compiler is the most commonly used compiler for both C and C++ files due to its simplicity. Mostly this compiler comes built into the Linux operating system, but if it is not present on your system, it can simply be installed using the command below:

Once the compiler is installed on your system, you can use this method, simply execute the command that is shown below:

The command above uses the “G++” keyword to compile the file whose name is mentioned at the end of the command, which is the “sample.cpp” file. This creates a compiled file with the name “a.out”. This compiled file can then be executed as shown below:

In the case that you want to compile the file with a different name than “a.out”, you can use the “-o” option as demonstrated below:

$ g++ -o compiled_file sample.cpp

Through this simple method, the C++ file can be easily compiled.

Method 2: Using the GCC Compiler

Much like the “G++” compiler that was demonstrated in the previous section, GCC is also another compiler, but this compiler is utilized for both C and C++ programs. If this compiler does not exist on your system by default, you can simply install it with the command shown below:

After its installation, you can utilize it by using the keyword “GCC” along with the name of the C++ file which in this case is the “sample.cpp” file. Furthermore, this compiler executes the “-lstdc++” option. This option signifies that the compiler will search the C++ library for its compilation.

The first command below compiles the program, and the “GCC” creates an executable file named “./a.out”. The second command executes the “./a.out” file:

$ gcc sample.cpp -lstdc++ $ ./a.out

Here you go! You have learned to compile a C++ file using the “GCC” and “G++” compilers.

These were the two most common methods that are used to compile a C++ file in Linux.

Conclusion

In Linux, the “GCC” and “G++” compilers can be used to compile a C++ program. The “G++” compiler takes the name of the file to be compiled, whereas the “GCC” compiler requires an additional option, “lstdc++” with the file name. This post has given a detailed explanation of each of these methods to compile a C++ program on Linux.

TUTORIALS ON LINUX, PROGRAMMING & TECHNOLOGY

Источник

Оцените статью
Adblock
detector