CSCI 151 – section 04

Compiling Your Project

 

There are two major platforms you may choose for software development, UNIX-like and Windows-like.

 

UNIX – Gnu compiler

 

To create programs in Unix, use any editor (vi, pico, emacs) to create your cpp and header files.  To compile with the gnu compiler type:

 

            g++ -c myfile.cpp

 

This will compile the file, creating an object file called myfile.obj

 

To link the program, type

 

            g++ -o myprogram.exe myfile.obj

 

This will produce an executable file called myprogram.exe

 

If your executable program won’t run, check file permissions, you may have to change them to allow the file to execute.

 

The –g option allows you to produce files with debugging information.  You can then use any available debugger (gdb or DDD).

 

You can use these commands with multiple files.  As your project develops, requiring multiple files, with some files dependant on others, you will need to create a makefile.  This is a file with the filename ‘makefile’, and contains compiling and linking information

 

A simple makefile would look like the following:

 

myprog:  myprog.o

                        g++ -o myname myprog

 

myprog.o: myprog.cpp myprog.h

                       g++ -c –g myprog.cpp myprog.h

 

Note that the space preceding the compilation statements are a single tab character.  To execute this file, type ‘make’.  This makefile will first compile myprog.cpp to myprog.o.  It will then link it with myprog.h to create the executable, myprog.  Makefiles can do much more and can be very complex.  There are many excellent sites on the web that describe the uses and programming of makefiles.

 

For more information on the gnu compiler, check those user-friendly man pages.

 

 

Windows - Microsoft Visual C++

 

If you choose to use a visual tool, such as Microsoft Visual C++, then be sure that you create a Win32 Console application.  Below are simple instructions for creating, compiling and running a project in Microsoft Visual C++.

 

Once the Application has started choose : Select File - > New

 

A dialog box appears allowing you to enter the type and name of the project

 

 

 

After selecting ‘Win32 Console Application’ and entering the name for the project, press ‘OK’. This will trigger another dialog box.  Here choose ‘empty project’ and press ‘Finish’

 

 

 

 

Another dialog box appears to confirm the project information.  Press ‘OK’.

 

 

 

 

After project creation, the application  opens the project and displays a list of file folders. 

 

 

 

 

To add files to the project, right click on a folder and choose ‘add files to folder’.

 

 

In the ‘Insert Files’ dialog box, choose the file(s) that you wish to add and select ‘OK’

 

Once you have a file in the project, you can choose to compile that file.  If you have all of the files, you can also choose Build or Execute and all necessary files will be compiled

 

 

The window at the bottom shows the result of compiling and linking.

 

 

To execute your program, choose Build - >execute  or just select the exclamation point tool

 

 

And your program will run in an MSDOS window