How Do I ......????
Rick Sivernell
res005ru
Sat Oct 9 07:26:55 PDT 2004
On Sat, 9 Oct 2004 08:22:10 -0400
Kurt Wall <kwall at kurtwerks.com> wrote:
> On Thu, Sep 09, 2004 at 02:08:49PM -0500, Ben Duncan took 69 lines to write:
> >
> > Ok, I am writing a system that contains several modules.
> >
> > The header for the system has a declaration for a function defined
> > as :
> >
> > int OpenWindow( int row, int col, int rowsize, int colsize, const char
> > *title)
> >
> > row, col, rowsize, colsize are USED thru out all the modules.
> >
> > I have one program called slag-module.c, that forms the library core
> > and calls all other modules compiled in. Some of the modules are
> > called : grid.c, button.c, form.c, checkbox.c, ..etc..etc..
>
> They each include the header, right?
>
> > So, I have a compile statement that looks like thus:
> > ------------------------------------------------------
> > for file in `ls src/*.c`
> > do
> > OBJ=`basename $file ".c"`
> > echo "Now creating file $OBJ from $file "
> > cc -c -O2 -fno-strength-reduce " objdir/${OBJ}.o \
> > -I./include -I/usr/include/slang \
> > -fwritable-strings \
> > src/${OBJ}.c
> > done
>
> That's certainly doing it the hard way. Make is your friend:
>
> OBJS := grid.o button.o form.o checkbox.o
> CFLAGS := -O2 -fno-strength-reduce -fwritable-strings
> INCLUDES := -I./include -I/usr/include/slang
>
> .c.o:
> $(CC) -c $(CFLAGS) $(INCLUDES) -o objdir/$@ $<
>
> > echo "Now creating the .so module"
> > cc -c -02 -fno-strength-reduce -shared -fPIC \
> > -I./include -I/usr/include/slang src/slag-module.c \
> > objdir/*.o \
> > -o objdir/slag-module.so \
> > -L/usr/lib -lslang -ldl -lm
> > --------------------------------------------------------
> >
> > In the slag-module.c is the actual OpenWindow function, and
> > in several of the c program modules they call
> > OpenWindow( 'int row', 'int col', 'int rowsize', 'int colsize', "String
> > of data")
>
> Why not just use a proper function? And why are are you specifying
> multi-character char types?
>
> OpenWindow(1, 2, 3, 4, "some string");
>
> > What I would like to do is set up some sort of GLOBAL variable, so that the
> > modules that are using "OpenWindow" would do it this way:
> >
> > gl_row = 'int row' ;
> > gl_col = 'int col' ;
> > gl_rowsize = 'int rowsize' ;
> > gl_colsize = 'int colzise' ;
> >
> > OpenWindow( "string of data")
> >
> > An where "gl_xxxxx" is modifiable from any modules, they are
> > defined JUST in one place.
>
> You just write a wrapper around OpenWindow() that calls it the way you
> want it called:
>
> OpenWindowWrapper(const char *s)
> {
> OpenWindow(row_val, col_val, rowsize_val, colsize_val, s);
> }
>
> More generally, unless I'm missing something, it is unwise program design
> to do what you're proposing. It defeats type checking and makes your code
> a lot harder to read. Those global variables can get changed by anything
> in scope, which introduces some nasty bugs.
>
> > So, how do I set this up do to this ?
>
> Kurt
> --
> No man in the world has more courage than the man who can stop after
> eating one peanut.
> -- Channing Pollock
> _______________________________________________
> Linux-users mailing list
> Linux-users at linux-sxs.org
> http://mail.linux-sxs.org/cgi-bin/mailman/listinfo/linux-users
Kurt is absolutely correct about global variables, they are extremely dangerous. If
you must use them, do it as judiciously as possible to the non use. I would suggest
designing your code to pass or set variables in their local scope. Pass them to a
function by reference ( &<name> ) or by value ( <name> ) if the value does not change.
If you are new to C/C++ development a good book will explain it. especially the K&R
bible. One really needs to understand the scoping rules for functions also.
Kurt is one very good of a resource.
cheers
--
Rick Sivernell
Dallas, Texas 75287
972 306-2296
res005ru at verizon.net
Gentoo Linux
Registered Linux User #193859
.~.
/ v \
/( _ )\
^ ^
In Linux we trust!
More information about the Linux-users
mailing list