Learn to program c on linux

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

Last updated October 7, 2021 By Abhishek Prakash 21 Comments

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:

Step 3: You run the generated object file to run your C program in Linux:

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.

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).

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

Switch to directory where you have kept your C program (or provide the path) and then generate the object file by compiling the program:

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:

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

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.

Читайте также:  Words english для windows

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 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.

You must ensure that you have gcc compiler installed on your Linux system.

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.

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.

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?

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.

Like what you read? Please share it with others.

Источник

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

Last updated February 18, 2021 By Abhishek Prakash 40 Comments

Brief: This tutorial teaches you to run C and C++ programs in Linux terminal. It also show the steps to setup a C++ development environment in Ubuntu Linux using Eclipse IDE.

I have been requested more than once about writing an easy to follow tutorial to run C++ program in Linux. In this guide, I’ll show you:

Process is pretty much similar to running C program in Linux.

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

Learn C++ for Free

If you are new to C++, join this FREE online course from Microsoft and learn C++ programming from the experts.

Prerequisite: Install build-essential

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

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

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

Once you have the build-essential 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 terminal.

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

You can save the program wherever you want.

Compile C++ code in 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 -o option, your are telling the compiler to generate the executable code in file swap. If you don’t do that, it will be defaulted to a.out file which is not a good programming practice.

Читайте также:  Bluetooth драйвера для windows mobile

Run C++ code in Linux terminal

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

This will run your code.

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 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:

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 in this tutorial.

Install Eclipse in Ubuntu based Linux distributions

For Ubuntu Linux, you can simply click on the link below to install Eclipse from Ubuntu Software Center.

Alternatively, you can install it using apt-get commands in the terminal:

Install Eclipse C++ Development Tooling (CDT) Plugin

Once you have it installed, it is time to prepare it for C++ development. By default, Eclipse is configured for Java development.

To configure it for C++ development, we need to install a plugin called C++ Development Tooling (CDT). To install CDT:

Step 1:

In the Eclipse menu, go to Help and then select Install New Software.

Step 2:

Next, click on the “Available Software Sites” link.

Step 3:

In the next step, search for CDT and check the box to select it for installation. Click OK afterward.

Step 4:

In here, select the newly added source from the drop down. It will now show you the option for C++ CDT. Just select C++ Development Tools here.

A few click on the Next button.

Accept the terms of course.

It will get the software from the repository and install it.

Once the installation is finished, you need to restart Eclipse.

Compile and run C++ program with Eclipse CDT

You’ll see the information about C++ Plugin at the next start.

You can now import or create C++ projects.

Once you have everything ready, you can compile the C++ project and run it:

That’s all you need to get started with C++ development in Ubuntu Linux. I hope you found this article useful. Questions and suggestions are welcomed.

Like what you read? Please share it with others.

Источник

Writing and Compiling C++ on Linux [A How-To Guide]

Powerful insights in business, technology, and software development.

If you’ve adopted Linux, chances are you might have done so for development purposes. After all, it has everything you need to program in most languages, and do so for (almost) free.

With Linux you can program in some of the most important languages on the planet, such as C++. In fact, with most distributions, there’s very little you have to do to start working on your first program. And what’s better, you can easily write and compile all from the command line.

If you’re a lone programmer or you work for a custom software development company like BairesDev, it should take you no time to get up to speed on programming with Linux as your platform of choice.

With that said, I want to guide you through the process of writing and compiling your first C++ program on Linux. I’ll demonstrate how this is done on both Ubuntu and Red Hat distributions.

Читайте также:  Графический драйвер для windows 10 64 bit последняя версия

What you’ll need

The only things you’ll need for this tutorial are:

A running instance of a Ubuntu- or Red Hat-based Linux distribution.

I’ll be demonstrating with the tried and true “Hello, World!”. This is an incredibly basic example, but it makes it easy for new users to follow. If you’re unfamiliar with it, all it does is print out the phrase “Hello, World!” on the screen.

Installing the necessary tools

Although there are some Linux distributions that come with everything you need to start developing (out of the box), you may come across one that doesn’t. Without the right tools, your bespoke software development experience will get frustrating fast.

So, how do you install the necessary software? Let’s do this on Ubuntu first. Open a terminal window on your desktop and issue the command:

In order to do this on Red Hat, you’ll use the dnf command with the groups options like so:

Either command will install everything you need to compile your first C++ application.

Writing the program

Now we need to write the “Hello, World!” program. Because this is a simple application, you can use the Nano editor. Open a terminal window and issue the command:

That command will create a new file, named hello.cpp, and open it for editing. In that empty file, paste the following text:

Save and close the file with the keyboard shortcut [Ctrl]+[X] and then type “y” (no quotes) to use the name we gave the file from the start.

At this point you now have your C++ file, hello.cpp, ready to compile.

Compiling the program

The next step is to compile our newly-written program. The command to do this is really quite simple. The basic command is:

That command will compile the program and create an executable file named a.out. Not very helpful, right? So instead of letting g++ name the executable, let’s give it the name hello, by using the output option (-o) with the command:

The above command will compile the hello.cpp file and create a new executable binary, named hello.

Running the new program

Now that you’ve used g++ to compile your program, it’s time to run it. Because this is a terminal-only application, you have to run it from within the terminal as a command. To do this, issue the command:

When you run the above command, you should see the output of the Hello, World! program (Figure 1).

The Hello, World! program output.

The reason why you have to run the program with the leading ./ characters, is because the program isn’t in your $PATH, which is a collection of directories wherein a command can be run globally. Because of this, you have to run the command from within the directory housing the hello binary.

Let’s say, however, you want to be able to run that Hello, World! application from within any directory on your Linux machine. If you want to do that, you must copy the binary file into a directory within your $PATH. To find out what directories are in your $PATH, issue the command:

This will list out every directory in your $PATH (Figure 2).

All of the directories in a user $PATH.

A safe bet is always /usr/local/bin. Copy that binary file with the command:

Now, all you have to do is issue the command hello to see the output of the Hello, World! program.

Conclusion

Writing and compiling your first program on Linux wasn’t nearly as hard as you assumed it would be, right?. From this very basic example, you can start programming and compiling more and more complex applications, until coding on Linux is second nature.

Источник

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