Saturday, May 27, 2017

How to compile C program in Linux

Although it is super easy to print "Hello World!" in c program, you might wondering if you've switched to linux first time.
Here is the step by step process to compile and run a c program in linux.

1. Open terminal: (CTRL+ALT+T)

Terminal in Ubuntu 14.04 LTS

2. Change directory to desired location: cd DIRECTORY-NAME

3. Open your file in any text editor: Here I am using vim editor.

4. Vim will look something like this

5. Press "i" for go to insert mode

6. Type a "C" program

7. Press "escape" button then ":wq": Here escape key is used for go to command mode, :w for save, and q for quit. For more about vim commands you can visit here.

8. Now its time to compile the program with GNU compiler: Type "gcc" followed by C program you just typed. Hit enter. If there would by any error in your program it will be displayed. Go back to step 3 and fix them.

9. If a new line displayed without any message then it means program has been compiled successfully.

10. Good news, now its time to run your successfully compiled C program: Press "./a.out" and hit (or politely press) enter button.

11. Congratulations, You've just learned to compile and learn C program in linux.

Now you know how to compile and run a c program in linux.
Share if you feel others who are still programming with windows should also know this.

No comments:

Post a Comment