C , C++, C-Objective Programming On Linux CLI Using GCC & CLANG Compilers

Please Read This 👇🏾

 


Hi Guys, In this post I will show you that how you can do the C programming on bash shell without using any IDE tool.For this you just have to install some compilation tool in your system. The most popular , handy, powerful & rich featured compilers are mentioned below. May be they are  preinstalled within your Linux distro or not. We will see how to install & use them with some basic options.

So lets start...

(I) GCC Compiler

1) Install the GCC compiler for your system (if not) or upgrade it with below command.

sudo apt-get update && sudo apt-get upgrade

2) Open any  editor to write a code.

3) Type your program (for test write a simple Hello World program) & save the file (using esc + :wq!) . save file with extension .c for c programming.

C Programming With GCC On BASH
C Programming Using GCC


4) Now compile the code with below command.

gcc filename.c

C Programming With GCC On BASH
C Programming Using GCC

or You can add the gcc version number (works as same above) to compile.

gcc-9 filename.c

C Programming With GCC On BASH
C Programming Using GCC

5) You will find  a new file named a.out in the same folder path.

6) Execute this file as below

./a.out

C Programming With GCC On BASH
C Programming Using GCC

Yea!!! You have successfully run your first hello world C program using bash.


Option For GCC compile

- Use -g flag to see the debug information. See below examples.

gcc -g filename.c

option flags in gcc commands
C Programming Using GCC


-Use -wall option to enable all compiler warnings.

gcc -g -Wall filename.c


options and flags in GCC command
C Programming Using GCC

- Use -o option to change the default name (a.out) of output file to any desire name.

gcc  g -Wall -o 'name' filename.c

gcc options and flags in C programming
C Programming Using GCC

- Use -lm option to link math library (use function like sqrt,cos,sine)

gcc -g -wall -o name filename.c -lm

- Use man option to learn more.

man gcc

(II) CLANG

CLANG is another advance, powerful and popular compiler for Linux. It support C, C++ and C-Objective languages.See below how to install & use it.

-To install clang in your Linux system run below commands.

sudo apt-get update && sudo apt-get upgrade

sudo apt-get install clang

-To compile the source file.

clang filename.c

clang options and flags
C Programming Using CLANG


-An output a.out file will generate in same folder. To run the compiled file run below command

./a.out

Flags / Option with CLANG

- Use -o option to change the default compiled name (a.out) to desire name.

clang -o name filename.c

clang options and flags
C Programming Using CLANG


- Use -w to disable all diagnostics/warnings

clang -w filename.c

- Use -x <language> to treat subsequent input files as having type <language>

clang -x c filename.c

C Programming Using CLANG

-Use -v to see verbose output.

clang -v filename.c

C Programming Using CLANG

-Use -S to run only preprocess and compilation steps.

clang -S filename.c

clang option for C programming linux
C Programming Using CLANG

- Use man option to learn more.

man clang  or click here for all flags and options



Sharing is Caring 😊



Post a Comment

0 Comments