Re: [Dev-C++] RES: Programming with multiple modules/files
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Per W. <pw...@ia...> - 2007-10-14 20:50:48
|
First of all - if I was you, I would immediately rewrite the functions to
take parameters. Globals for parameter-passing is WRONG. W.R.O.N.G. You
most definitely don't want such a solution. For a school work, you would
not pass. For a commercial work, you would get huge support costs. For
private stuff - no reason why not do it the correct way!
Second: My suggesttion does handle multiple files including the same
global.h file, and takes care of the extern keyword in a reasonable way.
Having multiple source files calling functions from multiple other source
files is how most software is written. Either write one header file for
each source file, and let each source file include all needed include
files. Or, if the project is small, add all function prototypes to a
single include file.
But make sure that you immediately drop any globals for parameter passing.
Yow will you ever remember to give all global variables an initial value
before calling a function? With parameters, the compiler will complain.
With globals, it will not know!
/pwm
On Sun, 14 Oct 2007, Andr=E9 Mac=E1rio Barros wrote:
> Thank you!
>
> But, I forgot to tell that:
> The "other.c"-type files also make
> references to the global variables and
> they also call functions that belong to
> other "other.c"-type files.
>
> In other words:
> ///main.c///////
> #include "global.h"
> main()
> {
> func_1();
> a =3D 100;
> func_2();
> func_3()
> ...
> }
> ///module1.c///////
> extern int a;
> extern int b;
> extern int c;
> func_1()
> {
> a=3D300;
> ...
> }
> ///module2.c///////
> extern int a;
> extern int b;
> extern int c;
> extern func_1();
> func_2()
> {
> a=3D300;
> func_1(); // see this!
> ...
> }
> ///module3.c///////
> extern int a;
> extern int b;
> extern int c;
> extern func_1();
> func_3()
> {
> a=3D300;
> func_1(); // see this!
> ...
> }
> ///global.h///////
> int a;
> int b;
> int c;
> void func_1(void);
> void func_2(void);
> void func_3(void);
> /////////////
>
> Yes, I could improve the flexiblity of these functions
> using parameter-passing. But, is there anything that I
> can do to avoid these "extern" declarations in the modules?
>
> Regards
> Andre
>
>
> -----Mensagem original-----
> De: Per Westermark [mailto:pw...@ia...]
> Enviada em: s=E1bado, 13 de outubro de 2007 09:33
> Para: Andr=E9 Mac=E1rio Barros
> Cc: dev...@li...
> Assunto: Re: [Dev-C++] Programming with multiple modules/files
>
>
> //global.h
> #ifndef _GLOBAL_H
> #define _GLOBAL_H
>
> #ifdef MAIN
> #define EXTERN
> #else
> #define EXTERN extern
> #endif
>
> EXTERN int a;
> EXTERN int b;
> ...
>
> #endif // _GLOBAL_H
>
> // main.c
> #define MAIN=09=09// Want all global variables in this file.
> #include "global.h"
>
> int main(void) {
> ...
> }
>
> // other.c
> #include "global.h"=09// Get info about all global variables
>
> /pwm
>
> On Sat, 13 Oct 2007, Andr=E9 Mac=E1rio Barros wrote:
>
> > Dear users,
> >
> > Please consider the following project-file
> > cenario:
> > ///file1.c///////
> > #include "global.h"
> > main()
> > {
> > func_x();
> > a =3D 100;
> > ...
> > }
> > ///file2.c///////
> > extern int a;
> > func_x()
> > {
> > ...
> > a=3D300;
> > }
> > ///global.h///////
> > int a;
> > void func_x(void);
> > ///////////
> >
> > My problem is:
> > 1) I have a lot of file2.c-type files (files with only
> > function definitions), a lot of functions
> > and several "extern" variable declarations;
> > 2) Trying to include global.h in file2.c too, avoids
> > the need of declaring all extern variables. But this
> > action generates warnings of duplicate includes (2 or
> > more include global.h);
> > My question is:
> > Is there any way to manipulate several file2.c-type files
> > without declaring each extern variable?
> >
> > Regards
> > Andre Barros
> >
> >
> >
> > No virus found in this outgoing message.
> > Checked by AVG Free Edition.
> > Version: 7.5.488 / Virus Database: 269.14.9/1068 - Release Date:
> 13/10/2007
> > 10:15
> >
> >
> > -----------------------------------------------------------------------=
--
> > This SF.net email is sponsored by: Splunk Inc.
> > Still grepping through log files to find problems? Stop.
> > Now Search log events and configuration files using AJAX and a browser.
> > Download your FREE copy of Splunk now >> http://get.splunk.com/
> > _______________________________________________
> > Dev-cpp-users mailing list
> > Dev...@li...
> > TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
> >
>
> No virus found in this incoming message.
> Checked by AVG Free Edition.
> Version: 7.5.488 / Virus Database: 269.14.9/1068 - Release Date: 13/10/20=
07
> 10:15
>
> No virus found in this outgoing message.
> Checked by AVG Free Edition.
> Version: 7.5.488 / Virus Database: 269.14.10/1070 - Release Date: 14/10/2=
007
> 09:22
>
>
> -------------------------------------------------------------------------
> This SF.net email is sponsored by: Splunk Inc.
> Still grepping through log files to find problems? Stop.
> Now Search log events and configuration files using AJAX and a browser.
> Download your FREE copy of Splunk now >> http://get.splunk.com/
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|