compiling_codeblocks
compiling_codeblocks
Compiling C
Programs
CodeBlocks
CONTENTS
This guide is to help you to familiarise yourself with the CodeBlocks environment on the School
computers.
Even if you are familiar with computer applications or programming languages, you should still read these
instructions; the behaviour of the operating system and applications may different to other computer
systems you may have encountered.
Before you attempt to begin programming in C, ensure that you are able to login from any within the lab,
using your University (ADF) username and password. If you cannot login, please contact the IT Service
Desk <http://www.itservicedesk.bham.ac.uk> or visit them in the Main Library.
DISK SPACE
Programming in C can use up significant amounts of disk space. If your disk space is full, one might
expect that the compiler to issue an error message but unfortunately this isn’t always the case - instead you
may see the compiler malfunction or exit in an unexpected manner.
REPORTING PROBLEMS
For problems CodeBlocks, the C programming language, or the lab exercises, consult the
supervisors during each lab session or email the course lecturer.
For problems with your computer logon account, disk quotas, faults with PCs, network problems,
report to the IT Service Desk <http://www.itservicedesk.bham.ac.uk>.
1
1 THE CODEBLOCKS ENVIRONMENT
Before running CodeBlocks you need to create a directory to store all your programs in.
On the Windows desktop, click on the Start button (bottom left hand corner) and then “This PC”.
Double click on your home drive (which is normally under “Network Locations”. Click on Home | New
Folder. When the new folder appears in your directory, type c_programs and press Enter.
You can also start CodeBlocks by clicking on the Start button and typing in “CodeBlocks”.
On some computers, the first time you start up CodeBlocks you may see a window (figure 1.1) which asks
which compiler you are seeing. If you see this window, click on “GNU GCC Compiler”, then click on
“Set as Default” then click on OK.
Again, on some computers the first time you start up CodeBlocks you may see the following window.
Click on OK to continue.
2
You should then see the main CodeBlocks window (figure 1.3).
The next stage is to create a new project. You can do this by clicking on “Create a new project” in the
centre of the CodeBlocks window, or you can do it through the menu File | New | Project.
You should then see the following window. Choose “Console Application” and click on Go.
The Console Application wizard should appear (Figure 1.5). Click on Next.
3
Figure 1.5. Console Application wizard.
The language selection window should appear (figure 1.6). Choose “C” and click on Next.
Do not choose “C++”.
In the next window, you are asked to choose the location and filename of the C project that you are
creating. On University computers, always choose U:\c_programs as the location to store projects in. For
the first project that you are creating, choose the following settings:
The other two boxes will be filled in automatically (see figure 1.7). Then click on Next.
4
Figure 1.7. Project title and location.
On the next window (figure 1.8), make sure that the compiler is set to “GNU GCC Compiler”, and click
on Finish.
You should now see the main project window (figure 1.9).
5
Figure 1.9. Main project window.
On the left hand side, click on the “+” icon next to “lab_1a”, then click on “Sources”. You should see the
following files:
If you install CodeBlocks on your own personal computer, you may see an example “main.c” program
appear. However there are some differences between this example program and the ANSI C standard we
are learning on this module. Therefore, it is recommended that you delete all the lines of “main .c” and
move onto the next section.
6
Figure 1.11. “main.c” source after deleting all the lines.
A FIRST C PROGRAM
Once you have reached the stage in figure 1.11, you can now begin entering your first C program. Click
once anywhere within the blank “main.c” window, and type in the following exactly as shown:
#include <stdio.h>
int main(void)
{
printf("Hello world\n"); /* This prints "hello world" */
return 0;
}
Once you have entered the program in, it now needs to be saved. Press Ctrl+S or click on the icon.
7
Rules for saving files
It is important to understand the distinction between pre-processing, compiling and linking programs.
In addition to header file and source file, you should also learn about the terms object file, executable
file, and library. Consult your lecture notes and the recommended textbook. In later exercises you may
need to finely control how individual files are compiled and linked.
If you typed in the program correctly, then you should see the following in the output window:
If you see this message, then you are now ready to run your program. If not, then read the section on
“Compiler Errors” in this handout, or consult a laboratory supervisor.
You should see the output of the program in a separate window (Figure 1.12).
8
COMPILER ERRORS
In the “lab_1a” program, try to create a deliberate error. On the line containing “printf”, remove the semi
colon ( ; ) from the end of the line. Click on Build and Build. You should now see the following in the
“Build Messages” window at the bottom of the screen:
This window now contains details of the lines in your program that contain compiler errors. You can
scroll up and view these errors. You should see an error message, namely:
Double-click on the error message. The cursor will be positioned at the appropriate point within your
program, usually just before or just after the actual error.
The error confirms that you have omitted a semicolon, and this is a very common error in C.
Correct the error and rebuild the program.
Tips
It is important to note that the line on which the compiler first detected the error. However, this is not
necessarily where error actually occurred.
Always examine several lines either side of the actual line where the compiler reported the error. If you are
within a loop or function, then you may also need to investigate the lines at the start and end of the
current loop or function as well.
Always compile your program frequently and consult a supervisor if you do not know how to remove
errors.
Linker errors are initially encountered less frequently than compiler errors. When a linker error occurs,
only the object file where the error occurred will be reported - there will not be any information relating to
line numbers. The commonest linker error is calling a function incorrectly (perhaps with the wrong name)
or failing to provide a library or object code required by a function. If you mis-spell or omit the main
function, this will also generate a linker error.
After you finish your programming session, the folder containing C program should have been saved. As
mentioned earlier, copy your C program folders (e.g. “lab_1a”), onto a USB stick or portable hard drive,
in case you accidently delete your U: files.
Sometimes when opening or reopening a project, your Workspace and Files window may disappear from
the left hand side. You can get it back by clicking on View | Manager.
9
2 COMMAND SUMMARY
To build (i.e. compile and link) a program Ctrl+F9 Build -> Build
To compile only (i.e. no linking) Ctrl+Shift +F9 Build -> Compile current file
To run a program Ctrl+F10 Build -> Run
To go to the next compiler error Alt+F2 Build -> Errors -> Next Error
To go to the previous compiler error Alt+F1 Build -> Errors -> Previous Error
Other
C Source file .c
C++ Source file (don’t use this) .cpp
Header file .h
Executable program .exe
Object file .o
Project file .cbp
10
3 CAUSES OF STRANGE ERROR MESSAGES
Build | Build and Build | Run menu options are Your program can’t be rebuilt whilst it is still
greyed out running. Quit the program and recompile.
undefined reference to 'WinMain@16’ You have not defined the “main” function within
your program.
multiple definition of 'main' You have two or more C source files in your
workspace, both containing “main”. Only one
source file should contain “main”.
Rebuilding your program after making changes Multiple C Source Files – remove one or more
does not change your program when it is run source files (as above)
undefined reference to 'Printf' Errors similar to this may indicate that you have
made a spelling mistake when using a C function.
In this case “printf” has been spelt wrongly with a
capital “P”.
The error will also occur if your own functions are
not defined or called correctly.
Errors or warnings about spaces
You must not use any spaces in your project or
filenames on a MacOS or Linux system.
4 OBTAINING CODEBLOCKS
CodeBlocks is a free open source application. It can be downloaded from the following website:
http://www.codeblocks.org/
Once you have read the introduction go to the Downloads | Binaries section.
Windows users should download the “codeblocks-20.03mingw-setup.exe” file, which includes the
GCC compiler. The other versions of CodeBlocks do not include the GCC compiler.
MacOS users should note that the version of CodeBlocks for this operating system may be older that the
Windows version but will work in the almost exactly same way.
On MacOS, when creating your projects, do not put spaces in any project or folder names. Use names
such as “C_programs”, do not use names such as “C Programs” containing a space.
11
5 OTHER C COMPILERS
More advanced users may wish to note that there are guides available (from the same place that you
obtained this guide) for the following other C compilers:
Dr S Pammu 2019
12