- How To Compile And Run a C/C++ Code In Linux
- Step #1: Install C/C++ compiler and related tools
- Step #2: Verify installation
- How to Compile and Run C/C++ program on Linux
- How do I compile the program on Linux?
- How do I run or execute the program called demo on Linux?
- Compiling and running a simple C++ program
- How do I generate symbolic information for gdb and warning messages?
- How do I generate optimized code on a Linux machine?
- How do I compile a C program that uses math functions?
- How do I compile a C++ program that uses Xlib graphics functions?
- How do I compile a program with multiple source files?
- Linux Programming Made Easy – A Complete Guide With Resources For Beginners
- Need Web Hosting?
- Linux kernel development
- What You Need to Know
- Development Skills
- Understanding Linux
- Process and Culture
- Developing Kernel Modules
- What You Need to Know
- Compare Popular Web Hosting Plans
- BlueHost
- Dreamhost
- SiteGround
- A2 Hosting
- GreenGeeks
- Hostinger
- Developing Applications For the Linux Operating System
- What You Need to Know
- Scripting in Linux
- What You Need to Know
- Bash Resources
- Python Resources
- Perl Resources
- Need Web Hosting?
- General Linux Resources
- Summary
How To Compile And Run a C/C++ Code In Linux
I am a new Linux user and student who used to write C or C++ programs on MS-Windows. Now, I am using Ubuntu Linux. How can I compile a C or C++ program on Linux operating systems using bash Terminal application?
To compile a C or C++ program on any Linux distro such as Ubuntu, Red Hat, Fedora, Debian and other Linux distro you need to install:
Tutorial details | |
---|---|
Difficulty level | Easy |
Root privileges | No |
Requirements | GNU C/C++ compiler |
Est. reading time | 2 minutes |
- GNU C and C++ compiler collection
- Development tools
- Development libraries
- IDE or text editor to write programs
Step #1: Install C/C++ compiler and related tools
If you are using Fedora, Red Hat, CentOS, or Scientific Linux, use the following yum command to install GNU c/c++ compiler:
# yum groupinstall ‘Development Tools’
If you are using Debian or Ubuntu Linux, type the following apt-get command to install GNU c/c++ compiler:
$ sudo apt-get update
$ sudo apt-get install build-essential manpages-dev
Step #2: Verify installation
Type the following command to display the version number and location of the compiler on Linux:
$ whereis gcc
$ which gcc
$ gcc —version
Sample outputs:
Fig. 01: GNU C/C++ compilers on Linux
How to Compile and Run C/C++ program on Linux
Create a file called demo.c using a text editor such as vi, emacs or joe:
How do I compile the program on Linux?
Use any one of the following syntax to compile the program called demo.c:
In this example, compile demo.c, enter:
If there is no error in your code or C program then the compiler will successfully create an executable file called demo in the current directory, otherwise you need fix the code. To verify this, type:
$ ls -l demo*
How do I run or execute the program called demo on Linux?
Simply type the the program name:
$ ./demo
OR
$ /path/to/demo
Samples session:
Animated gif 01: Compile and run C and C++ program demo
Compiling and running a simple C++ program
Create a program called demo2.C as follows:
To compile this program, enter:
To run this program, type:
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
How do I generate symbolic information for gdb and warning messages?
The syntax is as follows C compiler:
cc -g -Wall input.c -o executable
The syntax is as follows C++ compiler:
g++ -g -Wall input.C -o executable
How do I generate optimized code on a Linux machine?
The syntax is as follows C compiler:
cc -O input.c -o executable
The syntax is as follows C++ compiler:
g++ -O -Wall input.C -o executable
How do I compile a C program that uses math functions?
The syntax is as follows when need pass the -lm option with gcc to link with the math libraries:
cc myth1.c -o executable -lm
How do I compile a C++ program that uses Xlib graphics functions?
The syntax is as follows when need pass the -lX11 option with gcc to link with the Xlib libraries:
g++ fireworks.C -o executable -lX11
How do I compile a program with multiple source files?
The syntax is as follows if the source code is in several files (such as light.c, sky.c, fireworks.c):
cc light.c sky.c fireworks.c -o executable
C++ syntax is as follows if the source code is in several files:
g++ ac.C bc.C file3.C -o my-program-name
See gcc(1) Linux and Unix man page for more information.
🐧 Get the latest tutorials on Linux, Open Source & DevOps via
Category | List of Unix and Linux commands |
---|---|
Documentation | help • mandb • man • pinfo |
Disk space analyzers | df • duf • ncdu • pydf |
File Management | cat • cp • less • mkdir • more • tree |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Linux Desktop Apps | Skype • Spotify • VLC 3 |
Modern utilities | bat • exa |
Network Utilities | NetHogs • dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • glances • gtop • jobs • killall • kill • pidof • pstree • pwdx • time • vtop |
Searching | ag • grep • whereis • which |
Shell builtins | compgen • echo • printf |
Text processing | cut • rev |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
Comments on this entry are closed.
thank you so much ur solution gave a relief…
it made my gcc command to work
Very nice article…..
In Fig. 01, you did “whereis” twice. Shouldn’t it be “which” the second time? Thanks for the tut though. Big fan!
Another mistake, please change the following comment:
## assuming that executable-file-name.c exists ##
to
## assuming that program-source-code.c exists in the current directory ##
how to compile a program that use math functions and other things?
For the sake of supplying an example, let’s say you want to use the cosine function. This is supplied in the Linux math library. The cosine function is called ‘cos()’. Similarly, the sine function is called ‘sin()’.
First, to find information about how to use them, type “man cos” in a terminal session. This gives you the manual page for the cosine function. The output from ‘man’ may vary for your system, but it likely tells you three things: 1. first, include the math.h header, 2. cos() takes a ‘double’ as its argument and it returns a double as its output, 3. to build your program, tell the C compiler to include the math library (-lm).
Here’s a sample program that does all of this:
Love it!
Thank you. I have a trouble in doing step 1 and 2. But they are fixed.
thank u ,
need pdf of the commands guide to access the c/c++/java.
to compile and run a c++ program in ubuntu follow these simple steps:
1 open terminal window.
2 type “gedit” .
3 A gedit window will appear whereyou can write your program.
4 save your program as “filename.cpp” on desktop, “.cpp” is compulsory.
5 open terminal again and type “cd Desktop”.
6 In second line type “g++ filename.cpp”.
7 Type “./a.out”.
NOW YOUR WILL RUN.
very nice to your step.
thanks
Thanks! This article really helped me to find the GNU compiler in a Linux Operating System and showed me how to compile a C program.
dear sir,
what is the procedure to run .cpp program in linux distro debian 5 ?
just about to get around to learning c along with teaching my sons it. i had no idea where to start, the first page i checked is a bumper bonanza.
Источник
Linux Programming Made Easy – A Complete Guide With Resources For Beginners
Updated June 18, 2021
Disclosure: Your support helps keep the site running! We earn a referral fee for some of the services we recommend on this page. Learn more
Linux is an operating system, platform, ecosystem, and culture. While the continued dominance of Windows and Mac OS for desktop computing causes the uninitiated consumer to assume that Linux is a sort of fringe option for extreme geeks and those who don’t want to pay for an operating system, the truth of the matter is that Linux is the most used and most important operating system on the planet. It powers the web, it powers our infrastructure, it powers the largest supercomputers in the world.
Developing for Linux can be a bit of a challenge, but it can also be extremely rewarding. This short guide will provide you with an introduction to the information and resources you need to get started with Linux programming. It covers four areas of Linux development: contributing to the kernel, building new modules, developing applications for Linux, and Shell scripting.
Need Web Hosting?
If you’re in the market for a new web hosting provider, be sure to check out our user reviews, our A-Z hosting guide and our top three popular hosting picks:
Linux kernel development
The Linux kernel is, perhaps, the most ambitious software development project on the planet. New stable releases come out approximately every three months, and each release involves thousands of developers working in dozens of countries.
Getting involved with kernel development is, paradoxically, both easier than it seems like it should be, and also ridiculously difficult. It is easier than it seems like it should be, because there is no hiring process, no interview, no bureaucracy. Anyone who knows what they are doing is welcome to develop for the core, and submit patches. On the other hand, it is also extremely difficult because the kernel is extremely complicated. Moreover, the kernel development community, while quite welcoming in some ways, does not tolerate amateur shenanigans very well. While it helps to have thick skin, nothing really substitutes for actually knowing what you are doing. Developing the core is pretty serious business, involving pretty serious computer science. If you are just getting started with operating system development, this is not really the place to start.
What You Need to Know
Development Skills
Programming of the Linux kernel is done in C. Not C++, not Objective-C, not C#. So the first thing that you need to do is learn the C programming language extremely well.
You also need to have a deep understanding of operating system theory, particularly as it relates to the Linux system. More on that in the next section.
- The C Programming Language: the definitive guide to the language. Also available as a free PDF.
- Operating System Concepts: a thorough introduction to the theory and practice of operating system development.
Understanding Linux
Naturally, you need to understand Linux both as a user and as a developer. This includes having a fairly deep understanding of how the Linux kernel is structured, and how the various sub projects fit together.
Process and Culture
Finally, you need to understand how the Linux development process actually works. This includes getting a feel for the community as a whole, for its culture, and how the various members of the development community relate to each other. Also, it means understanding distributed version control, and the development release cycle.
- How to Participate in the Linux Community: required reading. Start here.
- Three Ways for Beginners to Contribute to the Linux Kernel: a short, friendly guide to getting involved.
- Official Things:
- The Linux Kernel Archives
- The Linux Kernel Mailing List
- The Kernel Bug Tracker
- Being a Moron on linux-kernel: a guide on how not to behave on the Linux Kernel (or any other) mailing list. (Our recommendation: lurk for at least six-months before posting anything.)
Developing Kernel Modules
Before jumping into core development on the Linux kernel, a good way to increase your knowledge and expertise with Linux programming is to work on a kernel module. These are independently developed pieces of software that work with the kernel in order to function as a complete operating system. Kernel modules include things like device drivers for various hardware peripheries, as well as file managers and other low level operating system features.
The barriers to entry for working on a kernel module are, generally speaking, much lower than they are for working on the Linux kernel. There are hundreds of modules, developed by many different teams and individuals, so there is not one set of gatekeepers setting the tone for development. Moreover, the stakes are a bit lower with module development.
What You Need to Know
Kernel modules, like the kernel itself, are usually written in C. (There is some fringe debate about developing kernel modules in C++, and there are some off-the-wall ways of accomplishing this, but it is certainly not the normal way nor is it recommended.)
Obviously, if you are writing a device driver for a piece of hardware, you will need to know quite a bit about the type of hardware, and the firmware embedded on it. You also need a decent understanding of the Linux-based kernel, and the way that it interacts with kernel modules. Finally, if you are contributing to an existing kernel module, you will need to learn about their procedures and development cycle.
Also see the list of resources above in the Kernel Development section.
Compare Popular Web Hosting Plans
We think the following plans will suit the majority of our visitor’s needs.
BlueHost
Dreamhost
SiteGround
A2 Hosting
GreenGeeks
Hostinger
Developing Applications For the Linux Operating System
Once we get up out of the weeds of operating system development, and start talking about developing actual applications for the Linux operating system, the job gets a little easier and we have a lot more options about how to proceed. Compilers and interpreters for just about every programming language are available for the Linux platform, often more than one for a particular language.
What You Need to Know
If you are used to developing for Windows or Mac OS, the biggest shock when developing for Linux is probably the wide variety of Linux environments. There are dozens of Linux distros, and every Linux user has the ability to change quite a lot about how their particular environment works. This means, among other things, that you have to pay attention to dependency management a lot more than you might otherwise need to.
Another difference, particularly as compared to a Windows system is that many Linux users prefer to compile their applications from source code. This happens on Mac OS as well but with less frequency. In the Linux world, a large number of users will always prefer to compile applications from source, rather than using an installer package. This might affect how you think about development and distribution of your software.
Most Linux-centric development takes place in C, C++, Perl, or Python. Learning those languages well, and diving into the resources mentioned above (Kernel and Modules) and below (scripting) will help a lot.
- C Programming in Linux: a thorough introduction and tutorial about programming Linux applications in the C language.
- Linux Developer Training: paid courses on Linux development, from the Linux Foundation.
Scripting in Linux
Perhaps the “lowest-level” of programming in Linux is shell scripting. However, this is no less “programming” than anything else. Shell scripting in Linux is a great way to automate routine tasks and accomplish more work in less time. Additionally, digging into advanced shell scripting will give you a deeper knowledge and understanding of the Linux operating system. Shell scripting is really what separates novice Linux consumers from advanced Linux users.
What You Need to Know
While there are other options, the most common scripting language is Bash. Even if you plan to move on to more advanced scripting languages, getting a handle on Bash will start you off on the right foot for all types of operating system scripting. Most people who really get into scripting as a way to boost productivity, use either Python or Perl for most of their work. Once you have hit a wall with what you can easily accomplish in Bash, you will probably want to move onto one of those languages.
Bash Resources
Python Resources
- Learn Python the Hard Way: one of the most popular introductions to Python development.
- Automate the Boring Stuff with Python: Practical Programming for Total Beginners: a great introduction to scripting (as opposed to “development”) in Python. Also available for free online as a video course.
Perl Resources
Need Web Hosting?
If you’re in the market for a new web hosting provider, be sure to check out our user reviews, our A-Z hosting guide and our top three popular hosting picks:
General Linux Resources
Linux.org: a central forum for all things Linux.
The Linux Documentation Project: a giant library of Linux guides on all of the subjects above, plus a bunch of other things. This is one of the most important places to go for in-depth Linux information.
Summary
That was a wild ride! From Linux kernel programming all the way down to shell scripting. There are endless ways to program the Linux operating system. With this guide, you should be well on your way.
Источник