faqts : Computers : Operating Systems : Linux

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

85 of 107 people (79%) answered Yes
Recently 8 of 10 people (80%) answered Yes

Entry

C++: Compile: Operating system: Linux: How to compile C or C++ program on Linux? [run / hello world]

May 13th, 2005 02:43
Knud van Eeden,


----------------------------------------------------------------------
--- Knud van Eeden --- 22 November 2003 - 06:01 pm -------------------

C++: Compile: Operating system: Linux: How to compile C or C++ program 
on Linux? [run / hello world]

---

Steps: Overview:

 1. Create your program

 2. Compile your program

 3. Run your program

---

Steps: Worked out:

After making sure you have the C++ compiler installed on your computer
(so that e.g. 'cc' is a recognized command):

 1. Create your program

    1. Open a console

    2. use your favorite text editor (e.g. vi or emacs):

       1. Using text editor Vi, type:

           vi <your program source filename>

            e.g.

             vi ddd.c

           1. press <INSERT> to start editing

           2. to save your file when finished

              1. press <ESCAPE>

              2. followed by typing

                  :wq

                 to save and quit your file in the
                 current directory

       2. Using text editor Emacs, type:

         emacs <your program source filename>

          e.g.

           emacs ddd.c

         1. to save your file in the current directory and quit your
            file, type simultaneously:

            <CTRL><X><C>

            followed by

             y

    3. type some source code, e.g. :

        #include <stdio.h>

         main() {

         printf( "hello world" );

        }

 2. Compile your program

    1. On the command line type the command:

        cc <your program source filename>

         e.g.

          cc ddd.c

    2. this will create a program with the name

          a.out

 3. Run your program

    1. This by changing the execute bit

       1. use e.g. the chmod command

           chmod 750 <your output filename>

           e.g.

            chmod 750 a.out

    -- or --

    2. by changing the execute bit, and after that putting this file in
       the appropriate bin directory will allow you to only
       type the filename (instead of the full path)

       1. use e.g. the move or copy command

           mv <your output filename> <your bin directory>

            e.g.

             mv a.out /bin

    3. finally run your program, by typing on the command line:

         <the full path to your filename>

        ---

        e.g.

         /home/a.out

        ---

        e.g. if the file is in your current directory type:

         ./<your output filename>

          e.g.

           ./a.out

         ---

         Note: here './' means alway in the current directory
         (compare this with '../' which means in the
         directory above, '.../', which means 2 directories
         above, and so on)

    4. You will then see on the command line

        hello world

---
---

Tested successfully on

 Linux Debian v3 (using the 'vi' editor)

 Linux Mandrake v9 (using the 'vi' editor)

 Linux Red Hat v9 (using the 'emacs' editor)

 Linux Slackware v9 (using the 'vi' editor)

 Unix FreeBSD v4.8 (using the 'vi' editor)

---

not working (cc was not installed) on:

 Linux SuSE v9 (and using the 'vi' editor, error cc not installed)

 Linux TurboLinux v8 (and using the 'vi' editor, error cc not 
installed)

 Unix Sun Solaris v9 (using the 'vi' editor, cc error 'language 
optional software package not installed')

---
---

Book: see also:

---

[book: Brown, P. J. - begin with Unix - ISBN: 90-6789-026-X - p. 208]

---
---

Internet: see also:

---

Linux: File: Permission: Set: How to set your read/write/execute 
permissions? [chmod / run]
http://www.faqts.com/knowledge_base/view.phtml/aid/24443/fid/107

---

Operating system: Linux: Editor: VI: File: Overview: Can you give an 
overview of the VI editor?
http://www.faqts.com/knowledge_base/view.phtml/aid/29029/fid/1561

---

Operating system: Linux: File: Execute: Run: How to run an executable 
program in Linux?
http://www.faqts.com/knowledge_base/view.phtml/aid/29086/fid/107

----------------------------------------------------------------------