Pages

How to Compile and Execute C program in Linux

Linux is very popular OS in these days mostly among programmers and other sectors. We discuss how to compile and execute C program in Linux environment. Linux provide default C compiler which is GCC (GNU Compiler Collection). We use this compiler to compile and execute our C program. For this, we have to follow the following steps:
  1. Write a simple C program using any text editor and save it.
  2. Compile the program with this command: gcc filename.c -o outputfilename
  3. Execute the program with the command: ./outputfilename
In step 2 gcc species a compiler we are going to use, filename.c is the sour code file, -o species the output file name and outputfilename is our output file name which is used to execute out program. We don't need to provide file extension for the output file name. In some cases we need to link to linker, in such cases we need to give extra parameter while compiling the program.
eg: gcc filename.c -o outputfilename -lm
Here -lm is extra parameter that tells the compiler to link to the linker.

Example:


File name with green color is an executable file we have created using gcc hello.c -o hello command. ./ is the command to access executable file in Linux.



@msucil

Phasellus facilisis convallis metus, ut imperdiet augue auctor nec. Duis at velit id augue lobortis porta. Sed varius, enim accumsan aliquam tincidunt, tortor urna vulputate quam, eget finibus urna est in augue.

No comments:

Post a Comment