0% found this document useful (0 votes)
22 views13 pages

compiling_codeblocks

This guide provides instructions for using the CodeBlocks environment for programming in C at the School of Engineering. It covers setup, compiling and running programs, troubleshooting common errors, and offers a command summary for efficient navigation. Additionally, it includes information on obtaining CodeBlocks and mentions alternative C compilers for advanced users.

Uploaded by

prinz.j06
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views13 pages

compiling_codeblocks

This guide provides instructions for using the CodeBlocks environment for programming in C at the School of Engineering. It covers setup, compiling and running programs, troubleshooting common errors, and offers a command summary for efficient navigation. Additionally, it includes information on obtaining CodeBlocks and mentions alternative C compilers for advanced users.

Uploaded by

prinz.j06
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

School of Engineering

Introduction to Computing for Engineers


Software and Systems

Compiling C
Programs
CodeBlocks
CONTENTS

Section 1 The CodeBlocks Environment 2-9


Section 2 Command Summary 10
Section 3 Causes of strange error messages 11
Section 4 How to obtain CodeBlocks 12
Section 5 Other C Compilers 12

THE PURPOSE OF THIS GUIDE

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 START

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.

To check your disk space:


 Double click on “Computer”
 On a campus computer, right click on U: drive, and click on “Properties”. On your own
(Windows) computer this will be usually C:.

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.

Start up CodeBlocks by the following:

Click on the Start button, then scroll down to CodeBlocks


and then click on CodeBlocks 20.03.

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.

Figure 1.1. Choosing the Default Compiler (won’t always appear).

Again, on some computers the first time you start up CodeBlocks you may see the following window.
Click on OK to continue.

Figure 1.2. Default application for C files (won’t always appear).

2
You should then see the main CodeBlocks window (figure 1.3).

Figure 1.3. Main CodeBlocks window.

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.

Figure 1.4. New project – “Console Application”.

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++”.

Figure 1.6. Language selection window – 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:

Project Title = lab_1a


Folder to create project in = U:\c_programs

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.

Figure 1.8. Compiler (should be set to GNU GCC).

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:

Figure 1.10. Source files.

Double-click on “main.c”. On University computers, a blank file will appear.

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:

/* Hello World program */

#include <stdio.h>

int main(void)
{
printf("Hello world\n"); /* This prints "hello world" */
return 0;
}

In all your C programs, make sure that you:


 Use comments /* .......*/ to explain and document your program. Marks will be deducted for
programs that are poorly commented.
 Learn to indent your program in the familiar “staircase” style that you can find in all C textbooks.
Reading a program that is not indented can be difficult or even impossible.

Once you have entered the program in, it now needs to be saved. Press Ctrl+S or click on the icon.

Saving your program


Saving your files frequently is an important part of computing. Computers can crash, and you also need
to look at how you wrote your code from previous laboratory sessions. It is therefore vital that you make
copies of your files regularly by copying them onto a USB stick or external drive.

7
Rules for saving files

 Save your files frequently.


 On University computers only save your files onto your U: drive.
 Ensure that your C program filenames end in .c. If you find that files are being created as “.cpp”, this
means that you chose “C++” as the programming language, instead of “C” as is recommended.
 At regular intervals, copy your C program folders onto a USB stick or portable hard drive.
If you fail to save your program frequently, and the computer or the software crashes, you will lose all
your work for that session. If this happens and your work is being assessed, you will not get any extra
time.

COMPILING AND LINKING

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.

For now, all you need to do is


Click on “Build”, then “Build”
Or Press Ctrl+F9.

If you typed in the program correctly, then you should see the following in the output window:

Output file is bin\Debug\lab_1a.exe with size 28.47 KB


Process terminated with status 0 (0 minute(s), 0 second(s))
0 error(s), 0 warning(s) (0 minute(s), 0 second(s))

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.

RUNNING YOUR PROGRAM

To run the “lab_1a” program:


Press Ctrl+F10.
Or click on “Build” then “Run”.
Or click on the icon.

You should see the output of the program in a separate window (Figure 1.12).

Figure 1.12. Output of the “lab_1a” program.

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:

Build failed: 1 error(s), 1 warning(s) (0 minute(s), 0 second(s))

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:

u:\c_programs\lab_1a\main.c 8 error: expected ';' before 'return'

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.

REOPENING YOUR PROJECT

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.

To reopen your program:


 Start CodeBlocks
 Click on File | Open | Project
 Browse to the folder containing your C program. You may need to click on “This PC” and then click
on your U: drive at the bottom.
 Navigate to the “c_programs” and “lab_1a” folders, and click on “lab_1a.cbp”. Do not open
“main.c” from this window because it will not be setup correctly as a C project.
 If you are opening a project on the same computer you can also look for it in File | Recent Projects.

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

Compiling and running a program

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

Debugging (separate guide available on debugging)

Start debugging F8 Debug -> Start / Continue


Right click -> Toggle breakpoint,
Insert / remove breakpoint F5
or click to left of source code window
Continue until next breakpoint F8 Debug -> Start / Continue
Add variable to watch list - Right-click on variable and “Watch …”
Step one instruction, skipping over functions F7 Debug -> Next Line
Step one instruction and into functions Shift+F7 Debug -> Step Into
Step out of a function Ctrl+F7 Debug -> Step Out
End the debugger Shift+F8 Debug -> Stop Debugger

Other

Close the current window Ctrl+W Window close button


Click on Tab at top of
Cycle between source files Ctrl+Tab
workspace
Save current source file Ctrl+S File -> Save
Help facility F1 Help menu bar

Important Filename Suffixes

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)

error: invalid operands to binary ^ The ^ operator can’t be used in C to evaluate


(have 'double' and 'double') powers. You need to use the “pow” function
defined in the Math library.

warning: implicit declaration of You have not included “math.h”.


function 'sqrt'

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.

CodeBlocks is also available for several different Linux distributions.

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:

 Windows – Visual Studio


 Linux – Eclipse / gcc
 MacOS - Xcode

Dr S Pammu 2019

12

You might also like