smart way to inlcude files in a project and also gobal variables 
Author Message
 smart way to inlcude files in a project and also gobal variables

Hi there,

Thanks for you resonse it gave me some good ideas on how to solve the
prolem. Heres what I havecome up with which we are using in our project
currently, which might be useful to you. As this also solves the problem of
initialising the varaiables also

main.c
----------

#define GLOBAL
#define MAIN_C
#include "header.h"

void main(void)
{

    c_Var=99;

Quote:
}

one.c
----------

#define ONE_C
#include "header.h"

void one(void)
{
    c_Var=1;

Quote:
}

two.c
----------
#define TWO_C
#include "header.h"

void two(void)
{
    c_Var=2;

Quote:
}

header.h
----------

#ifndef HEADER_H
#define HEADER_H

#ifdef GLOBAL
#define    BEGIN_DECLARATION
#else
#define     BEGIN_DECLARATION extern
#endif

#define    END_DECLARATION ;

#ifdef MAIN_C

    #ifndef GLOBAL_H
    #include "global.h"
    #endif

    #ifndef GLOBAL1_H
    #include "global1.h"
    #endif

#endif

#ifdef ONE_C

    #ifndef GLOBAL_H
    #include "global.h"
    #endif

    #ifndef GLOBAL1_H
    #include "global1.h"
    #endif

#endif

#ifdef TWO_C

    #ifndef GLOBAL_H
    #include "global.h"
    #endif
    #ifndef GLOBAL1_H
    #include "global1.h"
    #endif
#endif

#endif

global.h
----------

#ifndef GLOBAL_H
#define GLOBAL_H

    BEGIN_DECLARATION
        char c_Var
        #ifdef GLOBAL
            = 5
        #endif
    END_DECLARATION

#endif

global1.h
----------
#ifndef GLOBAL1_H
#define GLOBAL1_H

    BEGIN_DECLARATION
        char *s_Var
        #ifdef GLOBAL
            = "FDO"
        #endif
    END_DECLARATION

#endif

prototype.h
----------
#ifndef PROTOTYPE_H
#define PROTOTYPE_H

#ifdef MAIN_C
    #define LINK_MAIN
#else
    #define LINK_MAIN extern
#endif

#ifdef ONE_C
    #define LINK_ONE
#else
    #define LINK_ONE extern
#endif

#ifdef TWO_C
    #define LINK_TWO
#else
    #define LINK_TWO extern
#endif

    LINK_MAIN void one(void);
    LINK_ONE void one(void);
    LINK_TWO void one(void);

#endif
Regrads
--



Mon, 27 Jan 2003 03:00:00 GMT  
 smart way to inlcude files in a project and also gobal variables

Quote:

> void main(void)

int main(void), please, and be sure to return a value.

Quote:
>     #define LINK_MAIN extern

You don't need that, since external linkage is the default.

I have the impression that you're trying too hard.
The interface header can simply contain stuff like:
        extern int foo(char *);
        extern double param;
Every module can include that interface header, and
additionally define some of the things declared in it:
        #include "myAPI.h"
        int foo(char *s) {
                ...
        }
        double param = 5.0;
--



Tue, 28 Jan 2003 03:00:00 GMT  
 smart way to inlcude files in a project and also gobal variables

Quote:

> > void main(void)

> int main(void), please, and be sure to return a value.

I have heard that on some systems you have to declare main
like this:

int main(int argc, char * argv[])

Otherwise itll be a system-unfriendly program?

Honestly, is this right?

/Efraim
--



Wed, 29 Jan 2003 03:00:00 GMT  
 smart way to inlcude files in a project and also gobal variables

Quote:


> > int main(void), please, and be sure to return a value.

> I have heard that on some systems you have to declare main
> like this:

> int main(int argc, char * argv[])

> Otherwise itll be a system-unfriendly program?

> Honestly, is this right?

No, it's unnecessary restrictive.  The C Standard (5.1.2.2.1/1) states
that the two definitions for main are legal:

    int main(void) { ... }
    int main(int argc, char *argv[]) { ... }

and perhaps in some other implementation-defined (i.e., nonstandard, and
the implementation must document those extensions) manner.  Note the
latter prototype can be replaced with anything that's legal, such as

    typedef int Integer;
    typedef char Byte;
    int main(Integer first, Byte **second) { ... }

is also perfectly legal.

Specifically, main does not return void.  (This is by and large the same
in Standard C++, although the details are somewhat different.)

--

 __ San Jose, CA, US / 37 20 N 121 53 W / ICQ16063900 / &tSftDotIotE
/  \ You cannot step into the same river once.
\__/ Cratylus
    Alcyone Systems' CatCam / http://www.catcam.com/
 What do your pets do all day while you're at work?  Find out.
--



Fri, 31 Jan 2003 03:00:00 GMT  
 smart way to inlcude files in a project and also gobal variables

Quote:

> I have heard that on some systems you have to declare main
> like this:
> int main(int argc, char * argv[])

A conforming hosted implementation of C must accept either form.
Since the actual run-time startoff linkage to main has the form
with 2 parameters, that means that some implementations have to
handle the 0-parameter definition of main specially (differently
than if it had any other name).  This misfeature is intentional.
--



Fri, 31 Jan 2003 03:00:00 GMT  
 smart way to inlcude files in a project and also gobal variables
On 12 Aug 2000 13:51:20 GMT, "Efraim Mostrom"

Quote:

>I have heard that on some systems you have to declare main
>like this:

>int main(int argc, char * argv[])

>Otherwise itll be a system-unfriendly program?

>Honestly, is this right?

>/Efraim

It is entirely possible that some compiler(s) has not fully
implemented the standard and will not accept
        int main (void){...

That makes the statement true but it does not make it right.

<<Remove the del for email>>
--



Fri, 31 Jan 2003 03:00:00 GMT  
 smart way to inlcude files in a project and also gobal variables
Here we go again......

You use

        int main(int argc, char * argv[])

Where there are arguments and

        int main(void)

Where there are no arguments but

        void main(void)

should ONLY be used on self hosted single application systems  IE
embedded systems where there is no operating system to return to.   It
these systems there is often no provision for a return for main because it
can never happen!  


Quote:



>> > void main(void)

>> int main(void), please, and be sure to return a value.

>I have heard that on some systems you have to declare main
>like this:

>int main(int argc, char * argv[])

>Otherwise itll be a system-unfriendly program?

>Honestly, is this right?

>/Efraim

/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\ Chris Hills  Staffs  England    /\/\/\/\/\

\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
--



Fri, 31 Jan 2003 03:00:00 GMT  
 smart way to inlcude files in a project and also gobal variables
Quote:



> > > int main(void), please, and be sure to return a value.

> > I have heard that on some systems you have to declare main
> > like this:

> > int main(int argc, char * argv[])

> > Otherwise itll be a system-unfriendly program?

> > Honestly, is this right?

> No, it's unnecessary restrictive.  The C Standard (5.1.2.2.1/1) states
> that the two definitions for main are legal:

>     int main(void) { ... }
>     int main(int argc, char *argv[]) { ... }

> and perhaps in some other implementation-defined (i.e., nonstandard, and
> the implementation must document those extensions) manner.

<snip>

More specifically, the above two forms are guaranteed to be supported
by a compliant compiler.  Any other forms are not.

Quote:

> Specifically, main does not return void.  (This is by and large the same
> in Standard C++, although the details are somewhat different.)

I'm not sure what you're saying here.  The requirements in C++ are
the same as C :  support for the forms int main() and
int main(int, char *[]) are both required by the standard, but
any other form is not.
--



Fri, 31 Jan 2003 03:00:00 GMT  
 smart way to inlcude files in a project and also gobal variables

Quote:

> You use
>         int main(int argc, char * argv[])
> Where there are arguments and
>         int main(void)
> Where there are no arguments but
>         void main(void)
> should ONLY be used on self hosted single application systems  IE
> embedded systems where there is no operating system to return to.

No, in a hosted implementation there are always arguments
available for main.  It is up to the program whether to
declare them or not; if not, the compiler has to adjust
things so that the function entry works anyway.  Of course,
if the arguments are not declared then they cannot be
accessed (in any portable way).

The way that a freestanding implementation invokes a
program is defined in the implementation's documentation;
it doesn't have to be by invoking a function "main", and
even if it does start off by invoking "main", there might
be arguments and/or a return value, and it might differ
from the standard hosted interface.
--



Sat, 01 Feb 2003 10:37:08 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. Need gobal variable

2. How to declare a gobal variable in ATL project?

3. Global variable with a project with .c and .cpp files

4. are there better ways to read config file?

5. Gobal string

6. clone projects, make mult projects in bat file

7. Missing files in Smart Card SDK

8. what is the difference between static variable and gobal varibale.

9. Question: Best place to put global variables on a VS.NET C# project

10. Environment variables in project settings

11. Format of dsp files (Visual Studio project files)

12. Q: header files not shown in project file list

 

 
Powered by phpBB® Forum Software