Sunday, April 15, 2012

How to Compile a C Program Using the GNU Compiler (GCC)




  1. In order to see if you have the GNU C/C++ compiler installed on your system
    • Type/Copy/Paste: gcc --version
    • If the command is not found, it is likely that gcc/g++ isn't installed.
  2. 3
    Install gcc/g++ on your system, consult your Unix or GNU/Linux distribution documentation for the appropriate installation method. If you are using Microsoft Windows, please see the following installation method used to install MinGW GNU C/C++ compiler for Microsoft Windows How to install MinGW( Minimalist GNU C/C++ Compiler) on Microsoft Windows
  3. 4
    First, create a working directory using the following command:
    • Type/Copy/Paste: mkdir CCPP
  4. 5
    Second, change into the directory
    • Type/Copy/Paste: cd CCPP
  5. 6
    Third, use a text editor such as nano, gedit, vi, notepad,(on Microsoft Windows ) for example,
  6. 7
    Unix/GNU/Linux Instructions:
    • Type/Copy/Paste: nano main.c
    • Type/Copy/Paste: gedit main.c
    • Type/Copy/Paste: vi main.c
  7. 8
    Windows Instructions:
    • Type/Copy/Paste: notepad main.c
    • You can use notepad as a text editor on Microsoft Windows
  8. 9
    Enter the following source code below:
  9. 10
    Type/Copy/Paste:
    • #include
    • #include
    • int main()
    • {
    • printf("Hello World\n");
    • return(0);
    • }
  10. 11
    Once this task is complete save the source code as a text file in your editor asmain.c
  11. 12
    Fourth, compile the file by issuing the following command
    • Type/Copy/Paste:
    • gcc main.c -o HelloWorld
  12. 13
    Fifth, run the command by issuing the following command
  13. 14
    Unix/GNU/Linux instructions:
    • Type/Copy/Paste: ./HelloWorld
  14. 15
    Microsoft Windows Instructions:
    • Type/Copy/Paste: HelloWorld.exe
    • On Windows when the HelloWorld.c file is compiled it will usually have an .exe extention.
  15. 16
    To compile multiple files:
    • Type/Copy/Paste:
    • gcc -o outputfile file1.c file2.c file3.c
  16. 17
    To compile with more error checking using the -Wall tag:
    • Type/Copy/Paste:
    • gcc -Wall -o outputfile file1.c
  17. 18
    To compile files without linking them:
    • Type/Copy/Paste:
    • gcc -c file1.c file2.c file3.c
    • After the last step, if you want to link them, type: gcc -o outputfile file1.o file2.o file3.o


Thanks to:

No comments:

Post a Comment