Initialize Variables of a Header File

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ramon

    Initialize Variables of a Header File

    Is there a way to initialize the variables (or other data) of a header
    file by using a function (similar to main() function)??

    Thankx
  • William Pursell

    #2
    Re: Initialize Variables of a Header File

    On Mar 15, 4:02 pm, Ramon <ramif...@yahoo .co.ukwrote:
    Is there a way to initialize the variables (or other data) of a header
    file by using a function (similar to main() function)??
    I'm not sure I understand your question. IMO, variables
    should only be declared in a header, and not defined,
    so it makes little sense to attempt to initialize them.
    What prevents you from doing:

    int x;
    int main( void )
    {
    initialize_x();
    ...


    I get the impression that you are trying instead to do:
    /* myfile.h */
    int x = initialize_x();

    and this is a very bad idea, indeed, since header
    files should be able to be included in multiple
    translation units...do you want the initialize_x()
    function to run multiple times?

    What exactly are you trying to do?

    Comment

    • Ben Pfaff

      #3
      Re: Initialize Variables of a Header File

      Ramon <ramif_47@yahoo .co.ukwrites:
      Is there a way to initialize the variables (or other data) of a header
      file by using a function (similar to main() function)??
      Sure. Write a function to initialize your variables, then call
      it.
      --
      char a[]="\n .CJacehknorstu" ;int putchar(int);in t main(void){unsi gned long b[]
      ={0x67dffdff,0x 9aa9aa6a,0xa77f fda9,0x7da6aa6a ,0xa67f6aaa,0xa a9aa9f6,0x11f6} ,*p
      =b,i=24;for(;p+ =!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
      2:{i++;if(i)bre ak;else default:continu e;if(0)case 1:putchar(a[i&15]);break;}}}

      Comment

      • Ramon

        #4
        Re: Initialize Variables of a Header File

        William Pursell wrote:
        On Mar 15, 4:02 pm, Ramon <ramif...@yahoo .co.ukwrote:
        >Is there a way to initialize the variables (or other data) of a header
        >file by using a function (similar to main() function)??
        >
        I'm not sure I understand your question. IMO, variables
        should only be declared in a header, and not defined,
        so it makes little sense to attempt to initialize them.
        What prevents you from doing:
        >
        int x;
        int main( void )
        {
        initialize_x();
        ...
        >
        >
        I get the impression that you are trying instead to do:
        /* myfile.h */
        int x = initialize_x();
        >
        and this is a very bad idea, indeed, since header
        files should be able to be included in multiple
        translation units...do you want the initialize_x()
        function to run multiple times?
        >
        What exactly are you trying to do?
        >
        Sorry for not being clear.

        I've tried to initialize variables in a header file by doing something
        like this:


        #ifndef HANDSHAKING_H_
        #define HANDSHAKING_H_
        ..
        ..
        ..
        int nextHS = 0; // location of next element of handshakeList
        ..
        ..
        ..
        #endif /*HANDSHAKING_H_ */


        By doing this, the linker complains and apparently it is not acceptable
        in gcc (version 4.1.1-21).

        My question is:
        Can I write a function that is called /automatically/ by the compiler
        and that initializes all my variables that are in the header file
        (somewhat similar to main() function of a normal C program) ?

        And if I can write such a function, how can I implement it?

        Thankx in advance!

        Comment

        • Harald van =?UTF-8?b?RMSzaw==?=

          #5
          Re: Initialize Variables of a Header File

          On Sat, 15 Mar 2008 17:48:43 +0100, Ramon wrote:
          Sorry for not being clear.
          >
          I've tried to initialize variables in a header file by doing something
          like this:
          >
          >
          #ifndef HANDSHAKING_H_
          #define HANDSHAKING_H_
          .
          .
          .
          int nextHS = 0; // location of next element of handshakeList .
          .
          .
          #endif /*HANDSHAKING_H_ */
          So when you have two files that include that header, you have two files
          that define nextHS.
          By doing this, the linker complains and apparently it is not acceptable
          in gcc (version 4.1.1-21).
          You should usually post the exact error message. In this case, the
          problem is apparent from the code itself, but when it's not, the error
          message can clarify a lot.
          My question is:
          Can I write a function that is called /automatically/ by the compiler
          and that initializes all my variables that are in the header file
          (somewhat similar to main() function of a normal C program) ?
          No. But you don't need one, see below.
          And if I can write such a function, how can I implement it?
          In your handshaking.h, put

          extern int nextHS;

          In _one_ source file, put

          int nextHS = 0;

          The linker tells you that you have multiple definitions of nextHS, so
          this way, you make sure you only have one definition.

          Comment

          • Ben Pfaff

            #6
            Re: Initialize Variables of a Header File

            Ramon <ramif_47@yahoo .co.ukwrites:
            I've tried to initialize variables in a header file by doing something
            like this:
            >
            >
            #ifndef HANDSHAKING_H_
            #define HANDSHAKING_H_
            .
            .
            .
            int nextHS = 0; // location of next element of handshakeList
            .
            .
            .
            #endif /*HANDSHAKING_H_ */
            This is not the way you do it. Instead, write "extern int
            nextHS;" in the header file and "int nextHS = 0;" in exactly one
            C source file (not header file).
            --
            "I ran it on my DeathStation 9000 and demons flew out of my nose." --Kaz

            Comment

            • William Pursell

              #7
              Re: Initialize Variables of a Header File

              On 15 Mar, 16:48, Ramon <ramif...@yahoo .co.ukwrote:
              William Pursell wrote:
              On Mar 15, 4:02 pm, Ramon <ramif...@yahoo .co.ukwrote:
              Is there a way to initialize the variables (or other data) of a header
              file by using a function (similar to main() function)??
              >
              I'm not sure I understand your question. IMO, variables
              should only be declared in a header, and not defined,
              so it makes little sense to attempt to initialize them.
              What prevents you from doing:
              >
              int x;
              int main( void )
              {
              initialize_x();
              ...
              >
              I get the impression that you are trying instead to do:
              /* myfile.h */
              int x = initialize_x();
              >
              and this is a very bad idea, indeed, since header
              files should be able to be included in multiple
              translation units...do you want the initialize_x()
              function to run multiple times?
              >
              What exactly are you trying to do?
              >
              Sorry for not being clear.
              >
              I've tried to initialize variables in a header file by doing something
              like this:
              >
              #ifndef HANDSHAKING_H_
              #define HANDSHAKING_H_
              .
              .
              .
              int nextHS = 0; // location of next element of handshakeList
              .
              .
              .
              #endif /*HANDSHAKING_H_ */
              >
              By doing this, the linker complains and apparently it is not acceptable
              in gcc (version 4.1.1-21).
              >
              My question is:
              Can I write a function that is called /automatically/ by the compiler
              and that initializes all my variables that are in the header file
              (somewhat similar to main() function of a normal C program) ?
              >
              And if I can write such a function, how can I implement it?
              AFAIK, you cannot do that in standard C. However, gcc
              allows you to identify functions which are called prior
              to main():

              void __attribute__(( constructor))
              initialize (void)
              {
              /* code that executes before main */
              }

              IMO, you are better off just calling the initialization
              function yourself...it makes for clearer code and there's
              no gain whatsoever in using tricks like this.

              However, you keep using the phrase "variables ... in
              the header file". Only declarations should appear in the
              header file; the definition should not be in the header.
              (eg, in the header, you should have the word "extern"
              on each variable declaration.)

              There is a common style to use headers simply
              as textual replacement to minimize the amount
              of cruft at the start of the .c file, but that's
              not really what they're for. If you have a lot
              of global variables and you are trying to hide
              the declarations/definitions, consider something
              like:

              a.h:
              extern int x;

              a.c:
              #include "a.h"
              int main(void) { /* use x */ }

              x.c:
              int x;

              $ gcc -c x.c && gcc -c a.c && gcc a.o x.o

              Comment

              Working...