Zwe Hlaing Bwa

စိတ္ကူးတည့္ရာေရးထားေသာ ဘေလာ့ခ္

Archive for April, 2012

How to Install C and C++ Compilers in Ubuntu and testing your first C and C++ Program

Posted by Zwe on April 22, 2012


If you are a developer you need C and C++ Compiler for your development work.In ubuntu you can install the build-essential for C and C++ compilers.

1. Install the build-essential package by typing the following command in the terminal: sudo 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.
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.
6. If you have a C++ program simply replace gcc with g++ and hello.c with hello.cpp. The options do the same things.
7. If you get a permissions error, you need to make the file executable. You can do this with chmod +x hello.cpp
8. Now type in the terminal ./hello and the program will run.

This will install all the required packages for C and C++ compilers

Testing C and C++ Programs

Compiling Your first C Programs

Now you need to open first.c file

sudo gedit first.c

add the following lines save and exit the file

Firstly compile the code using the following command

cc -c first.c

that would produce an object file you may need to add to the library.

then create an executable using the following command

cc -o first first.c

Now run this executable using the following command

./first

Output should show as follows

Hello, world

Compiling your first C++ program

If you want to run c++ program follow this procedure

g++ is the compiler that you must use.

you should use a .cpp file extension rather than a .c one

You need to create a file

sudo gedit first.cpp

add the following lines save and exit the file

Run your C++ Program using the following command

g++ first.cpp -o test

./test

Output should show as follows

Hello World!

Advertisements

Posted in Cplusplus | 3 Comments »

Compiling C++

Posted by Zwe on April 21, 2012


to get runtime initialization, you have to compile with

c++ testfile.cpp

which shoul result with a.out in the current directory. You can start it with

./a.out

Resulting file can be renamed automatically, using

c++ testfile.cpp -o whatever

and started with

./whatever

Compiling C++ on a Mac

Though the Mac isn’t officially supported at CSU, there’s probably and IDE that you can download for your Mac that looks very similar to the one we use in class. Still, one of the simplest ways to compile your program is using the program g++. You will do this using the command line (terminal), which is usually found under the ‘Applications’ folder, and then the ‘Utilities’ folder. Note, you may want to drag this onto your program bar at the bottom of the screen for future access. Typically, you will do the following steps:

Open up the terminal, as well as some kind of editor (like word pad). You can alternatively do this from the command line, but use whatever you’re comfortable with.
Edit/change your program (for example, my.cpp).
Save your changes, remembering the folder that you put it in. For simplicity, you may want to create a CSCI1301 folder directly in your hard drive (root directory)
From the terminal, you will make sure you’re in the same directory as your .cpp file, then type g++ -o myprogram my.cpp
The step above compiles my.cpp (source code) into an executable called ‘myprogram’. You can then run it by typing ./myprogram
Go to step 2 until your program works.
Note: if you’re not a Unix guru, don’t sweat it. You can change directories by typing ‘cd’ and then the directory you want to go to. If you’ve set it up correctly (by creating the csci1301 folder), before you compile the first time, you can type cd /csci1301 and you will be in the right directory.

You may want to check out the Mac site for a program called Eclipse. It seems to be pretty powerful, and I think it’s free.

Posted in Cplusplus | 2 Comments »