简体   繁体   English

C ++变量extern声明

[英]C++ variable extern declarations

I have declared some constants in a header file: 我在头文件中声明了一些常量:

extern int  g_iShortSize1FrameEncoded=30;
extern int  g_iByteSize1FrameEncoded=(g_iShortSize1FrameEncoded*2);
extern int  g_iShortSize1FrameDecoded=960;
extern int  g_iByteSize1FrameDecoded=(g_iShortSize1FrameDecoded*2);

This is really convenient for me because I need to use these "constants" in various apps and change them often, and I want to do this only once so that I don't forget to change anything. 这对我来说真的很方便,因为我需要在各种应用程序中使用这些“常量”并经常进行更改,并且我只想执行一次,这样我就不会忘记进行任何更改。

It compiles fine. 它编译良好。

However my declaration is a bit "wild". 但是,我的声明有点“狂野”。

I have to trust the compiler will compile it in the right way. 我必须相信编译器会以正确的方式进行编译。

Is my approach fine anyway? 反正我的方法还好吗?

I will not change these values at runtime, only during development. 仅在开发期间,我不会在运行时更改这些值。

I have 3 different applications, and all consume / need these values. 我有3个不同的应用程序,并且都使用/需要这些值。

In the applications I simply wanted to include them as 在应用程序中,我只想将它们包括为

#include "..\..\myconstants.h"

No, it's not fine, and they are not constants. 不,这不好,它们也不是常数。 By initialising an extern variable in its declaration, the declaration becomes a definition. 通过在其声明中初始化extern变量,该声明将成为定义。 Since it has external linkage, it must obey the One Definition Rule and be defined just once; 由于它具有外部链接,因此必须遵守“一个定义规则”,并且只能被定义一次; but yours will be defined wherever the header is included. 但是您的将在包含标头的任何位置进行定义。

If they are constants, then make them so, with internal linkage: 如果它们是常量,则使用内部链接使其成为常量:

const int  g_iShortSize1FrameEncoded=30;
^^^^^

On the other hand you say you need to "change them often". 另一方面,您说您需要“经常更改”。 If you mean they actually change at runtime (rather than changing them by editing the initialiser and recompiling), then they can't be constants; 如果您是说它们实际上在运行时发生更改(而不是通过编辑初始化程序和重新编译来更改它们),那么它们就不能是常量; instead, you'll need to declare them in the header and define them in exactly one source file: 相反,您需要在标头中声明它们,并在一个源文件中定义它们:

// declarations in header
extern int g_iShortSize1FrameEncoded; // no initialiser

// definitions in source file
int g_iShortSize1FrameEncoded = 30;

In either case, the variables will be initialised in the order of their definitions; 无论哪种情况,变量都将按照其定义顺序进行初始化。 so as long as no value depends on a later variable's value, they will get the expected values. 因此只要没有值取决于后面的变量的值,它们就会获得期望值。

Usually you'd just put this in header file: 通常,您只需将其放在头文件中:

extern const int  g_iShortSize1FrameEncoded;
extern const int  g_iByteSize1FrameEncoded;
extern const int  g_iShortSize1FrameDecoded;
extern const int  g_iByteSize1FrameDecoded;

and this in .cpp file corresponding to that header: 与此对应的.cpp文件中的标头:

const int  g_iShortSize1FrameEncoded=30;
const int  g_iByteSize1FrameEncoded=(g_iShortSize1FrameEncoded*2);
const int  g_iShortSize1FrameDecoded=960;
const int  g_iByteSize1FrameDecoded=(g_iShortSize1FrameDecoded*2);

This way linker knows that variables are placed in one compilation unit and refers to them from other compilation units. 通过这种方式,链接器知道变量位于一个编译单元中,并从其他编译单元引用它们。 Also note the const keyword -- you wrote that they are constants. 还要注意const关键字-您写道它们是常量。

This kind of global variables is usually used like this: 这种全局变量通常是这样使用的:

some_header.h: some_header.h:

#ifndef SOME_HEADER_FILE_INCLUDED
#define SOME_HEADER_FILE_INCLUDED

extern int  g_iShortSize1FrameEncoded;
extern int  g_iByteSize1FrameEncoded;
extern int  g_iShortSize1FrameDecoded;
extern int  g_iByteSize1FrameDecoded;

#endif

main.cpp: main.cpp中:

#include "some_header.h"

int g_iShortSize1FrameEncoded = 30;
int g_iByteSize1FrameEncoded = (g_iShortSize1FrameEncoded * 2);
int g_iShortSize1FrameDecoded = 960;
int g_iByteSize1FrameDecoded = (g_iShortSize1FrameDecoded * 2);

void another(void);

int main(int argc, char **argv)
{
   another();
   return 0;
}

another.cpp: another.cpp:

#include "some_header.h"

void another(void)
{
    g_iShortSize1FrameEncoded = 50;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM