📄 global-declarations.html
字号:
<html lang="en">
<head>
<title>Using the GNU Compiler Collection (GCC)</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="Using the GNU Compiler Collection (GCC)">
<meta name="generator" content="makeinfo 4.3">
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home">
<!--
Copyright © 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
<p>Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.2 or
any later version published by the Free Software Foundation; with the
Invariant Sections being "GNU General Public License" and "Funding
Free Software", the Front-Cover texts being (a) (see below), and with
the Back-Cover Texts being (b) (see below). A copy of the license is
included in the section entitled "GNU Free Documentation License".
<p>(a) The FSF's Front-Cover Text is:
<p>A GNU Manual
<p>(b) The FSF's Back-Cover Text is:
<p>You have freedom to copy and modify this GNU Manual, like GNU
software. Copies published by the Free Software Foundation raise
funds for GNU development.-->
</head>
<body>
<div class="node">
<p>
Node:<a name="Global%20Declarations">Global Declarations</a>,
Next:<a rel="next" accesskey="n" href="VMS-Misc.html#VMS%20Misc">VMS Misc</a>,
Up:<a rel="up" accesskey="u" href="VMS.html#VMS">VMS</a>
<hr><br>
</div>
<h3 class="section">Global Declarations and VMS</h3>
<p>GCC does not provide the <code>globalref</code>, <code>globaldef</code> and
<code>globalvalue</code> keywords of VAX-C. You can get the same effect with
an obscure feature of GAS, the GNU assembler. (This requires GAS
version 1.39 or later.) The following macros allow you to use this
feature in a fairly natural way:
<pre class="smallexample"> #ifdef __GNUC__
#define GLOBALREF(TYPE,NAME) \
TYPE NAME \
asm ("_$$PsectAttributes_GLOBALSYMBOL$$" #NAME)
#define GLOBALDEF(TYPE,NAME,VALUE) \
TYPE NAME \
asm ("_$$PsectAttributes_GLOBALSYMBOL$$" #NAME) \
= VALUE
#define GLOBALVALUEREF(TYPE,NAME) \
const TYPE NAME[1] \
asm ("_$$PsectAttributes_GLOBALVALUE$$" #NAME)
#define GLOBALVALUEDEF(TYPE,NAME,VALUE) \
const TYPE NAME[1] \
asm ("_$$PsectAttributes_GLOBALVALUE$$" #NAME) \
= {VALUE}
#else
#define GLOBALREF(TYPE,NAME) \
globalref TYPE NAME
#define GLOBALDEF(TYPE,NAME,VALUE) \
globaldef TYPE NAME = VALUE
#define GLOBALVALUEDEF(TYPE,NAME,VALUE) \
globalvalue TYPE NAME = VALUE
#define GLOBALVALUEREF(TYPE,NAME) \
globalvalue TYPE NAME
#endif
</pre>
<p>(The <code>_$$PsectAttributes_GLOBALSYMBOL</code> prefix at the start of the
name is removed by the assembler, after it has modified the attributes
of the symbol). These macros are provided in the VMS binaries
distribution in a header file <code>GNU_HACKS.H</code>. An example of the
usage is:
<pre class="example"> GLOBALREF (int, ijk);
GLOBALDEF (int, jkl, 0);
</pre>
<p>The macros <code>GLOBALREF</code> and <code>GLOBALDEF</code> cannot be used
straightforwardly for arrays, since there is no way to insert the array
dimension into the declaration at the right place. However, you can
declare an array with these macros if you first define a typedef for the
array type, like this:
<pre class="example"> typedef int intvector[10];
GLOBALREF (intvector, foo);
</pre>
<p>Array and structure initializers will also break the macros; you can
define the initializer to be a macro of its own, or you can expand the
<code>GLOBALDEF</code> macro by hand. You may find a case where you wish to
use the <code>GLOBALDEF</code> macro with a large array, but you are not
interested in explicitly initializing each element of the array. In
such cases you can use an initializer like: <code>{0,}</code>, which will
initialize the entire array to <code>0</code>.
<p>A shortcoming of this implementation is that a variable declared with
<code>GLOBALVALUEREF</code> or <code>GLOBALVALUEDEF</code> is always an array. For
example, the declaration:
<pre class="example"> GLOBALVALUEREF(int, ijk);
</pre>
<p>declares the variable <code>ijk</code> as an array of type <code>int [1]</code>.
This is done because a globalvalue is actually a constant; its "value"
is what the linker would normally consider an address. That is not how
an integer value works in C, but it is how an array works. So treating
the symbol as an array name gives consistent results--with the
exception that the value seems to have the wrong type. <strong>Don't
try to access an element of the array.</strong> It doesn't have any elements.
The array "address" may not be the address of actual storage.
<p>The fact that the symbol is an array may lead to warnings where the
variable is used. Insert type casts to avoid the warnings. Here is an
example; it takes advantage of the ISO C feature allowing macros that
expand to use the same name as the macro itself.
<pre class="example"> GLOBALVALUEREF (int, ss$_normal);
GLOBALVALUEDEF (int, xyzzy,123);
#ifdef __GNUC__
#define ss$_normal ((int) ss$_normal)
#define xyzzy ((int) xyzzy)
#endif
</pre>
<p>Don't use <code>globaldef</code> or <code>globalref</code> with a variable whose
type is an enumeration type; this is not implemented. Instead, make the
variable an integer, and use a <code>globalvaluedef</code> for each of the
enumeration values. An example of this would be:
<pre class="example"> #ifdef __GNUC__
GLOBALDEF (int, color, 0);
GLOBALVALUEDEF (int, RED, 0);
GLOBALVALUEDEF (int, BLUE, 1);
GLOBALVALUEDEF (int, GREEN, 3);
#else
enum globaldef color {RED, BLUE, GREEN = 3};
#endif
</pre>
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -