Chapter 11. Known Causes of Trouble with GCC
You can partially avoid this problem by using the
Options That Control Optimization).
On AIX and other platforms without weak symbol support, templates need to be instantiated ex-
•
plicitly and symbols for static members of templates will not be generated.
On AIX, GCC scans object files and library archives for static constructors and destructors when
•
linking an application before the linker prunes unreferenced symbols. This is necessary to prevent
the AIX linker from mistakenly assuming that static constructor or destructor are unused and re-
moving them before the scanning can occur. All static constructors and destructors found will be
referenced even though the modules in which they occur may not be used by the program. This may
lead to both increased executable size and unexpected symbol references.
11.9. Common Misunderstandings with GNU C++
C++ is a complex language and an evolving one, and its standard definition (the ISO C++ standard)
was only recently completed. As a result, your C++ compiler may occasionally surprise you, even
when its behavior is correct. This section discusses some areas that frequently give rise to questions
of this sort.
11.9.1. Declare andDefine Static Members
When a class has static data members, it is not enough to declare the static member; you must also
define it. For example:
class Foo
{
...
void method();
static int bar;
};
This declaration only establishes that the class
tion named
Foo::method
to the ISO standard, you must supply an initializer in one (and only one) source file, such as:
int Foo::bar = 0;
Other C++ compilers may not correctly implement the standard behavior. As a result, when you switch
to
from one of these compilers, you may discover that a program that appeared to work correctly
g++
in fact does not conform to the standard:
that lack definitions.
11.9.2. Name lookup, templates, and accessing members of base classes
The C++ standard prescribes that all names that are not dependent on template parameters are bound
to their present definitions when parsing a template function or class.
are looked up at the point of instantiation. For example, consider
void foo(double);
struct A {
template
typename T
1. The C++ standard just uses the term "dependent" for names that depend on the type or value of template
parameters. This shorter term will also be used in the rest of this section.
Foo
. But you still need to define both
reports as undefined symbols any static data members
g++
-ffloat-store
has an
named
int
Foo::bar
and
method
1
Only names that are dependent
option (refer to Section 4.10
, and a member func-
elsewhere. According
bar
245
Need help?
Do you have a question about the ENTERPRISE LINUX 3 - USING GCC and is the answer not in the manual?
Questions and answers