Archived

This topic is now archived and is closed to further replies.

how to create header file

This topic is 5791 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I want to create a header file so i can put aside all the messy stuff in it and leave the WinMain and Scene stuff in the cpp file i tried just saving the document as .h file but borland 5.5 dont seem to recognize it...... I thought i had to compile a cpp file into a .h file.... i dont know how to do that wit borland

Share this post


Link to post
Share on other sites
ummmmmm WHHHAAAAATTTT?

I think you are a bit confused about what header files are. Typically a header doesn''t even have any source. Just declerations for classes and functions. You then do the definitions of these in a .cpp (or .c) file (usually with the same file name) Then wherever you want to use those classes or functions you include the header file at the top.

Hope that helps

Share this post


Link to post
Share on other sites
you can put that stuff inside aslong as you only include it once.
It won''t let you pre-compile it either (makes compiling time faster)

lets say you had a program

  
// test.c

#include <stdio.h>

void soandso(char *s)
{
printf("so and so is a %s\n",s);
}
void main(void)
{
soandso("wanker");
}




you can make it into two files

  
// test.h

#include <stdio.h>
void soandso(char *s)
{
printf("so and so is a %s\n",s);
}

// test.c

#include "test.h"

void main(void)
{
soandso("wanker");
}



You should be using two seperate C files though, and not placing the code in the header. The header ususally holds the information for calling the function, but not the code it self. Helpful when linking multiple C files together. You''ll leran that as you go along.



Beer - the love catalyst
good ol'' homepage

Share this post


Link to post
Share on other sites
..........how do i do that

getting multiple cpp files to compile together

i cant even make a header file properly

i saved all function declaration and include stuff into a text
and then saved it as base.h (i didnt put any code in it)

when i try including with it, borland compiler says "declaration styntax error"
(i typed #include "base.h")

Man im lost...

Share this post


Link to post
Share on other sites
RTFM!! That acronym is way to funny . I''m just joking; we were all once newbies.

I would suggest getting a book on C++; go to Amazon.com or something. I''ve heard Sam''s teach yourself C++ in 24 days is good; though I''ve never tried it. I have Deitel & Deitel''s C++ How to Program and a few others; I thought it was good. Or, you can find one of the many C++ tutorials on the Internet.

Good Luck!

Share this post


Link to post
Share on other sites
well i found tones of cpp tuts on the internet
and im very familiar wit the language(although i ddint master it) i even have a boook(a crappy one at that) but none has ever taught me how to compile multiple files...

ps. i cant afford to buy another book....

Share this post


Link to post
Share on other sites
Well, I assume you are on Windows so you don''t need makefiles or such things. Just add the files you want to compile to your project (I don''t use Borland, but MSVC should be similar). And #include the headers of the *.cpp files into the *.cpp files you want them to be included in. You''ll have to look up Borland''s documentation since I don''t know if Borland uses projects or not;it should be similar.

For example:
You included test.cpp test.h and main.cpp in your project, so if you want to use some functions in test.cpp in main.cpp just do this:
  
// In main.cpp

#include "test.h" // Gives you access to the functions in test.cpp assuming you put your function prototypes or class defintion in there.

Share this post


Link to post
Share on other sites
In VC++ the .cpp files have to be included in the project for them to compile. If they are just extra functions and stuff, ie without corresponding .h files, then just compile the project containing all the .cpp files. Hopefully this is similar to the Borland approach

A common mistake is to try including a .cpp file which cannot be done

Share this post


Link to post
Share on other sites
regarding the multiple C++ file linking thing.

Which Borland 5 is it?

Borland C++ Builder 5.x?
Borland C++ 5.x?
Borland C++ 5.x free download?


If it is the free download, you can use the makefile or link manually.

If it is builder5, it is under the menu or pretty much anything with the mouse, or you type useinit.

if it is c++5 then you would do as Floppy (nice name ) said and it is under the menus at the top.


Tell us which one you use, and if I see this topic again, I will try and post a small example for you if no one else does.


The code in each file is actually quite simple, it is just the linking part that is probably confusing the hell out of you at the moment. My guess is that you have to type it out (free console version) which is a real pain in the rump.



Beer - the love catalyst
good ol'' homepage

Share this post


Link to post
Share on other sites
oh so thats wat make.exe is for...

i have the free borland 5.5
using it on a free text editor called jfe(jen's file editor)

i tried looking through the help file(for borland compiler) by searching for the key words "header file" but couldnt find anything useful.

Now that i look up make file, it says something about "managing project compilation and link cycles" but im still lost..

My text editor has the project workspace feature but i dont think adding all files to the jfe project workspace doesnt do anything since jfe is not made for borland...

ok now that you know what borland i have, i would reeely apreciate it if you'd show me the example

ps: i dont need to type out everything since my text editor can implement aplication into it, i implemented the borland so i can compile wit a single click of a mouse(include and library directory is defined separately in a .cfg file)

Edited by - azaxaca on January 31, 2002 5:02:33 PM

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
here''s an example:

file: Blah.h

#ifndef BLAH_H
#define BLAH_H // to prevent multiple declarations of the
// following

void some_global_function(int foo);

class Blah{

public:
void do_blah();
int do_another_blah();

private:

char do_private_blah();

int variable_one;
double variable_two;
};

#endif // BLAH_H


file: BlahTwo.h

#ifndef BLAHTWO_H
#define BLAHTWO_H

#include "Blah.h"

struct BlahTwo{

public:

int some_variable;
Blah some_blah;
// etc. etc.
};

#endif BLAHTWO_H

as you can see, I''m putting declarations in the header files.
you can put definitions/code as well but usually you don''t. Unless
you''re doing stuff with templates in which case it''s easier to put
code/definitions in the header because most compilers need funny options otherwise.


file: Blah.cpp (contains definitions, code etc.)

#include "Blah.h"

void some_global_function(int foo){


}

void Blah::do_blah(){

variable_one = 1;
variable_two = 2.4;
}

int Blah::do_another_blah(){

// some code here
}

char Blah::do_private_blah(){

// some code here
}

now BlahTwo doesn''t need a .cpp because there''s nothing to define

file: main.cpp

#include "Blah.h"
#include "BlahTwo.h"

int main(int, char**){

Blah b;
BlahTwo btwo;
b.do_blah();
btwo.some_variable = 5;
}


to compile u would go

bcc32.exe main.cpp Blah.cpp

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
linking has to do with when u have a whole bunch of compiled code in lots of different modules (object files).

where you go
cpp.exe file1.cpp file2.cpp file3.cpp etc.cpp

it compiles and links it for you.
When you start getting bigger projects with lots of files all over the place, then u often do the above in two steps. First you compile each cpp file into an object file (.o or .obj usually, then you link them alltogether. e.g. with the same example from above

cpp.exe -c file1.cpp
cpp.exe -c file2.cpp
cpp.exe -c file3.cpp
cpp.exe -c etc.cpp

the option -c asks for no linking, so u end up with file1.obj, file2.obj, file3.obj and etc.obj. These would normally be put in the same directory and then u would go

cpp.exe -eMyExecutable file1.obj file2.obj file3.obj etc.obj

(the -e says create the executable with the name MyExecutable in borland) of course, for such a simple project you would probably not go to such lengths.

Normally people will supply the .h file and the .o (or a library of .o''s in a .lib (or some other library format perhaps), or even possible a .dll ) to you. Then you include the header files in your project (#include "etc. etc" and link in the other files. If you''ve used any functions/classes/etc. that came with the borland compiler then u will already have done this, however the compiler will normally link everything in for you. But if u get a third party library, then u will have to give the directory and the library files to the linker/compiler yourself.

e.g. I have Bob''s library, it comes with a whole bunch of headers and one or two library files, say bob1.lib and bob2.lib.

The headers are in c:\BobsCode\include\bob
the libraries are in c:\BobsCode\lib

in your code you would go something like
#include "bob\header1"
#include "bob\header2"

and in compiling you would go

bcc32.exe -c -Ic:\BobsCode\include file1.cpp
bcc32.exe -c -Ic:\BobsCode\include file2.cpp
etc. etc.

-I tells the compiler that u''re including stuff from that directory, -L is for where object or library files.

and then
bcc32.exe -Lc:\BobsCode\lib -eMyExe file1.obj file2.obj bob1.lib bob2.lib

of course, you would normally make a makefile to do that or some script or if u''re in vc then u just set appropriate options.

Share this post


Link to post
Share on other sites
my borland has cpp32.exe instead of cpp.exe...

anyways, let me get this streight

to do compilation of multiple files, i just make a header which
has delcaration for all functions and classes, include the
header in all my cpp files then type

bcc32.exe main.cpp blah.cpp etc.cpp

???????????????????

or if i want to create object files and then squeez them all
together, i convert all cpp into obj using cpp32.exe with -c
option...
then put everything together using -e option?

cpp32.exe -c main.cpp
cpp32.exe -c blah.cpp
cpp32.exe -c etc.cpp

cpp32.exe -eOPENGL main.obj blah.obj etc.obj
??????????????????

thats all i understood so far...

o and, well if you were trying to tell me how to set include and library directories(at the end) i already know how to do it

Share this post


Link to post
Share on other sites
well that didnt work, i must be doing something wrong..

cpp32.exe doesnt create an obj from my cpp..
and bcc32.exe main.cpp blah.cpp didnt work either

bcc32.exe main.cpp base.cpp (include and library directory is defined in BCC32.CFG and ILINK32.CFG)

base.cpp:
Error E2141 base.h 17: Declaration syntax error
*** 1 errors in Compile ***



Edited by - azaxaca on January 31, 2002 7:46:44 PM

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
sort of... you don''t need headers for compilation of multiple files, however when u start getting large messy projects, using headers can make things a lot easier. To compile multiple files in a small project - say u just have the files main.cpp file1.cpp and file2.cpp - you would go cpp.exe main.cpp file1.cpp file2.cpp
in this case it will compile all the files and link them automatically into one executable (usually the executable will have the name of the first cpp file u give to the compiler). With headers, usually all you are doing is moving the declarations into the header file (and there''s nothing to stop you from moving code into them as well, in fact there are some nice libraries out there which consist entirely of header files ) though to make an executable you will need a .cpp file with the usual main function. The whole purpose of header files is to make life easier.

Compiling into object files also makes things easier, rather than recompiling the whole project every time you change something, you recompile the .cpp file(s) which need to be recompiled because of some change. The compiler will compile and link (well, it probably ask the linker to do the linking) everything for you unless u ask it to do otherwise.

Both header and object files help you organise your project so that everything works a bit smoother, but they aren''t neccessary... it''s just that you''ll end up with nasty migraines otherwise .

forgive me if I''m pointing out the obvious, but that error tells you that on line 17 in the file base.h you''ve made a mistake.

Share this post


Link to post
Share on other sites
you know what, i never even bothered to check line 17, i
thought it was just because i did something wrong(in the
process of compiling), turns out that i dont have ";" at the
end of every function prototypes after line 17. And the
DeInitGL function declaration was never defined in base.cpp(but
i dont think that would''ve caused errors but i still found a
problem)


and now all i get are warnings saying "so and so is already
defined in e:\myprojects\opengl\base.obj"

and it works fine!!

the only thing is, that the program displays a total white
screen instead of the usual triangle..
Well i''ll fix that somehow
thanks for the help everyone

Share this post


Link to post
Share on other sites
Well, I''ve been away for a little while.

You may never see this, but this is straight example showing a function and a value.

three files,

main.cpp, func.h and func.cpp





main.cpp
  
#include "func.h"

void main(void)

{

char i;

Repeat=5;

for(i=0;i<Repeat;i++)

func("Hello World\n");

}




func.cpp
  
#include "func.h"

char Repeat;

void func(char *string)

{

printf("%s",string);

}




func.h
  
#include <stdio.h>

void func(char *string);

extern char Repeat;




This is what you type to compile it (you don''t need cpp32)
  
bcc32 main.cpp func.cpp




This is the compiler output
  
Borland C++ 5.5 for Win32 Copyright (c) 1993, 2000 Borland
main.cpp:
func.cpp:
Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland





and this is the program output
  
Hello World
Hello World
Hello World
Hello World
Hello World




main.cpp starts the program, sets Repeat, and runs func() the amount of times that Repeat was set to.

func.cpp actually STATES the function "func" and the value "Repeat".

func.h is what lets main.cpp know that func and Repeat exist.




Beer - the love catalyst
good ol'' homepage

Share this post


Link to post
Share on other sites