Const symbols suppressed

Discussion in 'C++' started by PlayDough, Jan 18, 2008.

  1. PlayDough

    PlayDough Guest

    I think this is something about C++ that I don't understand, and in
    versions of g++ prior to 4.1.1, hasn't been a problem.

    Say I have this basic file:

    test.cpp:
    const char test_str[] = "abcd";

    And I compile this. I'd expect test_str to be exported so other
    modules can use it. So, I do nm (for GNU) and dumpbin (for Visual
    Studio), and get....nothing.

    The test_str is not exported. In fact, dumping the output (objdump
    and dumpbin) shows nothing. And dumping the headers no readonly data
    section (.rodata for GNU, .rdata for VS). No output at all.

    But change things up. Change the filename to test.c and recompile.
    Now test_str is exported, and is placed in the .rodata/.rdata
    section. Exactly as expected.

    The only fix I have found for this is to do one of two things.

    test.cpp:
    extern const char test_str[];
    const char test_str[] = "abcd";

    or

    test.cpp:
    extern const char test_str[] = "abcd";

    I dislike the latter case, since we only use extern with declarations,
    and leave off the extern for allocations. That is, we'll put the
    extern statement in a header, and leave off the extern in the source
    file.

    So, only with the extern keyword does the symbol get exported.

    Is this a C++ feature that has recently been implemented in the GNU
    tools? We frequently have been doing something of the sort:

    test.h:
    extern const char test_str[];

    void some_func(int, int);

    test.cpp:
    const char test_str[];

    void some_func(int x, int y)
    {
    ...
    }

    Other modules include test.h and can now reference test_str. And we
    normally don't include test.h into test.cpp because it isn't
    necesssary all of the time (though not a bad idea, especially if you
    want to verify prototypes match the functions). But now it appears
    necessary.

    Can somebody clarify thie functionality?

    Thanks,
    Pete
     
    PlayDough, Jan 18, 2008
    #1
    1. Advertising

  2. PlayDough

    PlayDough Guest

    I finally figured out the proper search terms so that I could find the
    answer to this question.

    Now I understand that const symbols, by default, have internal
    linkage, unlike C. And apparently, g++ was broken prior to 4.1.1.

    Sorry to clutter things.

    Pete
     
    PlayDough, Jan 18, 2008
    #2
    1. Advertising

  3. PlayDough

    Jack Klein Guest

    On Fri, 18 Jan 2008 08:38:37 -0800 (PST), PlayDough <>
    wrote in comp.lang.c++:

    > I finally figured out the proper search terms so that I could find the
    > answer to this question.
    >
    > Now I understand that const symbols, by default, have internal
    > linkage, unlike C. And apparently, g++ was broken prior to 4.1.1.


    Not broken, just omitting a permissible, but not required,
    optimization.

    Since it has internal linkage, it can only be referred to in the
    current translation unit. Other translation units could refer to the
    char array if code in the current TU passed or returned a pointer to
    the start or some other member of the array, but to do this there
    would need to be an expression that referenced the array name to
    create such a pointer.

    If the compiler sees that the current TU does not reference an object
    with internal linkage, including a reference to create a pointer to
    it, it knows that the array cannot actually be accessed by the program
    at all.

    Some C compilers will do the same thing if the const char array is
    defined with the static keyword as well.

    > Sorry to clutter things.


    No problem, it is one of the decisions that creates a relatively
    silent change from C in C++. Some regard it as an unfortunate choice,
    others agree like it.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Jan 19, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    11
    Views:
    1,143
  2. Javier
    Replies:
    2
    Views:
    604
    James Kanze
    Sep 4, 2007
  3. Saeed

    SPACE is suppressed

    Saeed, Oct 23, 2008, in forum: HTML
    Replies:
    3
    Views:
    547
    Raymond SCHMIT
    Oct 23, 2008
  4. Saeed

    SPACES suppressed

    Saeed, Oct 23, 2008, in forum: HTML
    Replies:
    0
    Views:
    419
    Saeed
    Oct 23, 2008
  5. RichardOnRails
    Replies:
    4
    Views:
    200
    RichardOnRails
    Sep 27, 2010
Loading...

Share This Page