473,748 Members | 9,599 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

extern const variable in case label

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
Oct 2 '08 #1
21 2750
On Oct 2, 5:23*pm, "Christian Meier" <chris@no_spam. comwrote:
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. I think you should keep in mind that the case value can only be
constant integral values.
Oct 2 '08 #2
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?
Oct 2 '08 #3
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.
Oct 2 '08 #4
"Martin Eisenberg" <ma************ **@udo.eduwrote :
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.
Oct 2 '08 #5
On Oct 2, 11:23 am, "Christian Meier" <chris@no_spam. comwrote:
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:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Oct 2 '08 #6
"James Kanze" <ja*********@gm ail.comwrote:
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.
Oct 2 '08 #7
On Oct 2, 5:33 pm, "Christian Meier" <chris@no_spam. comwrote:
"James Kanze" <james.ka...@gm ail.comwrote:
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:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Oct 2 '08 #8
On 10ÔÂ2ÈÕ, ÏÂÎç5ʱ23·Ö, "Christian Meier" <chris@no_spam. comwrote:
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?
IIRC, just

const int iValue = 5;

no 'static', no 'extern'

--
Best Regards
Barry
Oct 3 '08 #9
"James Kanze" <ja*********@gm ail.comwrote:
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>
Oct 3 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
10655
by: Rolf S. Arvidson | last post by:
Be kind, a newb question here. I don't understand why I get a compile-time error from the following: I define a variable in main.cpp as such double x = 1.; int _tmain(int argc, _TCHAR* argv){ ...; }
4
2593
by: John Ratliff | last post by:
I have a few global variables in my app. They are defined in the main application class file and declared (extern) in a header which can be included by anyone who would want to use these variables. Two of the globals are pointers which are initialized at runtime. The others are classes whose values are initialized in the global space. When I had just the pointers, I didn't need to include the extern header in the defining class file. But...
10
6190
by: Mark A. Gibbs | last post by:
I have a question about mixing C and C++. In a C++ translation unit, I want to define a function with internal linkage and C calling convention. Here's a sample of what I want to do: // main.cpp // This is defined in a C module extern "C" void fake_qsort(void*, std::size_t, std::size_t, int (*compare)(const void*, const void*));
18
4283
by: tweak | last post by:
What's the best way to use extern when using multiplefiles that is easiest to maintain? Is it best to declare: extern int a; in a header file and include the header file in all files except where it's defined.
19
3859
by: ccwork | last post by:
Hi all, I am reading "C: A Reference Manual" 4th ed and I get lost for the "extern". It says that global object without specifying the storage-class specifier will have "extern" as the default storage-class specifier. My (little) C experience tells me that an object with "extern" is to let the linker knows that the object is referencing the object defined in somewhere, and this "somewhere" object does not have the storage-class specifier...
5
16607
by: siliconwafer | last post by:
Hi all, I wanted to know that is use of extern keyword mandatory in case of global variables and functions used in other source files? i.e consider a following piece of code from MSDN explaining extern storage class: /****************************************************************** SOURCE FILE ONE *******************************************************************/ extern int i; /* Reference to i, defined below */
17
4931
by: Tapeesh | last post by:
I would like to know what is the expected behaviour of C compilers when an extern decleration is intialized. When the following code is compiled using gcc //File extern.c int arr ; int a ;
7
3481
by: MikeF | last post by:
Group, I have a variable declared in 'main.c' as: const unsigned int x = 1; I have another module which uses this same variable, I've tried to declare as:
2
2904
by: Shraddha | last post by:
When I read about volatile keyword....while explaning there was an declaration like... extern const a; But as a rule const shoule be initialised where it is defined...but we do ot initialise extern where we declare it....So what is exactly happening here?
0
8830
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9541
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9321
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9247
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8242
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4602
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4874
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.