const has internal linkage in C++??

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

    const has internal linkage in C++??

    const has internal linkage in C++, but external linkage in C.
    Am I right ?

    But, linker reports multiply-defined error if the
    following header is included in multiple .cpp files.


    // test_const.h
    #ifndef HEADER_TEST
    #define HEADER_TEST

    const char* TEST_CONST = "Testing" ;

    class some
    {
    // members
    } ;

    #endif


    When I checked the symbol table, it was reported that,
    [Index] Value Size Type Bind Other Shndx Name
    [4] | 0| 4|OBJT |GLOB |0 |3 |TEST_CONST


    Why?
  • Ivan Vecerina

    #2
    Re: const has internal linkage in C++??

    "qazmlp" <qazmlp1209@red iffmail.com> wrote in message
    news:db9bbf31.0 309020210.3207c f6d@posting.goo gle.com...
    | const has internal linkage in C++, but external linkage in C.
    | Am I right ?
    Yes.

    | But, linker reports multiply-defined error if the
    | following header is included in multiple .cpp files.
    ....
    | const char* TEST_CONST = "Testing" ;

    Note that this is not a constant variable.
    It is a non-const pointer to statically allocated constant
    characters;

    You should try:
    const char* const TEST_CONST = "Testing" ;
    Or:
    const char TEST_CONST[] = "Testing" ;

    hth,
    Ivan
    --



    Comment

    • sks_cpp

      #3
      Re: const has internal linkage in C++??

      "Ivan Vecerina" <ivec@myrealbox .com> wrote in message
      news:3f547194$1 @news.swissonli ne.ch...[color=blue]
      > "qazmlp" <qazmlp1209@red iffmail.com> wrote in message
      > news:db9bbf31.0 309020210.3207c f6d@posting.goo gle.com...
      > | const has internal linkage in C++, but external linkage in C.
      > | Am I right ?
      > Yes.[/color]

      What exactly does that mean? (internal linkage vs external linkage)


      Comment

      • Attila Feher

        #4
        Re: const has internal linkage in C++??

        sks_cpp wrote:
        [SNIP][color=blue]
        > What exactly does that mean? (internal linkage vs external linkage)[/color]

        Lame description:

        External linkage: has a name which others can use (an exported name in the
        object file generated from the source code).

        Internal linkage: the compiler will not give an externally visible name for
        the thing, so others (other .c or .cpp files) will not be able to see it by
        name.

        By name is important, since I can still give its address. The most famous
        thing with internal linkage is statci global variables. Their name is only
        visible in the source file where they are defined.

        --
        Attila aka WW


        Comment

        • ramesh

          #5
          Re: const has internal linkage in C++??

          "Ivan Vecerina" <ivec@myrealbox .com> wrote in message news:<3f547194$ 1@news.swissonl ine.ch>...[color=blue]
          > "qazmlp" <qazmlp1209@red iffmail.com> wrote in message
          > news:db9bbf31.0 309020210.3207c f6d@posting.goo gle.com...
          > | const has internal linkage in C++, but external linkage in C.
          > | Am I right ?
          > Yes.
          >
          > | But, linker reports multiply-defined error if the
          > | following header is included in multiple .cpp files.
          > ...
          > | const char* TEST_CONST = "Testing" ;
          >
          > Note that this is not a constant variable.
          > It is a non-const pointer to statically allocated constant
          > characters;
          >
          > You should try:
          > const char* const TEST_CONST = "Testing" ;
          > Or:
          > const char TEST_CONST[] = "Testing" ;
          >
          > hth,
          > Ivan[/color]

          Hi,
          Can you please clarify me on external and internal linkage.
          regards,
          Ramesh

          Comment

          • llewelly

            #6
            Re: const has internal linkage in C++??


            [comp.unix.solar is removed.]

            qazmlp1209@redi ffmail.com (qazmlp) writes:
            [color=blue]
            > const has internal linkage in C++, but external linkage in C.
            > Am I right ?[/color]

            No. A long time ago, in the days of ARM C++, const had internal
            linkage, but that was changed in the early years of
            standardization . I can't recall the last time I used a compiler
            that didn't implement the 'new' const has external linkage rule.

            Comment

            • Andrey Tarasevich

              #7
              Re: const has internal linkage in C++??

              llewelly wrote:[color=blue]
              > ...[color=green]
              >> const has internal linkage in C++, but external linkage in C.
              >> Am I right ?[/color]
              >
              > No. A long time ago, in the days of ARM C++, const had internal
              > linkage, but that was changed in the early years of
              > standardization . I can't recall the last time I used a compiler
              > that didn't implement the 'new' const has external linkage rule.[/color]

              No. By default 'const' objects have internal linkage in C++. The problem
              with OP's code is that the object is not 'const'.

              --
              Best regards,
              Andrey Tarasevich
              Brainbench C and C++ Programming MVP

              Comment

              • llewelly

                #8
                Re: const has internal linkage in C++??

                Andrey Tarasevich <andreytarasevi ch@hotmail.com> writes:
                [color=blue]
                > llewelly wrote:[color=green]
                >> ...[color=darkred]
                >>> const has internal linkage in C++, but external linkage in C.
                >>> Am I right ?[/color]
                >>
                >> No. A long time ago, in the days of ARM C++, const had internal
                >> linkage, but that was changed in the early years of
                >> standardization . I can't recall the last time I used a compiler
                >> that didn't implement the 'new' const has external linkage rule.[/color]
                >
                > No. By default 'const' objects have internal linkage in C++.[/color]

                You are correct - I was thinking of inline functions, for some reason,
                which once had internal linkage but now have external linkage.

                Comment

                • Ivan Vecerina

                  #9
                  Re: const has internal linkage in C++??

                  "ramesh" <kramesh99@indi a.com> wrote in message
                  news:af454d71.0 309020607.5d038 a6e@posting.goo gle.com...
                  | "Ivan Vecerina" <ivec@myrealbox .com> wrote in message
                  news:<3f547194$ 1@news.swissonl ine.ch>...
                  | > "qazmlp" <qazmlp1209@red iffmail.com> wrote in message
                  | > news:db9bbf31.0 309020210.3207c f6d@posting.goo gle.com...
                  | > | const has internal linkage in C++, but external linkage in C.
                  | > | Am I right ?
                  | > Yes.
                  .....
                  | > hth,
                  | > Ivan
                  |
                  | Hi,
                  | Can you please clarify me on external and internal linkage.

                  Well, Attila probably gave an explanation, but here's my throw at it.
                  External linkage = accessible from other compilation units (.cpp files).

                  Given the definition of a global variable with external linkage,
                  such as:
                  namespace n { int g; }
                  any other translation unit may access it by declaring the variable
                  again:
                  namespace n { extern int g; }
                  NB: for global variable declaration, 'extern' must be used to
                  explicitly specify that this is a declaration, but not
                  a definition(/"allocation ") of the variable.

                  It also means that if another source file/library was to define a
                  global variable of the same name, an error will occur (typically
                  reported as a name collision at link time):
                  namespace n { int g; } // in a second file --> link error.


                  Static variables, as well as constants in namespace(=glob al) scope,
                  have internal linkage. It means that two translation units can
                  use a global variable that has the same name: the two variables
                  are allocated independently, or duplicated, and will not collide.


                  Internal/external linkage exists also for functions (e.g. static
                  non-member functions have internal linkage) and for classes (a
                  class defined in function scope is also currently said to have
                  internal linkage: it will not be accessible from the outside,
                  or collide with an identically named class in another function).
                  [ for classes IIRC, this might change in the next C++ standard,
                  to address the issue described in the next paragraph ].


                  Other than these usage aspects, the only real limitation of objects
                  that have internal linkage is that they cannot be used as template
                  parameters (~because of how link names are generated for templates).
                  This means that a predicate class used as a template parameter
                  cannot currently be defined within a function.


                  This limitation of objects with internal linkage is the main reason
                  why 'static' for global objects is considered to be deprecated.
                  The modern alternative is to use anonymous namespaces:
                  namespace { int g; } // external linkage, but never collides
                  During the compilation of each file, a unique 'prefix' is generated
                  for objects defined in an anonymous namespace.
                  They have external linkage, but will never "collide" because
                  of the unique prefix.



                  I hope you'll find these explanations useful,
                  Ivan
                  --



                  Comment

                  • Ivan Vecerina

                    #10
                    Re: const has internal linkage in C++??

                    "Ivan Vecerina" <ivec@myrealbox .com> wrote in message
                    news:3f55dbbc$1 @news.swissonli ne.ch...
                    | Internal/external linkage exists also for functions (e.g. static
                    | non-member functions have internal linkage) and for classes (a
                    | class defined in function scope is also currently said to have
                    | internal linkage: it will not be accessible from the outside,
                    | or collide with an identically named class in another function).
                    Errata: actually, it is said to have no linkage.
                    To quote the standard, there are actually 3 types of linkage --
                    3.5/2:
                    A name is said to have linkage when it might denote the same object,
                    reference, function, type, template, namespace or value as a name introduced
                    by a declaration in another scope:

                    — When a name has external linkage, the entity it denotes can be referred to
                    by names from scopes of other translation units or from other scopes of the
                    same translation unit.

                    — When a name has internal linkage, the entity it denotes can be referred to
                    by names from other scopes in the same translation unit.

                    — When a name has no linkage, the entity it denotes cannot be referred to by
                    names from other scopes.



                    Ivan
                    --




                    Comment

                    Working...