Basic/simple question regarding header file

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

    #1

    Basic/simple question regarding header file

    If in one of the header file, a constant is defined:

    const UINT UM_CAL_DONE = func();

    and it is included in several other .cpp file. Does the func()
    excecuted many times? If it is included in multithread application.
    data in the func() must be synchonized?

  • kathy

    #2
    Re: Basic/simple question regarding header file

    My goal is to use func() to generate a constant which can be used by
    many threads or functions.

    Comment

    • Victor Bazarov

      #3
      Re: Basic/simple question regarding header file

      kathy wrote:[color=blue]
      > If in one of the header file, a constant is defined:
      >
      > const UINT UM_CAL_DONE = func();
      >
      > and it is included in several other .cpp file. Does the func()
      > excecuted many times?[/color]

      Yes, very likely. To prevent that, declare your constant 'extern'
      and only define (and initialise) it in a _single_ translation unit.
      [color=blue]
      > If it is included in multithread application.
      > data in the func() must be synchonized?[/color]

      That's beyond the scope of this newsgroup, but probably yes.

      V
      --
      Please remove capital As from my address when replying by mail

      Comment

      • Stephan Brönnimann

        #4
        Re: Basic/simple question regarding header file

        Victor Bazarov wrote:[color=blue]
        > kathy wrote:[color=green]
        > > If in one of the header file, a constant is defined:
        > >
        > > const UINT UM_CAL_DONE = func();
        > >
        > > and it is included in several other .cpp file. Does the func()
        > > excecuted many times?[/color]
        >
        > Yes, very likely. To prevent that, declare your constant 'extern'
        > and only define (and initialise) it in a _single_ translation unit.[/color]

        Just to prove Victors answer compile the following program with
        g++-3.3 -g -Wall -c -o try.o try.cc
        g++-3.3 -g -Wall -c -o foo.o foo.cc
        g++-3.3 -g -Wall -o try try.o foo.o

        try.h
        =====
        #ifndef TRY_H
        #define TRY_H

        int mkCint();
        const int cInt = mkCint();

        #endif

        try.cc
        ======
        #include "try.h"
        #include <iostream>

        int main()
        {
        std::cout << cInt << std::endl;
        }

        int mkCint()
        {
        std::cout << "mkCint()" << std::endl;
        return 2;
        }

        foo.cc
        ======
        #include "try.h"

        The output is
        UNIX> ./try
        mkCint()
        mkCint()
        2

        Note: the program segfaults if compiled with g++-3.4/g++-4.0 (I've just
        submitted a bug report). As work-around write to stdout in mkCint().

        Regards, Stephan

        Comment

        • Marco Wahl

          #5
          Re: Basic/simple question regarding header file

          >> > If in one of the header file, a constant is defined:[color=blue][color=green][color=darkred]
          >> >
          >> > const UINT UM_CAL_DONE = func();
          >> >
          >> > and it is included in several other .cpp file. Does the func()
          >> > excecuted many times?[/color]
          >>
          >> Yes, very likely. To prevent that, declare your constant 'extern'
          >> and only define (and initialise) it in a _single_ translation unit.[/color]
          >
          > Just to prove Victors answer compile the following program with
          > g++-3.3 -g -Wall -c -o try.o try.cc
          > g++-3.3 -g -Wall -c -o foo.o foo.cc
          > g++-3.3 -g -Wall -o try try.o foo.o
          >
          > try.h
          > =====
          > #ifndef TRY_H
          > #define TRY_H
          >
          > int mkCint();
          > const int cInt = mkCint();
          >
          > #endif
          >
          > try.cc
          > ======
          > #include "try.h"
          > #include <iostream>
          >
          > int main()
          > {
          > std::cout << cInt << std::endl;
          > }
          >
          > int mkCint()
          > {
          > std::cout << "mkCint()" << std::endl;
          > return 2;
          > }
          >
          > foo.cc
          > ======
          > #include "try.h"
          >
          > The output is
          > UNIX> ./try
          > mkCint()
          > mkCint()
          > 2[/color]

          Just to provide Stephans example following Victors answer:

          try.h
          =====

          #ifndef TRY_H
          #define TRY_H

          int mkCint();
          extern const int cInt;

          #endif


          try.cc
          ======

          #include "try.h"
          #include <iostream>

          const int cInt = mkCint();

          int main()
          {
          std::cout << cInt << std::endl;
          }

          int mkCint()
          {
          std::cout << "mkCint()" << std::endl;
          return 2;
          }


          foo.cc
          ======

          #include "try.h"


          The output is
          =============

          $ ./try
          mkCint()
          2
          [color=blue]
          > Note: the program segfaults if compiled with g++-3.4/g++-4.0 (I've just
          > submitted a bug report). As work-around write to stdout in mkCint().[/color]

          Approved for

          $ g++ --version
          g++ (GCC) 4.0.2 20050808 (prerelease) (Ubuntu 4.0.1-4ubuntu9)

          Comment

          • mark zhong

            #6
            Re: Basic/simple question regarding header file

            thank you
            "Marco Wahl <0@0>" <marco.wahl@gma il.com>
            ??????:11390502 80.690151.11030 @o13g2000cwo.go oglegroups.com. ..[color=blue][color=green][color=darkred]
            >>> > If in one of the header file, a constant is defined:
            >>> >
            >>> > const UINT UM_CAL_DONE = func();
            >>> >
            >>> > and it is included in several other .cpp file. Does the func()
            >>> > excecuted many times?
            >>>
            >>> Yes, very likely. To prevent that, declare your constant 'extern'
            >>> and only define (and initialise) it in a _single_ translation unit.[/color]
            >>
            >> Just to prove Victors answer compile the following program with
            >> g++-3.3 -g -Wall -c -o try.o try.cc
            >> g++-3.3 -g -Wall -c -o foo.o foo.cc
            >> g++-3.3 -g -Wall -o try try.o foo.o
            >>
            >> try.h
            >> =====
            >> #ifndef TRY_H
            >> #define TRY_H
            >>
            >> int mkCint();
            >> const int cInt = mkCint();
            >>
            >> #endif
            >>
            >> try.cc
            >> ======
            >> #include "try.h"
            >> #include <iostream>
            >>
            >> int main()
            >> {
            >> std::cout << cInt << std::endl;
            >> }
            >>
            >> int mkCint()
            >> {
            >> std::cout << "mkCint()" << std::endl;
            >> return 2;
            >> }
            >>
            >> foo.cc
            >> ======
            >> #include "try.h"
            >>
            >> The output is
            >> UNIX> ./try
            >> mkCint()
            >> mkCint()
            >> 2[/color]
            >
            > Just to provide Stephans example following Victors answer:
            >
            > try.h
            > =====
            >
            > #ifndef TRY_H
            > #define TRY_H
            >
            > int mkCint();
            > extern const int cInt;
            >
            > #endif
            >
            >
            > try.cc
            > ======
            >
            > #include "try.h"
            > #include <iostream>
            >
            > const int cInt = mkCint();
            >
            > int main()
            > {
            > std::cout << cInt << std::endl;
            > }
            >
            > int mkCint()
            > {
            > std::cout << "mkCint()" << std::endl;
            > return 2;
            > }
            >
            >
            > foo.cc
            > ======
            >
            > #include "try.h"
            >
            >
            > The output is
            > =============
            >
            > $ ./try
            > mkCint()
            > 2
            >[color=green]
            >> Note: the program segfaults if compiled with g++-3.4/g++-4.0 (I've just
            >> submitted a bug report). As work-around write to stdout in mkCint().[/color]
            >
            > Approved for
            >
            > $ g++ --version
            > g++ (GCC) 4.0.2 20050808 (prerelease) (Ubuntu 4.0.1-4ubuntu9)
            >[/color]


            Comment

            Working...