[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

extern const variable in case label

Christian Meier

10/2/2008 9:24:00 AM

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


21 Answers

thomas

10/2/2008 9:50:00 AM

0

On Oct 2, 5:23 pm, "Christian Meier" <chris@no_spam.com> wrote:
> 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

well. I think you should keep in mind that the case value can only be
constant integral values.

Christian Meier

10/2/2008 10:06:00 AM

0

> well. I think you should keep in mind that the case value can only be
> constant integral values.

What do you exactly mean with this? Isn't "extern const int iValue" a
constant integral value?


Martin Eisenberg

10/2/2008 11:52:00 AM

0

Christian Meier wrote:

> 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?

How about this?

enum { iValue = int(5) };


Martin

--
Quidquid latine scriptum est, altum videtur.

Christian Meier

10/2/2008 12:17:00 PM

0

"Martin Eisenberg" <martin.eisenberg@udo.edu> wrote:
> Christian Meier wrote:
>
>> 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?
>
> How about this?
>
> enum { iValue = int(5) };
>

Thanks for your answer!
Ok, you're right, that could be a solution for "int".
To tell a bit more of the truth, I have these constants for quite all
built-in types (double, long int, short...) and IIRC enum values must have
type int.


James Kanze

10/2/2008 1:30:00 PM

0

On Oct 2, 11:23 am, "Christian Meier" <chris@no_spam.com> wrote:
> 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.

Maybe. Maybe not.

> 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?

Do you have some valid reason for wanting iValue to have the
same address in every translation unit? It seems strange to me
to give an int identity, especially if you're not going to
change it. And otherwise, what's the problem with what you have
written initially?

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Christian Meier

10/2/2008 3:33:00 PM

0

"James Kanze" <james.kanze@gmail.com> wrote:

> Do you have some valid reason for wanting iValue to have the
> same address in every translation unit?

No...

> It seems strange to me to give an int identity, especially if you're not
> going to
> change it.

What else would you use for a global integer constant? Would you use a
#define?

> And otherwise, what's the problem with what you have written initially?

With my compiler and linker I have these symbols over 10 000 times in my
executable and I was able to reduce the program size a bit with changing
these constants to extern which are not used in case labels.


James Kanze

10/2/2008 6:39:00 PM

0

On Oct 2, 5:33 pm, "Christian Meier" <chris@no_spam.com> wrote:
> "James Kanze" <james.ka...@gmail.com> wrote:
> > Do you have some valid reason for wanting iValue to have the
> > same address in every translation unit?

> No...

> > It seems strange to me to give an int identity, especially
> > if you're not going to change it.

> What else would you use for a global integer constant? Would you use a
> #define?

No. Just a static int const, like you did originally.

> > And otherwise, what's the problem with what you have written
> > initially?

> With my compiler and linker I have these symbols over 10 000
> times in my executable and I was able to reduce the program
> size a bit with changing these constants to extern which are
> not used in case labels.

That is strange. I've yet to see a compiler where they'd take
any space at all, as long as their address wasn't taken.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Barry

10/3/2008 2:57:00 AM

0

On 10?2?, ??5?23?, "Christian Meier" <chris@no_spam.com> wrote:
> 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?
>

IIRC, just

const int iValue = 5;

no 'static', no 'extern'

--
Best Regards
Barry

Christian Meier

10/3/2008 5:50:00 AM

0

"James Kanze" <james.kanze@gmail.com> wrote:
> > > It seems strange to me to give an int identity, especially
> > > if you're not going to change it.

> > What else would you use for a global integer constant? Would you use a
> > #define?

> No. Just a static int const, like you did originally.

Ok thanks. Probably I will not change the code so it remains a static const
int.


> > With my compiler and linker I have these symbols over 10 000
> > times in my executable and I was able to reduce the program
> > size a bit with changing these constants to extern which are
> > not used in case labels.

> That is strange. I've yet to see a compiler where they'd take
> any space at all, as long as their address wasn't taken.

Eventually my assumption was wrong about the more than 10 000 symbols for
this variable.... And I have not checked whether the address is taken.


<Offtopic: UNIX Commands and Compiler version>

But the command "nm MyExecutable | grep iValue | wc -l" has the output
"11059" at this moment.
We use GCC 4.1.0...

</Offtopic>


Christian Meier

10/3/2008 5:51:00 AM

0

> IIRC, just
> const int iValue = 5;
> no 'static', no 'extern'

IIRC, constants in the file scope have internal linkage (in C++).