public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "rwitmer at xmission dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/45082]  New: Static const signed int class member causes undefined symbol.
Date: Mon, 26 Jul 2010 15:25:00 -0000	[thread overview]
Message-ID: <bug-45082-19484@http.gcc.gnu.org/bugzilla/> (raw)

A class that has a static const signed int data member when passed to a
function as a const int& parameter will cause the data member to be an
undefined symbol.  If the static const signed int data member is re-typed as
const unsigned int the error goes away.  I've verified this on g++ 3.4.6 and
4.1.2. The following code example illustrates the error.

#include <stdio.h>

static const signed int GLOBAL_BUFF_SIZE=30;

class myclass
{
  public:

    static const          int BUFF_SIZE = 20;  // Causes linker error.
  //static const   signed int BUFF_SIZE = 20;  // Causes linker error.
  //static const unsigned int BUFF_SIZE = 20;  // No     linker error.

    static const          int ANOTHER_BUFF_SIZE;

    int myArray[BUFF_SIZE];

/*  Linker error.
% g++ static2.cpp
Undefined                       first referenced
 symbol                             in file
myclass::BUFF_SIZE                  /var/tmp//cc2IHYwP.o
ld: fatal: Symbol referencing errors. No output written to a.out
collect2: ld returned 1 exit status
% g++ --version
g++ (GCC) 3.4.6
and
g++ (GCC) 4.1.2
% ccppc --version                            
ccppc (Wind River VxWorks G++ DWARF-EH 4.1-131) 4.1.2
*/
};

// This solves the linker problem.  
// Not an option for me.  I need to dimension an array with BUFF_SIZE.
const int myclass::ANOTHER_BUFF_SIZE=20;

// Function that takes a constant reference to an integer.
// When a static const signed int is passed to this function
// that was defined in a class, you get the linker error.
// If that static const is declared unsigned int, you dont get the error.
// When the reference is to a global static, there is no error.
void funky(const int& in)
{
  printf("What came into funky() was %d\n", in);
}

int main()
{
  printf("%d\n", myclass::BUFF_SIZE);  // This doesn't cause the error.
  funky(myclass::BUFF_SIZE);           // This line causes the error.
  funky(myclass::ANOTHER_BUFF_SIZE);   // This doesn't cause the error.
  funky(GLOBAL_BUFF_SIZE);             // This doesn't cause the error.
}


-- 
           Summary: Static const signed int class member causes undefined
                    symbol.
           Product: gcc
           Version: 4.1.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: rwitmer at xmission dot com


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=45082


             reply	other threads:[~2010-07-26 15:25 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-07-26 15:25 rwitmer at xmission dot com [this message]
2010-07-26 15:32 ` [Bug c++/45082] " jakub at gcc dot gnu dot org
2010-07-26 15:59 ` rwitmer at xmission dot com
2010-07-26 16:33 ` redi at gcc dot gnu dot org
2010-07-26 16:37 ` redi at gcc dot gnu dot org
2010-07-27 15:27 ` rwitmer at xmission dot com
2010-07-27 23:55 ` redi at gcc dot gnu dot org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-45082-19484@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).