- How to Write, Compile and Run a C Program in Ubuntu and Other Linux Distributions [Beginner’s Tip]
- Method 1: How to run C programs in Linux terminal
- Method 2: How to run C programs in Linux using a code editor like Visual Studio Code
- 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?
- How to Write and Run a C Program in Linux
- Step 1: Install the build-essential packages
- Step 2: Write a simple C program
- Step 3: Compile the C program with gcc Compiler
- Step 4: Run the program
- Karim Buzdar
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.
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 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.
Источник
How to Write and Run a C Program in Linux
Linux is becoming programming heaven for developers, being an open-source and free operating system. Turbo C compiler is already an old approach to compile programs so let us programmers move to Linux for a new programming environment. In this article, we will explain how to write, compile, and run a simple C program. This will serve as a basis for you to move to more complicated and useful C programs that you can write and execute on Linux.
We have run the steps and commands mentioned in this article on a Ubuntu 20.04 LTS system but it will work on other versions like Ubuntu 18.04 or distributions like Debian 10 in the exact same way.
We will be using the Linux command-line tool, the Terminal, in order to compile a simple C program. To open the Terminal, you can use the Ubuntu Dash or the Ctrl+Alt+T shortcut.
Step 1: 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:
You will be asked to enter the password for root; the installation process will begin after that. Please make sure that you are connected to the internet.
Step 2: 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
Alternatively, you can write the C program through the Terminal in gedit as follows:
This will create a .c file where you can write and save a program.
Step 3: Compile the C program with gcc Compiler
In your Terminal, enter the following command in order to make an executable version of the program you have written:
Make sure your program is located in your Home folder. Otherwise, you will need to specify appropriate paths in this command.
Step 4: Run the program
The final step is to run the compiled C program. Use the following syntax to do so:
You can see how the program is executed in the above example, displaying the text we wrote to print through it.
Through this article, you have learned how to write, compile and run a simple C program in Linux. All you need is the essential packages and the right skills to make you a programming guru in Linux!
Karim Buzdar
About the Author: Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. You can reach Karim on LinkedIn
Источник