Wednesday, November 16, 2011

Running C / C++ in Ubuntu


  1. Install the build-essential package by typing the following command in the terminalsudo apt-get install build-essential
  2. Now create a file that has the extension .c (if you plan to write a C program) or .cpp (for a C++ program).
  3. Write the code in that file.
  4. Now open a terminal and go to the place where you saved that file using the cd command (e.g. cd Desktop).
  5. If you have a C program type in the terminal 
    • gcc -Wall -W -Werror hello.c -o hello.c
    • The first line will invoke the GNU C compiler to compile the file hello.c and output (-o) it to an executable called hello.
    • The options -Wall -W and -Werror instruct the compiler to check for warnings.
    • If you have a C++ program simply replace gcc with g++ and hello.c with hello.cpp. The options do the same things.
    • If you get a permissions error, you need to make the file executable. You can do this with chmod +x hello.cpp
    • Now type in the terminal ./hello and the program will run.

    No comments:

    Post a Comment