Starting Out with Code::Blocks

Transcription

Starting Out with Code::Blocks
Code::Blocks intro
p. 1
Starting Out with Code::Blocks
VERSION 1.0! last modified: 2016-04-04
NOTE!
The screenshots included here are from the Mac OS X version -- however, while the Windows versions
looked different sometimes, the content was pretty much the same.
Where to get Code::Blocks
Download and install an appropriate version for your computer starting from:
http://www.codeblocks.org/downloads/26
• If you are running Windows, since I am not sure which is best, I'd go with their recommendation and
use "codeblocks-16.01mingw-setup.exe" -- click on that file's "Sourceforge.net"
link.
– when I tried this on my spouse's Windows 8.1 laptop, I opened this downloaded file, and went
through the installation accepting all of the defaults.
• If you are running Mac OS X, try "CodeBlocks-13.12-mac.zip" -- click on that file's
"Sourceforge.net" link.
– Then, on my Mac, I double-clicked on "CodeBlocks-13.12-mac.zip" so it expanded;
– This seemed to result in a CodeBlocks.app file -- when I double-clicked on it, Code::Blocks
started up.
• If you are running Linux, try one of the files for your particular Linux distribution, also being sure to
read over the notes at the end of the Linux section of the page.
Starting Code::Blocks
• For Mac OS X, double-click on the file CodeBlocks.app -- for Windows, double-click on the
shortcut icon that I think should result from installation.
• On my Mac, when I double-clicked on CodeBlocks.app, I got this:
Code::Blocks intro
p. 2
• In the world of many (most?) C++ integrated development environments (IDEs), you put your
program -- your collection of C++ functions, at least one of which is named main -- into a project.
– In practice, this project is usually a folder/directory that contains your C++ program's files along
with some additional files that the environment uses for various purposes.
• In the largest window above, there is a link "Create a new project" -- you can click on that to, indeed,
create a new project. When you do so, you will see something like this:
Code::Blocks intro
p. 3
• The order of the icons varies between Mac and Windows versions, but both have the icon we want,
which is "Console application".
• Double-click on the "Console application" icon, and you should see something like:
Code::Blocks intro
p. 4
• Click the "Next>" button in the lower right, you should see something like:
• ...and since C++ is already highlighted, and that IS what you want, click the "Next>" button, and you
should see something like:
Code::Blocks intro
p. 5
• When you enter a Project title, the Project filename will be filled in based on that title. It will be
simpler if you avoid blanks in the Project title.
– For example, in "Project title:"'s textfield, you could enter something such as
test_circ_area
– next to "Folder to create project in:", click the little square to the right of the textfield to get a
"Choose a Folder" file browser -- and navigate to where you want this project directory.
NOTE - IN A CAMPUS LAB -- you CAN navigate to the U: drive, and save your project, thus,
on nrs-labs!
– Notice that "Project filename:" is now filled in, with something such as:
test_circ_area.cbp
– ...and "Resulting filename:" is now filled in, based on the folder you selected.
– The result might look something like this:
Code::Blocks intro
– Now click the Next> button, and you should see something like:
•
p. 6
Code::Blocks intro
p. 7
• MAKE SURE that, in the "Compiler:" drop-down, "GNU GCC Compiler" is selected -- it is likely
already the default.
– NOT knowing any better yet, I am leaving the "Debug" and "Release" configuration choices as
shown.
– ...and click the "Finish" button.
• Now you should see something like:
• See the area on the left-hand-side of the window, labeled "Management", with the "Projects" tab
selected? Click on the little triangle next to Sources -- I think that's where we will put our source
code files for our C++ program -- and see how it now lists main.cpp:
Code::Blocks intro
p. 8
...remember, every C++ program has to have a function named main!
– We'll be renaming this main.cpp to a file that is named the name we want for our eventual
executable file (this is a class coding standard, because I will also be running your programs
under Unix as well as within an IDE).
– Right click (control-click on a Mac) on the name main.cpp, and select "Rename file". I can't
get a screen shot of this, BUT it should be pretty straightforward to enter the new name you want
-- here, something like test_circ_area.cpp would be appropriate.
– You should then see your new name under "Sources":
• Double-click on test_circ_area.cpp, and in the large middle window, you should see a
syntax-colored main file template!
Code::Blocks intro
p. 9
• If you would like to change the font size, you can do so at Settings->Editor... -> General Settings -see the "Choose" button near the upper-right corner.
– There are some other useful-looking settings here, too -- how "big" a tab is, whether certain
things are done automatically, what kinds of newline characters should be used, and more;
• This needs a few changes to meet class coding standards, but it DOES run -Go to the Build menu, and select "Build and run". This actually compiles, links, loads, AND runs
this little program, and either a Mac Terminal or a Windows console window will open up -- and
you'll indeed see
Hello world!
within it, along with run statistics and an instruction to "type any key to continue".
• NOW: to make it meet class coding standards, do the following:
– at the VERY top, add:
#include <cstdlib>
You won't see this in my first example below -- BUT I needed it on the Windows' versions, and it
doesn't hurt to have it in the Mac version.
– change:
return 0;
to:
Code::Blocks intro
p. 10
return EXIT_SUCCESS;
– Do File->Save OR control-s/command-s to save your changes
– Then do Build->Build and run, and your tiny program should again be compiled, linked, loaded,
and run.
• BUT: how do I now ADD IN something like, say, circ_area.cpp and circ_area.h to this
project?
– You can copy them into a new empty file -- do File->New->Empy File, and you will likely see
something like:
– Click the Yes button, and you should see something like: (although this window looks VERY
different on the Windows version):
Code::Blocks intro
– For example, you could say you'd like to save it as circ_area.cpp
(and leave Where: as test_circ_area, and leave Format: as C/C++ files)
...and click Save button
– (when you see something like:
– ...I am just saying OK (at least for now).
• If you then pasted the contents from the posted circ_area.cpp into circ_area.cpp in
p. 11
Code::Blocks intro
Code::Blocks, you should see something like this:
• and you could repeat the above to create and paste in circ_area.h:
p. 12
Code::Blocks intro
p. 13
• and now when you do File->Save File, notice that circ_area.h is actually placed in a Headers
folder in the Management part on the left hand side -- originally appeared as:
– ...but when you click on the little gray triangle next to Headers, you see that circ_area.h IS
Code::Blocks intro
there:
• Now edit test_circ_area.cpp's main function to call circ_area.
– double-click on test_circ_area.cpp in the left Management window -- and
test_circ_area.cpp's contents show up in the main, center area:
p. 14
Code::Blocks intro
p. 15
– edit in the following:
#include <cstdlib>
at the beginning [SH 118/BSS 313 need, yours at home might, too]
#include <cmath>
after #include <iostream> [needed in labs]
#include "circ_area.h"
right before using namespace
cout << boolalpha;
at the beginning of the function body (I forgot this before
taking the screen shot below!!)
cout << "circ_area(10) == 314.159: "
<< (circ_area(10) == 314.159) << endl; right after the original cout
and the result looks similar to:
– do File->Save File (or command-s) to save -- see how the yellow change bars are now green, and
test_circ_area.cpp's tab no longer has an *, meaning that file has unsaved changes?
Code::Blocks intro
p. 16
– Build->Build-and-run to compile, link, load, and run your now-3-file program!
• When you Quit using the last item in the leftmost menu (File for Windows, and CodeBlocks for
Mac), you see something like:
– click Yes button.
• NOW -- what happens when you AGAIN double-click on Code::Blocks icon or CodeBlocks.app?
Code::Blocks intro
p. 17
– you get a similar opening screen as before;
– if you select "Open an existing project", and navigate to test_circ_area, and select
test_circ_area.cbp, (which didn't have the suffix showing in the Windows version), and
click the Open button -- it SHOULD open that project;
• IF you add files TO a project's directory/folder -- HOW can you then add it to the project?
– In the Project menu, select "Add files...", and then highlight the name (or names...!) of the newlyadded files, and click the "Open" button
– click the OK button when it asks something similar to:
...and you'll see the files now ARE listed in the Management window on the left!
(and you can edit the main function so it #include's a new function's .h file, and then call that
new function!)