Results 1 to 2 of 2

Thread: How to compile and run your .c / .cpp files

  1. #1
    Join Date
    Aug 2011
    Tehran, Iran
    Ubuntu 11.04 Natty Narwhal

    How to compile and run your .c / .cpp files

    I'm just a newbie in Ubuntu and I put my time searching for a way on how to compile and run .c/.cpp files in terminal and I'd like to share what I got with you. If you have an IDE, you're almost done, you can easily code your program and execute it via specified IDE but what if you don't have? You can just download Code::Blocks which is a light-weight IDE and compile/run your desired file or you can follow the following steps to simply compile and run it through your terminal

    1. Installing the build-essential package
    You can easily install build-essential by typing the following command in your terminal (Ctrl + Alt + T):
    sudo apt-get install build-essential
    2. Installing g++
    You need g++ compiler to compile and run your source code, for getting it, simply type the following command in your terminal:
    sudo apt-get install gcc g++
    3. Source code
    Create a file with .c extension for a C program or .cpp if it's a C++ program.

    4. Directory
    Now go to the directory in which you saved your file using cd command. (e.g. cd Desktop)

    5. C program compiling
    I assume your filename is test.c if you have saved it as another name, you can simply change the command with the correct filename. For compiling and running, simply type the following command in terminal:
    gcc test.c -o test
    6. C++ program compiling
    If your file is a .cpp, simply change the previous command to the following one:
    g++ test.cpp -o test
    7. Permission
    If you got a permission error, you need to change file to an executable one. For doing such, simply type the following command in terminal:
    chmod +x test.cpp
    8. Running program
    Now that all is done, you can run your source code by typing the following command in terminal:
    Note : If you don't use -o option while compiling, the name of the executable file will be a.out by default.
    Last edited by Sepehr_M; August 31st, 2011 at 12:11 PM.

  2. #2
    Join Date
    Nov 2009
    Ubuntu 10.04 Lucid Lynx

    Re: How to compile and run your .c / .cpp files

    This is nothing new and probably there are already hundreds of tutorials for doing this, but still, kudos for making it clear and elaborate. Definitely helpful if you're just starting out.
    Personally, I don't think there's intelligent life on other planets. Why should other planets be any different from this one?

    -Bob Monkhouse


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts