01_install_CodeBlocks
01_install_CodeBlocks
Alexandra Stefan
1/22/2024 1
Code::Blocks and C compiler
• C compiler
• In order to run C code a C compiler is needed.
• Programmers write code in a “human readable form”. A compiler will generate a corresponding
special program that the computer can run.
• IDE (Integrated Development Environment)
• is a program that is used to edit, compile, run and debug code, BUT it still needs a C compiler in
order to do that.
• Code::Blocks is an IDE. It is NOT a C compiler
• You need both Code::Blocks itself and a C compiler.
• I have selected Code::Blocks because it has an option to download and install at the same time
both the IDE and a C compiler. It has other options that do NOT include the compiler so pay
attention to which installer you download.
• Other IDEs are available (e.g. Apache NetBeans, Microsoft Visual Studio).
• Any IDE is fine, but you need to have a C compiler and set-up the IDE to find it.
• NOTE: Code::Blocks does not work for Mac. Instructions for working IDE for Mac are in Canvas
2
Downloading Code::Blocks
Here is a good video with instructions for installing Code::Blocks and creating projects
The slides below also have the steps for installing Code::Blocks.
From the Code::Blocks download page: http://www.codeblocks.org/downloads
Click “Download the binary release”
3
Unix and Mac
• Mac users:
• See Canvas->Modules->”M2-System Set-up”
• Unix/Linus users:
• Install the GCC (GNU Compiler Collection) compiler – E.g. search “install gcc
compiler on Unix” and follow instructions that seem clear to you.
• Download and install Code::Blocks for your system
4
Windows
Download codeblocks-20.03mingw-setup.exe - this is the package that has both Code::Blocks and a
C compiler (the MinGW C compiler). If you already have a C compiler or prefer to install the C compiler
separate, download the appropriate package (e.g. codeblocks-20.03-setup.exe).
6
We will use the GNU GCC Compiler.
(other C compilers should also be ok, but if available,
choose the GNU one for consistency.)
7
Check that you can “Build and run” a file.
Follow the next pages to create a file and try
to “Build and run” it. If it does not work, it
could be because Code::Blocks cannot find
the C compiler. See page 14 (after the section
on creating and running a C file) that shows
how to make Code::Blocks.
8
Create a C file, compile it and run it.
9
Project or no project?
• Larger pieces of code consist of multiple files and are developed using
a project that organizes all those files.
• You do NOT need to create a project for now. We will do that later on
so that we can Debug our code. (Method 2 below will be used to add
the file to a Project in order to debug it.)
• We will create just a C file.
10
Create an empty C file
Method 1:
- File -> New -> Empty file (This will create a file called
Untiltled)
- File->Save file as…
- Navigate to the location where you will store your code
from this class ( e.g. \courses\1310\code\lectures_code
) and enter the desired file name: welcome.c
Method 2:
- File -> New -> File …
- Select C/C++ source and click Go
- Select C
- Navigate to the location where you will store your code
from this class ( e.g. \courses\1310\code\lectures_code )
and enter the desired file name: welcome.c and click Save
- Do NOT select Add file to active project click Finish
(Later we will need to add the file to a project in order to
be able to debug it, but for now, we will just create a file.)
11
Write code in the C file
Type the text below in the file (the text will be colored):
#include <stdio.h>
int main(void)
{
printf("Welcome to CSE 1310!");
return 0;
}
12
Select: Build->”Build and run”
Note for future: make sure every time you need to compile and run your code
you select “Build and run”, not just “Run”. This way the new (most recent)
code is compiled, as opposed to running the previously compiled code. It is
similar to refreshing a webpage to enforce viewing the updated version.
You should see this window pop up. Notice that the first line prints what you wanted.
15
Code::Blocks Settings
To show line numbers and change other settings go to: Edit ->
“Editor Tweaks”
• And then “Show Line Numbers”
• Also note the “Show EOL Chars”. EOL stands for End Of Line.
Files from different Operating Systems will have different
EOL characters: Windows - CRLF, Mac – CR, Unix – LF . See
also the table from the Wikipedia page
https://en.wikipedia.org/wiki/Newline.
16
Code::Blocks Settings
17
18
Create a project
• File -> New -> Project
Open a project
• When you open a project, it makes it visible in Code::Blocks
and you can run and debug the code in that project from
Code::Blocks.
• To open go to: File-> Open and then navigate to where the
.cbp file is for the project you want to open 21
Project: add/remove files Add a file to a project
You can add or remove files from a project. • To add a existing file:
Remove a file from a project right-click on the project name
• To remove a file: right-click on it and select Add files … and
select Remove file… navigate to the file you want to add
• If you remove a file from a project it • To create a new file and make it part of the project:
does NOT delete the file, it simply Make sure this project is active (it is listed and its name is in
removes it from the IDE’s list of files bold font. If not, right-click on it and select Activate Project)
associated with that project. Go to File -> New -> Empty file
Click Yes to add this new file in the active project
Enter the file name
Double-check that the Debug and Release boxes are
checked and click Ok
There are other ways to add files or rename files from a project.
23
Running individual files vs projects
• When you Build and Run… it will be done for the ACTIVE PROJECT.
• If you have multiple projects, make sure the one you want to work on
is active. (You can close the others, or explicitly make this one the
active one – right click on the project name to get that option)
• If you want to run a single file, then ALL your projects must be closed
(there should not be any project in the list of projects)
24