| From: | |
| Date: | 20 Feb 2001 at 19:56:18 |
| Subject: | Re: includes and stuff |
To: amiga-c@yahoogroups.com
From: Laurent FAILLIE <l_faillie@yahoo.com>
Date sent: Tue, 20 Feb 2001 10:19:24 -0800 (PST)
Send reply to: amiga-c@yahoogroups.com
Subject: Re: [amiga-c] includes and
>
> --- Joris Kempen <Trekker@studenten.net> a écrit : >
> Hello,
>
> Hi too,
>
> >
> > I'm trying to make some functions for myself to make
> > programming easier, i
> > want to put them in a seperate .h and .c file but i
> > don't know how to
> > organize it.
>
> Cool,
>
> > I'll give an example:
>
> [...]
>
> > So that's the header file, but then i did some
> > thinking and had some
> > questions: the function needs the asl.library and
> > the asl.h to work
> > properly. But when you need to open a libary you
> > also need the Exec.h files
> > included. My question where to put those?
>
> I think it should be in the .c file. These headers are
> only used INSIDE the function (and not need for
> passing argument), so it will speed up compilation :
> asl.h will not be readed for compiling all stuff that
> include your .h
>
> > And should i really open those libraries in my
> > function or should it be a
> > request before you use the function itself?
>
> Should be openned before you use this function.
> It's means you should check inside your function if
> libraries are open or ... use autoopen capability of
> many compilers.
>
> > Then i started writing the myfunctions.c,
> > i wrote it, i'll give you the source for it (hasn't
> > been tested, some maybe
> > lot's of bugs, just for the idea)
>
> [...]
>
> >
> > So i found out, i needed the string.h include to be
> > able to use the strcat
> > function, again i don't know what to do: put a
> > #include <string.h> in this
> > myfunctions.c file, or should it be in my
> > myfunctions.h file...
>
> In myfunctions.c !
>
> > Maybe you don't get all my problems, but the
> > mainpoint is: how to organize
> > such things to put some things in seperate files.
> > Till this time i always
> > just one file with all the code scrammed into it,
> > and i want to start
> > reorganizing my code.
>
> The rule is :
>
> - if it's something used by the interface (struct,
> typedef, etc ... used in the .h file) the place is in
> the header.
> - if not, it's in the .c file.
>
> Bye
>
> Laurent
>
> =====
> The misspelling master is on the Web.
> _________
> / /(
> / Dico / / 100 % Dictionnary Free !
> /________/ /
> (#######( /
>
> Quoi, des fautes d'orthographe! Pas possible ;-D.
>
> __________________________________________________
> Do You Yahoo!?
> Get personalized email addresses from Yahoo! Mail - only $35
> a year! http://personal.mail.yahoo.com/
>
> ------------------------ Yahoo! Groups Sponsor
> ---------------------~-~> eGroups is now Yahoo! Groups Click here for
> more details http://click.egroups.com/1/11231/0/_/451227/_/982693166/
> ---------------------------------------------------------------------_
> ->
>
>
>
You can take this one step further and make object oriented libraries
(yes... in C). You can effectively hide private data internal to your
library in the following way:
/* file: library.h */
struct MyObject;
struct MyObject * MakeObject(); /* malloc and initialise an object */
void FreeObject(struct MyObject *); /* free it */
... /* other operations on MyObject */
/* file: library.c */
struct MyObject {
};
The interesting twist to this of course is that you can do something
snazy in the MakeMyObject() to dynamically load any libraries you
need by using a static variable to count the number of objects. e.g.
unsigned long MyObjectCount=0;
struct MyObject * MakeObject() {
}
void FreeObject(struct MyObject * obj) {
}
If you want to you can (and probably should) close any libraries
you've openned previously for this library.
Since you cannot access any features of the object before you've made
one.. and since you can load your libraries then this seems like a
pretty good approach for the more 'normal' things you might code. (Of
course, there are things which fit with this quite well, and other
things which don't.)
Ian Woods
------------------------ Yahoo! Groups Sponsor ---------------------~-~>
eGroups is now Yahoo! Groups
Click here for more details
http://click.egroups.com/1/11231/0/_/451227/_/982695093/
---------------------------------------------------------------------_->