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 Using GCC |
4) Now compile the code with below command.
gcc filename.c
C Programming Using GCC |
or You can add the gcc version number (works as same above) to compile.
gcc-9 filename.c
5) You will find a new file named a.out in the same folder path.
6) Execute this file as below
./a.out
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
-Use -wall option to enable all compiler warnings.
gcc -g -Wall filename.c
- Use -o option to change the default name (a.out) of output file to any desire name.
gcc g -Wall -o 'name' filename.c
- 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
-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.
- 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
clang -v filename.c
-Use -S to run only preprocess and compilation steps.
clang -S filename.c
C Programming Using CLANG |
- Use man option to learn more.
man clang or click here for all flags and options
Sharing is Caring 😊
0 Comments