Game Engine Architecture

(Ben Green) #1
113

to bypass the compiler’s cost/benefi t analysis and control function inlining
directly.


3.2.2.3. Linkage


Every defi nition in C and C++ has a property known as linkage. A defi nition
with external linkage is visible to and can be referenced by translation units
other than the one in which it appears. A defi nition with internal linkage can
only be “seen” inside the translation unit in which it appears and thus cannot
be referenced by other translation units. We call this property linkage because
it dictates whether or not the linker is permitt ed to cross-reference the entity
in question. So, in a sense, linkage is the translation unit’s equivalent of the
public: and private: keywords in C++ class defi nitions.
By default, defi nitions have external linkage. The static keyword is
used to change a defi nition’s linkage to internal. Note that two or more identi-
cal static defi nitions in two or more diff erent .cpp fi les are considered to be
distinct entities by the linker (just as if they had been given diff erent names),
so they will not generate a “multiply defi ned symbol” error. Here are some
examples:


foo.cpp


// This variable can be used by other .cpp files
// (external linkage).
U32 gExternalVariable;

// This variable is only usable within foo.cpp (internal
// linkage).
static U32 gInternalVariable;
// This function can be called from other .cpp files
// (external linkage).
void externalFunction()
{
// ...
}
// This function can only be called from within foo.cpp
// (internal linkage).
static void internalFunction()
{
// ...
}

bar.cpp


// This declaration grants access to foo.cpp’s variable.
extern U32 gExternalVariable;

3.2. Data, Code, and Memory in C/C++

Free download pdf