Coding in linux terminal

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?

Читайте также:  Linux root and sudo

Recommended Read:

Источник

9 Best Text Editors for the Linux Command Line

Looking to edit files in the command line? These are the best editors for Linux terminal when you go all command line.

A text editor is a must-have application for any operating system. We have no dearth of good text editors on Linux. There is Gedit for quick text note down, there is VS Code for writing codes and more. But those are all GUI-based editors. What if you need to edit files in the terminal? Linux sysadmins need to do that on a daily basis. The average desktop Linux user may also need to edit files in the command line from time to time. Your distribution has a terminal-based text editor installed by default. But don’t restrict yourself to just that. Here, I have compiled a list of the best command line text editors for Linux. Feel free to experiment. Please note that this is not a ranking list.

1. Neovim

neovim text editor

Neovim is a fork of Vim that aims to add more extensibility while simplifying it. If you’re comfortable with Vim, you will be good to go using Neovim. The project is being actively maintained and the progress is promising so far. Of course, unless you’re acquainted with how Vim works, you may not notice the striking difference between the two. But, overall, Neovim tries to take Vim up a notch.

How to install it?

For other Linux distributions or platforms, you may refer to its official installation instructions to get started. To give you a head start, I must mention that when using the terminal, you will have to type the following to launch it (instead of neovim):

2. ne – The Nice Editor

ne the nice editor

When compared to the classic and popular text editors, ne (the nice editor) is a good alternative that tries to offer advanced functionalities and make it easier to use them. In other words, it’s a simpler alternative to Vim/Emacs offering you powerful features. It is being actively maintained. I tried it installing on Ubuntu 22.04 and it worked just fine. You can explore more about it on their GitHub page. Of course, unless you test it extensively, you should take it with a pinch of salt.

How to install it?

You should find it available in the official repositories of your Linux distribution. For Ubuntu-based distros, you can install it using the command:

You can also check out their official download page for more information on other Linux distributions.

3. Vim

vim text editor

If you’ve been on Linux for quite some time, you must have heard about Vim. Vim is an extensively configurable, cross-platform, and highly efficient text editor. It may not be suitable for newbies but it’s something every aspiring Linux System administrator should get comfortable with. You will probably find it pre-installed in your Linux distribution. It is extremely popular for its wide range of advanced features. Vim can be quite agonizing for first-time users. I remember the first time I tried to edit a text file with Vim, I was completely puzzled. I couldn’t type a single letter on it and the funny part is, I couldn’t even figure out how to close this thing. If you are going to use Vim, you must be determined to climb up a very steep learning curve. But after you have gone through all that, combed through its official documentation, and practice the commands/operations, you’ll find it worth all the time spent. Not to forget, you can use it for basic text editing or leverage its support for hundreds of programming languages, extensions, and file formats.

Читайте также:  Linux добавить группу wheel

How to install it?

If you don’t have it installed already, you can just try typing in the command (on Debian-based systems) to install it:

You can also find it listed in your software center of the Linux distribution you use. In either case, just head on to its official download page to get more details.

4. GNU Emacs

gnu emacs text editor

GNU Emacs is undoubtedly one of the oldest and versatile text editors out there. In case you didn’t know, it was created by GNU Project founder Richard Stallman. Emacs is cross-platform and has both command line and a graphical user interface. It is also very rich with various features and, most importantly, extensible. Just as Vim, Emacs too comes with a steep learning curve. But once you master it, you can completely leverage its power. Emacs can handle just about any type of text file. The interface is customizable to suit your workflow. It supports macro recording and shortcuts as well. The unique power of Emacs is that it can be transformed into something completely different from a text editor. There is a large collection of modules that can transform the application for use in completely different scenarios, like — calendar, news reader, word processor, etc. You can even play games in Emacs!

How to install it?

You should find it in your software center, or if you prefer using the terminal on Ubuntu-based distros, you can type in:

You can find more information on it on their official download page. Once you’re done installing, you need to type in a specific command to launch emacs in your terminal, which is:

Basically, this command instructs not to include any window to launch the program but the terminal itself.

5. MCEDIT

mcedit text editor

Midnight Commander is a fairly popular command line file manager and mcedit is an internal file editor of GNU Midnight Commander. It’s possible that many of us forget about this text editor, which is embedded inside the midnight commander utility. What this editor does, is open the files specified on the command line. The editor is based on the terminal version of cooledit – a standalone editor for X Window System.

How to install it?

6. Nano

nano text editor

When it comes to simplicity, Nano is the one. Unlike Vim or Emacs, it is suitable for beginners to get used to quickly. If you want to simply create & edit a text file, look no further. The shortcuts available on Nano are displayed at the bottom of the user interface. It is minimal and perfectly suitable for editing system & configuration files. For those who don’t need advanced features from a command-line text editor, Nano is the perfect pick. If interested, you can learn how to use Nano text editor in our beginner’s guide.

How to install it?

For the most part, Nano editor should come in pre-installed on Ubuntu-based distributions. If it isn’t there, you can simply visit the official download page to get the binaries for the distribution you want.

7. Tilde

tilde text editor

Tilde is a terminal-based text editor tailored for users who are normally used to GUI applications. Unlike other options mentioned in this list — this may not be a power tool. But, for basic text editing operations, this is very easy to use. You do have some advanced functionality – but that’s not something to compare with Vim/Emacs. If you wanted to try something easy-to-use and different, this is the one I’d recommend you to try.

Читайте также:  Test Page

How to install it?

For information on other Linux distributions, you may refer to their GitHub page or the download page to explore more about it.

8. Micro

micro text editor

Micro is an easy-to-use and highly customizable text editor. It also implements the universal copy/paste/save shortcuts (CTRL + C/V/S), which is rare in Linux terminal editors. Another cool offering from this text editor is its plugin system and internal command mode. Though it’s not feature-rich like Vim or other mature text editors, it can easily replace tools like Nano for occasional file editing in the terminal.

How to install it?

Micro is available in the repositories of all major distributions. In Ubuntu, you can install it with:

For other Linux systems, you can check out their GitHub page. We have an article on Micro text editor with a free cheat sheet for beginners if you are interested.

9. Helix

helix editor

Helix editor is a rust-based terminal text editor, which is both fast and efficient in resources. It uses Tree-Sitter for syntax highlighting, which helps boost the speed. The built-in language server support provides context-aware completion, diagnostics, and code actions. Helix editor offers several powerful tools, that can make it work like other editors like Vim and Emacs. On their website, they call themselves, a post-modern text editor, if Neovim is the modern Vim, then Helix is post-modern. For those who are interested, you can check out a dedicated Helix article

How to install it?

For Arch and its derivatives, there is a package available in the Community repository. Also, an AUR package is also available, which builds the master branch. For other Linux distributions, you have to use Cargo, the Rust Package Manager. You should be able to install Cargo using your distribution package manager Also, you need to make sure that Git is installed. On Ubuntu-based distributions, install both like this:

sudo apt install git cargo
git clone https://github.com/helix-editor/helix cd helix cargo install --path helix-term

Helix also needs its runtime files so make sure to copy/symlink the runtime/ directory into the config directory. This location can be overridden via the HELIX_RUNTIME environment variable.

ln -s $PWD/runtime ~/.config/helix/runtime
export PATH=$PATH:/home/team/.cargo/bin

Wrapping Up

If you are an experienced Linux user, you must have heard of most of the options mentioned in this list, if not all. For most users, going with Nano, Vim or Emacs should be good enough. The seasoned terminal dwellers could experiment with the likes of Micro, Helix or some other terminal text editors. That’s just my recommendation. And I would like to know yours. Which text editor do you prefer in the command line? Did you find a new, interesting one on this list? Share it in the comments.

DuckDuckGo hidden features

Hidden Features! 25 Fun Things You Can Do With DuckDuckGo Search Engine

What is TTY in Linux

What is TTY in Linux?

s-tui is used for CPU utilization monitoring

Beautifully Monitor CPU Utilization in Linux Terminal With Stress Terminal UI

Access Grub in VM

How to Access the GRUB Menu in Virtual Machine

FOSS Weekly 23.28

FOSS Weekly #23.28: China’s Linux OS, Linux Exit Codes, Btrfs Origins and More

cp command in Linux

Using cp Command in Linux

Become a Better Linux User

With the FOSS Weekly Newsletter, you learn useful Linux tips, discover applications, explore new distros and stay updated with the latest from Linux world

Источник

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