Compile с linux terminal

How to create, compile & run a C Program in Linux terminal

The C programming language is still alive because it is simple and can do a lot of things. As we know Turbo C compiler is a discontinued integrated development environment, well, on Linux you don’t need it as there is already GNU Compiler Collection to compile and run C or C++ programs. Therefore, if you know the C language, it is much easier to learn, write programs and run other programming languages ​​on Linux operating systems such as C ++, Java, Perl, or PHP, as the languages ​​have certain similarities. Here we will show the steps to install GCC compiler and how to write, compile and run a C program in Linux.

Now, what is a program?

Let’s say you want to train your cat or puppy how to sit or jump on your commands, then what language you will use. Obviously, your mother tongue whether it is English, German, Chinese, Hindi, or something else. We will not bark or meaau. In a similar way, if we want to tell our computer to perform some specific tasks such as calculations, we need to train it how to do that, with the help of a set of rules. But the problem is Computer only understands the binary language that is 0 or 1, thus we have created Programming languages to create programs that we can understand and after compiling our computer and later can execute the same. Thus, a program is a sequence of actions to achieve a goal.

Steps to write, run and compile C program in Linux

Here we are using Ubuntu 20.04 LTS, however, the steps given here are not only for it. You can also implement on older Ubuntu versions such as 18.04/16.04 including Linux Mint, Debian, Kali, CentOS, RedHat, Fedora Elementary, and more…

1. Install Compiler and other Dev tools

To write and execute a C program on Linux, we need a compiler that will compile the code we have written and give us an executable file for the same. Therefore, for that, if you are on Debian or Ubuntu then install build-essential, and on RHEL based distros go for Development Tools.

For RHEL/Fedora/CentOS

Run system update command, first:

dnf groupinstall 'Development Tools' or yum groupinstall 'Development Tools'

On Ubuntu or Debian systems

sudo apt-get update sudo apt-get install build-essential manpages-dev

You should have sudo or root user access to run the above commands…

Install development tools on Ubuntu and Redhat centos

2. Check GCC version

GCC is the open-source library, which is an acronym for GNU Compiler Collection available for Linux and acts as a compiling system for Program C and other various programming languages. However, it mainly used to compile C and C++ programs… Thus, after installing the development tools in the first steps, you will also get GCC on your system. To confirm and check its version run:

Читайте также:  Linux размер имени файла

To know only installed path

Check GCC compiler version

3. Open a Text editor on Ubuntu or RHEL

All Linux distros now come with graphical text editors, however, we can use the command terminal to create a text file to write C program codes as well using command line text editors such as nano, VIM, gedit, and more... Here we are using nano.

To install nano, if it is not on your system type:

RHEL/CentOS- sudo yum install nano

Ubuntu/Debian– sudo apt install nano

4. Write your first C Program in Linux terminal

Let’s create a demo C program in which we will include common C program library stdio.h to use various variables including functions for performing input and output. If you are learning C programming then you already familiar with the C libraries that we define in the header of the program to call various functions for performing various tasks.

For example, if you are writing a C program that mathematical functions then we need to declare math.h library, for graphics, we include graphics.h and so on…

Create a file:

Now, let’s add the following lines to create a simple C program that will give an output “Say hello to H2s” when we compile and execute it. To Save the file by pressing Ctrl+X, type Y, and then hit the Enter Key.

// my first demo C program #include int main()

The explanation for the above command- In the above command first we have added stdio.h library in the header and then int main(); in this syntax main() is the entry point of any C/C++ program, in short, it tells the compiler to start compiling from this point and int main() function will accept any number of arguments but it returns integer value usually zero. However, that is an old way, developers usually prefer int main(void) this represents a function that expects no arguments or when a program doesn’t require any initial parameters.

After that we use printf function to show or print a text ” Say hello to H2S” and to break the output line or to have a page break, we used \n (Escape Sequences). As we have used the main () function that generally supposed to return 0 value to confirm there is no failure in compiling and the task has been finished successfully, however, if it gets non-zero that means a failure. Nevertheless, declaring return 0; is not compulsory even without the program and the main () function will work in the same way.

Write C Program Linux Ubuntu

4. Compile with GCC

Now, let’s Compile our first C program, we have written. We use GCC compiler

In the above command, we are compiling the demo.c file or program using GCC and saving it in an executable format that is a demo. You can save it with some other name as well.

5. Run C program in Ubuntu Terminal

Let’s Run it. For that simply type:

In the above, example, we have compiled a C program demo.c and saved it an executable called demo. Thus, to run the same, we type:

Compile C Program on Linux using GCC Compiler

6. C program to add numbers in Linux

Let’s go a little step further and write a simple program in C language to add numbers. In it, we will ask to users add two numbers and the program perform the addition and print the result.

Читайте также:  Запуск приложения через командную строку linux

Create a new program, let’s say sum.

Add the following code in that and save the file using Ctrl+X, type Y, and then press Enter key.

Program C two number add program on Ubuntu Linux

The explanation for the above code:

With the help int which is an integer variable, we are declaring three variables num1, num2, and sum. The num1 and num2 variables will hold the two numbers that a user will enter to get a result that will store in the sum variable.

After that, we are printing a text to ask a user to enter two numbers that you want to add.

Then we will use scanf function to take inputs from the user in integer format using another variable %d or % i. %d specifies the type of variable as decimal and %i specifies the type as an integer.

Now, we will use + operator to add num1 and num2 and store the result of the same in sum variable:

Finally, print the result of the addition using printf function. Now, what happening here is, first %i read the value stored in num1 and second %i read the value stored in num2, and third %i will read the value stored in sum. \n is just to page break line. After that, it prints all of them together which appears to the frontend user a familiar way to get an answer to its addition query.

printf(«%i + %i = %i \n», num1, num2, sum);

Compile and run your code

C programming on Linux terminal using GCC compiler

Closing thoughts:

Well, this tutorial is just an introduction on how to start with the C program in Linux, however, to go further in deep you have to acquire more knowledge through books on how libraries, functions, variables, and operators work in it.

Источник

How to Compile and Run a C++ Program in a Linux Terminal

For many developers, Linux is an ideal environment to write C++ code because of its open-source nature and powerful compiler. However, compiling and running C++ programs on Linux could be intimidating for users who are not familiar with the procedure. Fortunately, with the right instructions and tools, it can be made simple. This tutorial aims to guide you through the process of compiling and running C++ programs on Linux, breaking it down into easy-to-follow stages.

How to Compile and Run a C++ Program in the Linux Terminal

Compiling C++ programs on Linux is a straightforward process that can be achieved using the GNU Compiler, also known as g++. This command-line program converts your high-level language code into an executable file. If you have some knowledge of C++ programming, our main focus is to teach you how to compile and run C++ programs in the terminal.

To compile a C++ program in a Linux terminal using a g++ compiler, follow the below-given steps:

Step 1: First create a cpp file using the nano editor and paste your C++ code in it:

Here I am using the following code as an example.

Save the file using “Ctrl+X”, add “Y” and enter to exit.

Step 2: To compile the program, navigate to the directory in which you saved the .cpp file and run the following command:

Essentially, the -o option specifies the name of the output file that the compiler will create.

Note: If your program includes mathematical functions:

Step 3: Now run the output file using the following command:

Читайте также:  Пакеты имеющие неудовлетворенные зависимости linux generic

This will run our code and gives you the output.

Conclusion

Compiling and running a C++ program in a Linux terminal is a simple procedure that may be achieved by utilizing a few easy commands. Users can quickly compile their code using the g++ command and run their program by executing the resulting binary file. However, they must first create a cpp file, add the C++ code to it and then run the file according to the above-given steps.

About the author

Komal Batool Batool

I am passionate to research technologies and new ideas and that has brought me here to write for the LinuxHint. My major focus is to write on programming languages and computer science related topics.

Источник

How to Run C/C++ Programs in Linux [Terminal & Eclipse]

I have been requested more than once to write an easy-to-follow tutorial to run C++ programs in Linux. In this guide, I’ll discuss:

The process is pretty much similar to running C program in Linux.

Do note that I am using Ubuntu Linux while writing this article but the same steps are valid for other Linux distributions based on Ubuntu, such as Linux Mint, elementary OS, etc.

Prerequisite: Install build-essential

If you want to do coding in Ubuntu Linux, you should install build-essential package. It consists of various software that you will need to compile programs, including gcc and g++ compilers.

You may install gcc on Ubuntu and other distributions separately as well but the build-essential has additional tools that you may need.

Normally, build-essential should already be installed on your system. But to make sure, run the command below:

sudo apt install build-essential

Run C++ programs in Ubuntu Linux

Method 1: Compile and run C++ program in Linux terminal

Once the build-essential is installed, you are ready to code in C++. I believe that you already know how to code in C++, even a little bit. Our main aim is to see how to compile and run C++ programs in the terminal.

Let’s take an example of the swap program which I wrote in a file named swap.cpp. The content of this file is the following:

Sample C++ program in Ubuntu Linux

You can save the program wherever you want.

Compile C++ code in the Linux terminal

To compile the program, go to the directory where you have saved the cpp file and use the command in the following format:

Basically, with the -o option, you are telling the compiler to generate the executable code in file swap. If you don’t do that, it will default to a.out file, which is not a good programming practice.

Run C++ code in the Linux terminal

Once you have compiled the code, you’ll get the executable file. You just need to run it in the following manner:

You can refer to this gif for a better demonstration of running a C++ program in Ubuntu Linux.

Method 2: Setup Eclipse for C++ programming in Ubuntu Linux

That was the basic way of running a C++ program in Linux. But if you are working on a C++ project, building and running individual files would be a nightmare.

This is where Integrated Development Environment (IDE) comes in picture. One can argue a lot about the best IDE for Linux, but if you ask for my advice, I’ll say go ahead with Eclipse. This is the best IDE for C++ development, in my opinion. Did I mention that it is also open source?

Recommended Read:

Источник

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