How To Compose, Compile and Execute C / C++ program in Ubuntu

Author: | Posted in Linux, Softwares, Tutorials No comments

“C Language , Dennis Ritchie’s valuable gift to the tech world and also a powerful language still. it is the base for the most of the nowadays popular operating systems. Even some of you may not know about Ritchie but every one will come across the word ” C language “. Most of the programming beginners will make their first step on “C”, this is the epic phrase. Well we move to the next thing the open source and popular Operating operating system “Ubuntu”. It also have some link with C language. Some may familiar with windows operating system will make the C programming with the help of the installed C package.  If this stops you to switch from windows to Ubuntu or any Linux Distribution. Here is the solution.

C in Ubuntu

Yes, mates you can easily compose, compile and run the C program much easier in Ubuntu Operating System.

As Ubuntu is open source it available for free of cost , similarly lots of tools , compilers,  IDE , Debuggers for Ubuntu is also available as open source and free. GCC- the best and greater C compiler available  for Ubuntu. Linux use this compiler for its kernel building. Here we cover following compilers,

 

Compose a C program in Ubuntu

Its very simple to compose a C program in Ubuntu. Open a normal text editor and write your C program. Finally save that written program with the apt file name with the extension “.c”  ( It indicates that the written code is in C Language). Thus the composing finished.

C programming in Ubuntu

Compile and Execute the C program in Ubuntu

Just now we finished the process of writing C program as you all know next is the compilation and execution of the C program in Ubuntu.

Note :Compiler will not automatically detect the location of the given “C file” So navigate to the Local Folder in the Terminal using cd command.

Ubuntu Command to Compile the C program

gcc -Wall [filename.c] -o [output-filename]

Compiling C with warning ubuntu

-Wall -> Display all the warnings in the compiled C program

-o  -> indicates the output name of the Executable file compiled

Ubuntu Command to Run the C Program

./[output-filename]

runnung C in ubuntu

Finally you got the out put of the written C program successfully.

Popular GCC Compiler Command Line Options

GCC Syntax

gcc [options] [source files] [object files] [-o output file]

Here is the list of most frequently used command line option for the GCC Compiler you may try

gcc -c                                    compile source files to object files without linking
gcc -Dname[=value]     define a preprocessor macro
gcc -fPIC                             generate position independent code for shared libraries
gcc -glevel                          generate debug information to be used by GDB
gcc -Idir                              add include directory of header files
gcc -llib                               link with library file
gcc -Ldir                             look in directory for library files
gcc -o output file           write build output to output file
gcc -Olevel                       optimize for code size and execution time
gcc -shared                      generate shared object file for shared library
gcc -Uname                      undefine a preprocessor macro
gcc -w                                  disable all warning messages
gcc -Wall                          enable all warning messages
gcc -Wextra                    enable extra warning messages

Compose compile and Execute C++ Program in Ubuntu

Composing is as similar to that of the C programming just type your c++ program and save it with the extension “.cpp”

 

Compiling C++ program in Ubuntu

Before get into the compilation process you need the C++ compiler. Make sure that you have already installed “g++” compiler which is used to compile the C++ program. if not then install the “G++” Compiler using the below mentioned command in the terminal.

sudo apt-get install g++

G plus plus compiler in ubuntu

To Compile : g++ -Wall [filename.cpp] -o [output-filename]

To Execute : ./[output-filename]

Install the required compiler and start your first C/C++ programming in Ubuntu. Hope you enjoyed reading, share it with your friends over the social networks. If you have any doubts kindly drop it in comments section.

If you like this post, Share it to your friends. Dont forget to Subscribe our Feeds, Follow us on Twitter, Facebook and Pinterest.

Add Your Comment

*