案例标签中的extern const变量 [英] extern const variable in case label
问题描述
你好NG
我有以下代码:
file1.h:
static const int iValue = 5;
< EOF>
file2.cpp
#include< iostream>
#include" file1.h"
int main(int args,char * argv [])
{
switch(args){
case iValue:
std :: cout<< Hello \ n;
}
}
< EOF>
这可以很好地作为常量iValue的值。
编译file2.cpp中的main() - 函数时就知道了。
我还有一些包含file1.h的cpp文件。因此我有一份
" iValue"在每个翻译单元中,因为它具有内部链接。在使用这些目标文件(翻译单元)制作的
可执行文件中,我有
该变量的几个副本;都具有相同的价值。
现在,我想改变iValue的链接。 to extern以便
只是我的可执行文件中此常量的一个符号。
当我更改关键字static时extern我有一个问题,
符号在我的可执行文件中不止一次定义。
当我只声明变量iValue时在file1.h中使用外部链接
(" extern const int iValue;")并在一个名为file1.cpp的新文件中定义
(" extern const) iValue = 5;),然后我对多个
定义没有问题,但我的函数main()中有一个新问题,因为值为
的 iValue"编译file2.cpp时不知道。
你有什么建议我如何编译我的所有代码而没有
多于一个符号 iValue"在我的程序?或者,我想要达到的是什么
?
提前感谢您的所有答案,
Chris
Hello NG
I have the following code:
file1.h:
static const int iValue = 5;
<EOF>
file2.cpp
#include <iostream>
#include "file1.h"
int main(int args, char* argv[])
{
switch(args) {
case iValue:
std::cout << "Hello\n";
}
}
<EOF>
This works fine as the value of the constant "iValue" is known when
compiling the main()-function in file2.cpp.
I have some more cpp-files which include file1.h. Therefore I have a copy of
"iValue" in each translation unit as it has internal linkage. In the
executable which is made with these object files (translation units) I have
several copies of that variable; all having the same value.
Now, I would like to change the linkage of "iValue" to extern so that there
is only one symbol for this constant in my executable.
When I change the keyword "static" to "extern" I have the problem that the
symbol is more than once defined in my executable.
When I only declare the variable "iValue" in file1.h with external linkage
("extern const int iValue;") and define it in a new file called file1.cpp
("extern const iValue = 5;"), then I have no problem with multiple
definitions but I have a new problem in my function main() because the value
of "iValue" is not known when compiling file2.cpp.
Do you have any suggestions how I can compile all my code without having
more than one symbol for "iValue" in my program? Or is it not possible what
I am trying to reach?
Thanks for all your answers in advance,
Chris
推荐答案
10月2日,5:23 * pm,Christian Meier < chris@no_spam.comwrote:
On Oct 2, 5:23*pm, "Christian Meier" <chris@no_spam.comwrote:
Hello NG
我有以下代码:
file1.h:
static const int iValue = 5;
< EOF>
< br $>
file2.cpp
#include< iostream>
#include" file1.h"
int main(int args,char * argv [])
{
* * switch(args){
* *案例iValue:
* * * * std :: cout<< Hello \ n;
* *}
}
< EOF>
这可以正常作为常量iValue的值。
编译file2.cpp中的main() - 函数时就知道了。
我还有一些包含file1.h的cpp文件。因此我有一份副本
" iValue"在每个翻译单元中,因为它具有内部链接。在使用这些目标文件(翻译单元)制作的
可执行文件中,我有
该变量的几个副本;都具有相同的价值。
现在,我想改变iValue的链接。 to extern以便
只是我的可执行文件中此常量的一个符号。
当我更改关键字static时extern我有一个问题,
符号在我的可执行文件中不止一次定义。
当我只声明变量iValue时在file1.h中使用外部链接
(" extern const int iValue;")并在一个名为file1.cpp的新文件中定义
(" extern const) iValue = 5;),然后我对多个
定义没有问题,但我的函数main()中有一个新问题,因为值为
的 iValue"编译file2.cpp时不知道。
你有什么建议我如何编译我的所有代码而没有
多于一个符号 iValue"在我的程序?或者,我想要达到的是什么
?
提前感谢您的所有答案,
Chris
Hello NG
I have the following code:
file1.h:
static const int iValue = 5;
<EOF>
file2.cpp
#include <iostream>
#include "file1.h"
int main(int args, char* argv[])
{
* * switch(args) {
* * case iValue:
* * * * std::cout << "Hello\n";
* * }
}
<EOF>
This works fine as the value of the constant "iValue" is known when
compiling the main()-function in file2.cpp.
I have some more cpp-files which include file1.h. Therefore I have a copyof
"iValue" in each translation unit as it has internal linkage. In the
executable which is made with these object files (translation units) I have
several copies of that variable; all having the same value.
Now, I would like to change the linkage of "iValue" to extern so that there
is only one symbol for this constant in my executable.
When I change the keyword "static" to "extern" I have the problem that the
symbol is more than once defined in my executable.
When I only declare the variable "iValue" in file1.h with external linkage
("extern const int iValue;") and define it in a new file called file1.cpp
("extern const iValue = 5;"), then I have no problem with multiple
definitions but I have a new problem in my function main() because the value
of "iValue" is not known when compiling file2.cpp.
Do you have any suggestions how I can compile all my code without having
more than one symbol for "iValue" in my program? Or is it not possible what
I am trying to reach?
Thanks for all your answers in advance,
Chris
well。我想你应该记住,案例值只能是
常量积分值。
well. I think you should keep in mind that the case value can only be
constant integral values.
好吧。我认为你应该记住,案例值只能是
well. I think you should keep in mind that the case value can only be
常量积分值。
constant integral values.
你对此有何意义?不是extern const int iValue a
常数积分值?
What do you exactly mean with this? Isn''t "extern const int iValue" a
constant integral value?
Christian Meier写道:
Christian Meier wrote:
你呢有任何建议我如何编译我的所有代码
而没有多个符号用于iValue在我的程序中?
或者我想要达到的目标是不可能的?
Do you have any suggestions how I can compile all my code
without having more than one symbol for "iValue" in my program?
Or is it not possible what I am trying to reach?
这个怎么样?
enum {iValue = int(5)};
马丁
-
Quidquid latine scriptum est,altum videtur。
How about this?
enum { iValue = int(5) };
Martin
--
Quidquid latine scriptum est, altum videtur.
这篇关于案例标签中的extern const变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!