c++ 跨多个文件的全局变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9709116/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
c++ Global Variables Across Multiple Files
提问by fenkerbb
I have some code spread over three files and I would like to use a fourth "gloabls" file to store some physical constants such as the value of pi. This would avoid repetitive definitions of pi = 4*atan(1.0)
. After poking around, what I've tried is creating a global header file:
我有一些代码分布在三个文件中,我想使用第四个“global”文件来存储一些物理常数,例如 pi 的值。这将避免 的重复定义pi = 4*atan(1.0)
。摸索之后,我尝试过的是创建一个全局头文件:
/*globals.h*/
extern double g_pi;
and a global cpp file:
和一个全局 cpp 文件:
/*globals.cpp*/
#include "math.h"
#include "globals.h"
double g_pi = 4*atan(1.0);
Then I include these file in my main files:
然后我将这些文件包含在我的主文件中:
/*mainFile.cpp*/
//Include math and other libraries
#include globals.h"
int main() {
/*
....
*/
double x = 2*g_pi
/*
....
*/
}
This gives me an undefined reference error to g_pi. I'm using a g++ compiler on Ubuntu. Hopefully its a simple fix! Your suggestions are much appreciated. If more details are necessary I'll be happy to supply them.
这给了我一个未定义的 g_pi 引用错误。我在 Ubuntu 上使用 g++ 编译器。希望它是一个简单的修复!非常感谢您的建议。如果需要更多详细信息,我将很乐意提供。
回答by drahnr
You could simply use M_PI from the include (there are other constants too).
您可以简单地使用包含中的 M_PI(也有其他常量)。
Edit: your setup is correct. I got a working minmal example:
编辑:您的设置是正确的。我得到了一个工作最小的例子:
globals.h
globals.h
extern double g_tst;
globals.cpp
全局变量
#include "globals.h"
double g_tst = 4.0;
main.cpp
主程序
#include "globals.h"
#include <stdio.h>
#include <stdlib.h>
int main()
{
fprintf (stderr, "g_tst = %lf \n", g_tst);
return 0;
}
The problem is within your buildsystem
问题出在您的构建系统中
See wikipedia
参见维基百科
回答by Crymson
I think the problem is that you've got #include gobals.h instead of #include globals.h. This would give you the undefined references because it isn't inserting globals.h. The C++ precompiler doesn't fail when it can't find a header file. Instead you get an undefined reference message at compilation.
我认为问题在于你有#include gobals.h 而不是#include globals.h。这会给你未定义的引用,因为它没有插入 globals.h。C++ 预编译器在找不到头文件时不会失败。相反,您在编译时会收到一条未定义的参考消息。
回答by Matthias
The order of linking might be the problem. Try to link the global object file as the last one.
链接顺序可能是问题所在。尝试将全局对象文件链接为最后一个。
回答by Smittii
g_pi musst not be declared extern in one translation unit. You could use a small #define for this
g_pi 不得在一个翻译单元中声明为 extern。您可以为此使用一个小的#define
in globals.cpp
在 globals.cpp 中
#define MY_EXTERN_CPP
in /*globals.h
在 /*globals.h
#ifdef MY_EXTERN_CPP
#define MY_CONFIGURATION_EXTERN
#else
#define MY_CONFIGURATION_EXTERN extern
#endif
MY_CONFIGURATION_EXTERN double g_pi;
so g_pi will be extern in all translation units you include it except globals.cpp
所以 g_pi 在所有包含它的翻译单元中都是 extern,除了 globals.cpp
回答by sirian_ye
I had the same problem. I was using CMAKE, the problem was I didn't put my globals.cpp file into CMAKE executable. This problem should be something similar, nothing wrong with the implementation itself.
我有同样的问题。我正在使用 CMAKE,问题是我没有将 globals.cpp 文件放入 CMAKE 可执行文件中。这个问题应该是类似的,实现本身没有问题。