|
Borland
C++ 5.5 tutorial
:
Borland
C++ 5.5 tutorial
By
Pascal
COUDERT
I found
on the Borland site
the free compiler Borland C++ 5.5 allowing
to carry out programs for DOS and Windows and I will try to make you share
my discoveries relating to the use of this compiler. If you have other
interesting information, do not hesitate with me to make share of it.
I think
that the first thing to be made is to download the compiler which in fact
is a reduced version of Borland C++ builder . It does not contain
an editor of code or environment of development integrated IDE like
Borland C++ Builder or the visual development tools but it is perfectly
operational to carry out great applications and to give matter to be reflected.
In what relates to me, I prefer maitriser all the lines of code of my
programs and the tools is saying magic of visual development of applications
frighten me.
To
download the free compiler Borland C++ 5.5
:
To
download the free compiler Borland C++ 5.5 you must connect yourselves
on the site http://www.borland.com/bcppbuilder/freecompiler/
and follow the indications suggested (you will have initially to record
you free to obtain codes of acces who will allow you to proceed to the
downloading). The file name of the free compiler Borland C++ 5.5
is FreeCommandLineTools.exe and it requires 8.54Mo of
space on your hard disk.
To
install the free compiler Borland C++ 5.5
:
To
install the free compiler it is enough for you to cliquer on the name
of the file ( FreeCommandLineTools.exe ) and to leave the program
installation use the default options. All the files will thus be copied
in the file c:\Borland\Bcc5.5.
Being
given that this compiler is delivered without Environnement of Intgrated
development, you will have to do it initially fonctionner under DOS
or starting from Windows in a window DOS Moreover, as you will place
all the source files of the application which you wish to carry out
in only one file, it will be easier to you to launch the Bcc32.exe compiler
starting from this file by typing the command bcc32 . So
that the compiler can be launched in this manner, you must add to your
file autoexec.bat the following line: path=c:\Borland\Bcc55\bin and
to start again your computer.
If
you type then bcc32 under DOS or in a Dos window, the compiler
Borland C++ 5.5 displays its options of line of command :

Not to require to
type the commands necessary to each compilation (following Bcc32
), it is expected that the compiler will read the Bcc32.cfg file
which it will have normally to find in the file c:\Borland\Bcc5.5\bin.
Consequently, best and to immediately configure it by registering
the following lines there:
-I"c:\Borland\Bcc55\include
" : Indicate to the compiler which it must go to seek in the file
c:\Borland\Bcc55\include the files of inclusion containing the definitions
of the standard functions that you use in your program and that you
mention to him by directive #include .
-L"c:\Borland\Bcc55\lib " : Indicate to the compiler that it
must go to seek in the file c:\Borland\Bcc55\lib the lib files
containing information relating to the corresponding functions of
the DLL.

Being given that
the complilator will launch the editor of links ilink32.exe to
generate the executable file of your program, it is also desirable to
configure the file ilink32.cfg by registering the following line
there :
-L"c:\Borland\Bcc55\include
" : Indicate to the linkage editor that it must go to seek in the
file c:\Borland\Bcc55\lib the lib files containing information
to bind to your program the corresponding functions of the DLL.

By typing Ilink32
under DOS or in a window DOS one obtains the options of line of
ordering of the linkage editor Ilink32.exe :

To
test the installation of the free compiler Borland C++ 5.5
:
To test if the compiler
Borland C++ 5.5 is correctly installed on your computer, I propose
to you to make him carry out small executable test functioning under
DOS and another functioning under Windows 9x.
Test
program functioning under DOS :
1) Create a file
named c:\testdos
2) Using a text
editor type the following lines (the characters located after / /
are not essential. They are only used to comment on the program and
will not be taken into account by the compiler :
#include
<stdio.h>
/ / to include the file of heading containing the function printf()
int main()
/ / function of input of a program back written in language
C or c++
{ / / accodance of opening marking the beginning of the
code of the function main()
printf("Hello
World "); / / to display Hello World with the screen
return
0; / / parameter of the int type of zero value having
to be
/ / turned over by the function main()
} / /
accodance of closing marking the end of the code of the
function main()
|
3) Record the
lines above in a file called hello.cpp and place it in your
file c:\testdos .
4) Under DOS or
in a window DOS since the file c:\testdos launch the compiler
by typing the following command :
bcc32 hello
5) If your compiler
is correctly installed you must see the text Hello World being
displayed with the screen when you launch the program hello.exe
generated by the compiler.

Test
program functioning under Windows 9x
:
1) Create a file
named c:\testwin
2) With ugly of
a text editor type the following lines (the characters located after
/ / are not essential. They are only used to comment on the program
and will not be taken into account by the compiler :
#include
<windows.h>
/ / to include the file of heading containing the function MessageBox()
# pragma argsused / / not to generate a file listing
of compilation
WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE
hPrevInstance,
LPSTR
lpCmdLine,
int
nCmdShow) / / function of input of a Windows program
written in language C or c++
{ / /
accodance of opening marking the beginning of the code of
the function WinMain()
MessageBox(NULL,
" Hello World ", " ", MB_OK); / / to display Hello World
return
0; / / parameter of the type WINAPI of zero value having
to be
/ / turned over by the function WinMain()
} / /
accodance of closing marking the end of the code of the
function WinMain()
|
3) Record the
lines above in a file called hello.cpp and place it in your
file c:\testwin.
4) Under DOS or
in a window DOS since the file c:\testwin launch the compiler
by typing the following command :
bcc32 -tW
hello
You can note that
to indicate to the compiler which it is a question here of generating
a program for Windows we added the option of compilation -tW (attention
to respect the capital letters and the tiny ones). It should be noted
that in the case or you would wish more not to have to type this option
of compilation it would be enough for you to add it to the contents
of the Bwcc32.cfg file (do not do it immediately or to only
test because it will be further seen there is an easy way to avoid
juggling with window DOS when one wishes to work under Windows).
5) If your compiler
is correctly installed you must see the text Hello World being
displayed with the screen inside a box of message after having start
the program hello.exe (since Windows) generated by the compiler.

To get of the assistance and to use the API functions
:
In fact, all the
basic functions which make it possible to develop a program for Windows
are gathered in a unit called API (Application Programming
Interface) these functions have their entrance points in the
file windows.h located in the file c:\Borland\Bcc55\include.
This is why one places line #include <windows.h> at
the beginning of the code of the program. If one is brought to then
use functions written in another file of the file c:\Borland\Bcc55\include
it will be necessary to add to the beginning of the program corresponding
line #include <name of the file>. If you prefer to place
the file in the same file that your other sources then you will write
#include "name of the file".
The documentation
of the API functions and the standard resources of an application
Windows 32 bits is in the file win32.hlp provided by Microsoft
. One finds it with the majority of the compilers (paying). You
can also use the search engines of the Web to find it in downloading
on certain sites. For my part I used the Google
engine which came out me the following link :
http://www.inprise.com/devsupport/delphi/downloads/
On the corresponding
page you will find an input with the file win32.zip which contains
the file win32.hlp :
http://www.inprise.com/devsupport/delphi/downloads/win32.zip
(7.59 Mo)
You can also help
you of the help files of L ' API Windows CE for a first approach
of the functions which you need before looking further into search in
win32.hlp or elsewhere.
The help files of
API Windows CE are in downloading on the site of Microsoft
:
http://msdn.microsoft.com/msdn-files/027/001/460/wince212docs.exe
(5.05 Mo)
Another significant
source of information is in the kit platform SDK from Microsoft like
MSDN but good, for the moment let us not need any we really.
To
build the principal window of a Windows application
:
Here the code constituting
the basic skeleton of an application Windows 32 bits which we
will produce with the free compiler Borland C++ 5.5 and which
we will be able to modify thereafter to go a little further. C++
being extension from C and that personally I find that C
is simpler than C++ and than then the free compiler Borland
C++ 5.5 compiles C very well , we will work out of C .
In any case I do not include/understand C++ and I do not want
to include/understand it because I find that I am sufficiently organized
not to need to be guided artificially by the programming language him-mème
and its many constraints. Moreover API functions are written
out of C and the examples which are referred to it too.
|
#include
< windows.h >
/ / declarations
of variables
LPSTR lpszAppName="Ma
first Windows window ";
HINSTANCE
hInst;
HWND hWnd;
/ / Prototype
of functions
LONG WINAPI
WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
/ / Functions
int APIENTRY
WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine,
int nCmdShow)
{
MSG msg;
WNDCLASS
cls;
/ / Enregistrer
the class fenestrates principal application.
cls.hInstance
= hInstance;
cls.lpszMenuName
= lpszAppName;
cls.lpszClassName
= lpszAppName;
cls.hIcon
= LoadIcon(NULL, IDI_EXCLAMATION);
cls.hCursor
= LoadCursor(NULL, IDC_ARROW);
cls.hbrBackground
= (HBRUSH)(COLOR_WINDOW+1);
cls.style
= CS_VREDRAW | CS_HREDRAW;
cls.lpfnWndProc
= (WNDPROC)WndProc;
will cls.cbWndExtra
= 0;
will cls.cbClsExtra
= 0;
if (!RegisterClass(&
cls))
return(FALSE);
hInst =
hInstance;
/ / to create
the principal application
hWnd =
CreateWindow (lpszAppName, lpszAppName, WS_OVERLAPPEDWINDOW, 50,
50, 640, 470, NULL, NULL, hInst, NULL);
if ( !hWnd
)
return(FALSE);
ShowWindow(hWnd,
nCmdShow);
UpdateWindow(hWnd);
while(GetMessage(&
msg, NULL, 0, 0))
{
TranslateMessage(&
msg);
DispatchMessage(&
msg);
}
return(msg.wParam);
}
LONG WINAPI
WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
case
WM_CLOSE:
{
DestroyWindow(hWnd);
};
break;
case
WM_DESTROY:
{
PostQuitMessage(0);
};
break;
case
WM_QUERYENDSESSION:
{
DestroyWindow(hWnd);
};
break;
default:
return
DefWindowProc(hWnd, uMsg, wParam, lParam);
};
return
0;
}
|
After having written
the listing above with your text editor and to have recorded it under
the name fenbase.c and to have placed in the file c:\fenbase
of your hard disk. Good, you can also download it directly here:
fenbase.c . It will be noticed that the extension
of the file is not any more cpp but c . This will indicate
to the compiler or to the readers (it is more probable) than the program
is written in language C (good, Si one had left cpp that
would not have changed large thing as would say the other since C
is a subset of C++ . Perhaps on the level of the compile
time, I leave this calculation to the specialists). in short now it
is necessary to write the batch to launch the compiler with the
commands necessary without needing to tap keyboard in DOS each time.
Some prefer to use the files make but for my part I find that
it is too hard.
Summary of the files
of configuration having to be placed in c:\Borland\Bcc5.5\bin :
The file batch
will be comp.bat and placed in c:\fenbase. Its contents
will be identical so that we had typed under DOS or in window DOS to
carry out the test program Windows but with fenbase.c in the
place of hello.

Consequently
the contents of c:\fenbase are the following :

And
after a double clic on comp.bat , a few seconds of patience and
a clic on the cross of window DOS to close it when compilation is finished
one obtains :

Miracle!
here our small fenbase.exe
on which one can double cliquer to see being displayed our basic window
:

Good
I reduced it voluntarily a little it should well be acknowledged.
To
add resources (icons, menus, buttons etc...) to a window
:
For the moment we
do not have yet a window with menus, buttons etc... but it is in good
way. at the end of this paragraph we will have succeeded in creating
a window which is transformed into icon when it is reduced i.e. that
its icon (not standard provided by Windows) will be apear in the browser
or as shortened that one will want to place well on desk etc... but
especially one will have apris to use a file of resources in which one
will be able to add all the resources that one will want for our application.
Good for our study
one will say that our small end of program will be called testres.exe
and that all the files which we will create to develop it, in particular
the source code (testres.c) of the window will
be placed in the file c:\testres then with you to play for the
creation of the file.
An icon is a resource
and it must be mentioned in a file of resources (testres.rc).
It is also a drawing (testres.ico)
carried out with a program of drawing of icons. This file of resources
will have to contain the following elements :
| MAINICON
ICON LOADONCALL MOVEABLE DISCARDABLE testres.ico |
You will find the
description of resource ICON in the file rc.hlp which
is in the file b5ms.zip on site ftp of inprise : ftp://ftp.inprise.com/pub/bcppbuilder/techpubs/b5ms.zip
but this file weighs 17Mo . To extract a rc.hlp from it from
237 KB that made much. I then propose to you to download an older version
directly here file rc.hlp . Older but lighter
(132 KB), less heavy and largely sufficient for what concerns us. In
short, by opening the file rc.hlp you will find the link Resource-Definition Statements then ICON Resource . This heading will enable you
to include/understand what is written in our file testres.rc and
also to go to see which are the different resources at our disposal.
It will be also noticed that the file b5ms.zip contains a heavier
version (23,6 Mo) and more recent of the file win32.hlp mentioned
above on this page.
The syntax indicated
in the file rc.hlp of our resource icon for the file testres.rc
is nameID ICON [ load-mem ] filename where naneID can
be either an integer, or a name (character strings). In our case one
will choose a name. This name will be indicated in our main file of
management of window testres.c to the level of the declaration
of the class of window. Here the part of the program which corresponds
to the declaration of the class of window :
cls.hInstance
= hInstance;
cls.lpszMenuName = lpszAppName;
cls.lpszClassName = lpszAppName;
cls.hIcon = LoadIcon(hInstance,"MAINICON");
cls.hCursor = LoadCursor(NULL,IDC_ARROW);
cls.hbrBackground =(HBRUSH)(COLOR_WINDOW+1);
cls.style = CS_VREDRAW | CS_HREDRAW;
cls.lpfnWndProc = (WNDPROC)WndProc;
cls.cbWndExtra = 0; cls.cbClsExtra = 0; |
I marked you MAINICON
in boldface characters to voluntarily show you that one must find
it at the same time in the file of resources testres.rc and the
main file (patati and patata...) testres.c . by seeking the syntax
of the function LoadIcon() in the file win32.hlp (it appears
there, it is thus an API function final point) one finds :
HICON LoadIcon(HINSTANCE
hInstance, / / handle of authority application
LPCTSTR
lpIconName / / icon-name string gold icon resource to identify);
Where lpIconName
(replaced by MAINICON in our program) must be character strings
of the name of a resource icon it is thus gained! it does not remain
us any more that to compile testres.c with bcc32.exe to
make of it a testres.obj i.e. a file object (it is as that which
one must call it. It is all!!!), testres.rc with the compiler
of resources brc32.exe to make of it a testres.res i.e.
a file of resources compiled, for then binding testres.obj with
testres.res by using the linkage editor (one can also say linker)
ilink32.exe and thus carrying out our executable testres.exe
provided with its beautiful icon (interesting not?).
brc32.exe and
ilink32.exe just like bcc32.exe are in the file of installation
of Borland C++ 5.5 : c:\Borland\Bcc5\Bin consequently
we will be able to carry out the batch of compilation correspondents
and to carry out them one by since the browser because we defined the
path c:\Borland\Bcc5\Bin in the autoexec.bat.
You will find all
documentation available on bcc32.exe, brc32.exe, ilink32.exe in
the help file bcb5tool.hlp provided with the free compiler Borland
C++ 5.5.
To
compile testres.c on the way :
Here what the
file batch comp.bat must contain :

Compared
to the compilation of fenbase.c we simply added the option
-C for forbiding the compiler of launching ilink32.exe all
alone and obliging it to create only the file testres.obj .
Good, in this order, that would not have changed large thing if one
had not added -C because the testres.exe created (without
the desired icon) would have been crushed by the following (that moreover
later with its desired icon). But it is cleaner like that.
Consequently
the contents of c:\testres are the following :

And
after a double clic on comp.bat , a few seconds of patience
and a clic on the cross of window DOS to close it when compilation
is finished one obtains the creation of the file testres.obj :

To
compile testres.rc on the way :
The compiler of
resources win32 is thus brc32.exe (one had not spoken
yet much about it about that one). If you type brc32 under
DOS or in a Dos window, the compiler of resources displays its options
of line of command :

Here what the
file batch rc.bat ( rc for Resource Compilation
that made more fun!) for the compilation of testres.rc must
contain :

As indicated higher,
option -32 indicates a compilation win32 and the option
-R to produce only testres.res.
Consequently the
contents of c:\testres are the following :

And after a double
clic on rc.bat , a few seconds of patience and a clic on the
cross of window DOS to close it when compilation is finished one obtains
the creation of the file testres.res :

On
the way for the linkage editing (or linkage)
:
Recall
: The goal of the operation here is to bind testres.obj to
testres.res to make testres.exe of it.
Here what the
file batch link.bat (guess for link! ) must contain :

Then does that
you stop a corner of it?
Thus rather make
a turn in the file bcb5tool.hlp provided with the free compiler
Borland C++ 5.5 (I believe that I repeat myself) to see syntax
a little there which must take the line of command of ilink32 .
Here what one finds there :
ILINK32 [ @respfile][options
] startup myobjs, [ exe ], [ mapfile ], [ libraries ], [ deffile ],
[ resfile ]
Eh yes the place
of the commas is very significant. it is enough to forget one of them
and will patatra...
What is between
hooks is not essential it is written in Doc.!
[ @respfile
] = file of response of the linker (it will be seen how to make
some with our sauce later) for the moment one will occur some.
[ options ]
= -aa : To build a Windows application 32 bits (significant
not?). Better is worth being on!
startup =
c0w32 it is obligatory it is all! one finds this file (c0w32.obj)
in the file c:\Borland\Bcc5\Lib of the compiler. It appears
that it is for initializing well the linker and all the remainder
for the creation of a Windows program in the standards.
myobjs =
Ah here where should be placed testres.obj !
[ exe ] =
testres : One will say to him that our program will have to
be called testres.exe if it would not remember it!
[ mapfile ]
= Me I put nothing there. It will be able to make a file of mapping
if that pleases to him and also to choose the name corresponding as
long as it is there. Me that is equal for me.
[ libraries
] = import32 cw32 : good, they say that what is between
hooks is not essential but if one does not write here the references
to import32.lib and cw32.lib the linker never should
seek the inputs of the functions the API one in these files and one
wonders why testres.exe does not go: This program will be stopped
because it carried out an operation nonin conformity (and I pass from
there!).
[ deffile ]
= If one wants to give pleasure and build a file of definition
of module which one will refer here one can. Me I do not do anything.
The linker will have to only manage with its default options.
[ resfile ]
= Ah here where should be placed testres.res !
Consequently the
contents of c:\testres are the following :

And after a double
clic on link.bat , a few seconds of patience and a clic on
the cross of window DOS to close it when the linkage editing (or linkage)
is finished one obtains the creation of the file testres.exe :

Miracle! here
our small testres.exe
on which one can double cliquer to see being displayed our window
:

Good
I reduced it voluntarily a little it should well be acknowledged.
(There too I repeat myself?)
To
carry out a file batch of project
:
To develop our Windows
window with its icon we broke up the various stages :
- Compilation
of the main file of management of window testres.c with comp.bat.
- Compilation
of the file of resources testres.rc with rc.bat.
- Linkage editing
with link.bat.
In order to avoid
us all this handling (tiresome it should well be said), we will gather
all the commands in only one file called projet.bat (it is me
which invented it). Moreover, one will make so that the messages displayed
by the compilers and the linker during their work are placed in files
which it will be possible to go to read time as many than one will want
of it for the search for possible errors (nobody is perfect).
As long as one is
there for the search of the errors, I wanted to indicate to you that
file BCB5ERRS.HLP contained in b5std.zip could be very
useful for you (When you want to program all alone in your corner and
that you will refuse that one comes to give you consultings).
To download b5std.zip
:
ftp://ftp.borland.com/pub/bcppbuilder/techpubs/bcb5/b5std.zip
http://www.borland.com/techpubs/bcppbuilder/v5/updates/std.html
Here what the file
batch projet.bat must contain :

One will realize
that it is neither more nor less than the contents of the files batch
comp.bat, rc.bat and link.bat placed at the continuation
. We simply added one > of redirection towards an output
file for each stage . Information which was displayed in window
DOS when we proceeded in an independent way will be recorded in these
files.
If one re-initialise
the file c:\testres with only the files necessary here what it
must contain :

And
after a double clic on projet.bat , a few seconds of patience
and a clic on the cross of window DOS to close it when compilation is
finished one obtains :

It
is the same thing that before with in more the output files comp.txt
, rc.txt , link.txt , whole in only one operation.
The file testres.exe is also available and it does not remain
any more but with cliquer above to open our beautiful window, to make
a small short cut be placed on the desk for better seeing its icon etc...
It any
more but does not remain you to plunge you in the books and the help
files. The description of the standard functions of the language
C is in the file bcb5rtl.hlp contained in b5std.zip considering
higher. When you think of being able to use a function you must make
sure that it is compatible win32 while cliquant on Portability
. Also do not forget to include the file of heading containing
the function at the beginning of your program as we did for windows.h
. You have here all the elements which you need to work to carry
out beautiful applications without spending a penny.
With a good editor of free code as SynEdit
you will be able to do all that you want . It is
enough to indicate to him the file projet.bat in its small Advanced
- > Invoke DOS command for then launching compilation by pressing
the key F7 then enter.
I found
SynEdit on http://www.download.com
by using the key word editor and to download it it is enough
to cliquer on the following link: http://synedit.hypermart.net/synedit.zip
SynEdit weighs 3.28 Mo.
You can
also connect you on http://www.webnotes.org
to see what it is possible to make out of C with Borland ,
to find examples of code http://www.webnotes.org/sources.htm,
to use the search engines to find a file particular on Internet http://www.webnotes.org/chercher.htm
etc...
|