including a large constants file

J

john smith

Hi,

If I have a large constants file and include that in a .cpp file will the
executable become large?

That is if I include a file with a bunch of constants does the executable
include all of the constants or just
the constants that are used in the .cpp file?

I am wanting a way so that only the constants that are used are included in
the executable and not the whole file.
I am trying to reduce the size of my executable.

I had trouble verifying this.

Thanks alot for any advice.

Many thanks,

John
 
M

Mike Wahler

john smith said:
Hi,

If I have a large constants file and include that in a .cpp file will the
executable become large?

Define 'large'.
That is if I include a file with a bunch of constants does the executable
include all of the constants or just
the constants that are used in the .cpp file?

That depends entirely upon your compiler and platform.
I am wanting a way so that only the constants that are used are included in
the executable and not the whole file.

Again this depends upon your compiler, and also upon the
context in which they're used. Some might be stored in
your program's image, some might become 'immediate' operands
of machine instructions.
I am trying to reduce the size of my executable.

None of what you ask is addressed by the language, so
we cannot answer conclusively.
I had trouble verifying this.

Thanks alot for any advice.

Consult your compiler documentation, and/or search your
compiler's support resources.

-Mike
 
M

Mike Wahler

Mike Wahler said:
Define 'large'.


That depends entirely upon your compiler and platform.

I forgot to add: This also depends upon how you're defining
your constants. If you're defining const objects, what I
said before applies. If you're using #define, all that does
is give symbolic names to your values (it's merely a text-
substitution mechanism), and if you don't refer to the #defined
names, they should not enter into your code at all.

-Mike
 
E

E. Robert Tisdale

john said:
If I have a large constants file and include that in a .cpp file,
will the executable become large?

That is if I include a file with a bunch of constants,
does the executable include all of the constants?
Or just the constants that are used in the .cpp file?

I am wanting a way so that
only the constants that are used are included in the executable
and not the whole file.
I am trying to reduce the size of my executable.

cat f.cc
#include"physical.h"

using physical::unit::kilograms;
double v = 4.0*kilograms;
g++ -Wall -ansi -pedantic -O2 -S f.cc
cat f.s
.file "f.cc"
.globl v
.data
.align 8
.type v, @object
.size v, 8
v:
.long 0
.long 1074790400
.section .note.GNU-stack,"",@progbits
.ident "GCC: (GNU) 3.4.1"

You can search the comp.lang.c++ archives
for the physical.h header file.
It contains hundreds of constant definitions
but my GNU optimizing C++ compiler
g++ --version
g++ (GCC) 3.4.1

reserves space only for v!
 
E

E. Robert Tisdale

Mike said:
Define 'large'.


That depends entirely upon your compiler and platform.

And which optimization options you invoke.
Again this depends upon your compiler
and also upon the context in which they're used.
Some might be stored in your program's image,
some might become 'immediate' operands of machine instructions.


None of what you ask is addressed by the language
so we cannot answer conclusively.

No. that isn't quite right.
A good optimizing C++ compiler
will elide any constant definitions that it does not use.
A good optimizing C++ compiler
will combine any constants that it finds in numerical expressions
at compile time.
You should *not* cobble your code to compensate
for inferior implementations.
If you C++ compiler cannot perform these optimizations,
you should be shopping for a better C++ compiler.
Market forces and *not* ANSI/ISO standards
govern the quality of implementation.
Compiler developers must compete with each other for customers.
It is the programmers responsibility
to choose superior implementations
and driver inferior compiler developers out of business.
 
K

KTC

No. that isn't quite right.
A good optimizing C++ compiler
will elide any constant definitions that it does not use.
A good optimizing C++ compiler
will combine any constants that it finds in numerical expressions
at compile time.
You should *not* cobble your code to compensate
for inferior implementations.
If you C++ compiler cannot perform these optimizations,
you should be shopping for a better C++ compiler.
Market forces and *not* ANSI/ISO standards
govern the quality of implementation.
Compiler developers must compete with each other for customers.
It is the programmers responsibility
to choose superior implementations
and driver inferior compiler developers out of business.

I'm must be just getting confused with everything tonight. But
isn't Mike saying exactly what you're saying? That it's not the
standard that specify it but rather the implementation.

KTC
 
E

E. Robert Tisdale

KTC said:
I'm must be just getting confused with everything tonight.
But isn't Mike saying exactly what you're saying?

No.
I'm saying that we *can* answer conclusively.
That it's not the standard that specify it
but rather the implementation.

Mike Wahler and I agree on that part.
But the original poster, John Smith, requested,
"I am wanting a way so that
only the constants that are used are included in the executable
and not the whole file." I am saying that
that optimization is indeed what John Smith should expect
from a quality implementation.
He needs to find out whether or not his C++ compiler
can perform this optimization and, if it can't,
he needs to shop for a better C++ compiler.
Mike Wahler didn't say that. I don't know why.
 
K

KTC

No.
I'm saying that we *can* answer conclusively.


Mike Wahler and I agree on that part.
But the original poster, John Smith, requested,
"I am wanting a way so that
only the constants that are used are included in the executable
and not the whole file." I am saying that
that optimization is indeed what John Smith should expect
from a quality implementation.
He needs to find out whether or not his C++ compiler
can perform this optimization and, if it can't,
he needs to shop for a better C++ compiler.
Mike Wahler didn't say that. I don't know why.

Ah okay. The fact that it's half five in the morning and I haven't
slept yet might have had something to do with me not totally
understanding what you meant. =D
 
M

Mike Wahler

E. Robert Tisdale said:
No.
I'm saying that we *can* answer conclusively.


Mike Wahler and I agree on that part.
But the original poster, John Smith, requested,
"I am wanting a way so that
only the constants that are used are included in the executable
and not the whole file." I am saying that
that optimization is indeed what John Smith should expect
from a quality implementation.
He needs to find out whether or not his C++ compiler
can perform this optimization and, if it can't,
he needs to shop for a better C++ compiler.
Mike Wahler didn't say that. I don't know why.

Because here we talk about the language, not particular
implementations. Also, John did not provide any evidence
that his constants were the source of the 'bloat' anyway.
Perhaps it's something else.

-Mike
 
T

Thomas Matthews

john said:
Hi,

If I have a large constants file and include that in a .cpp file will the
executable become large?
[snip]

As Mike said, constants may be eliminated, part of the executable, or
reside in the data area.


I am trying to reduce the size of my executable.
Reorganizing constants may reduce the size of the sources but
may have little effect on the executable.

My primary method of reducing an executable is to:
** Reduce executable size if and only if the rest of the program
is working correctly, is robust, and either the customer is
complaining about the size or the executable is too big to
fit on the target platform.
1. Remove unused code.
2. Remove unwanted features and the code they occupy.
3. Combine common fragments into functions.
4. Change simple functions into inline macros.
These are functions whose contents take up as much executable
space as the call/return protocol.
5. Use assembly language functions to take advantage of special
processor instructions. Print out the compiler's assembly
language for the source function. Optimize it. Replace it
with yours only if there is a significant space savings.
I had trouble verifying this.

Thanks alot for any advice.

Many thanks,

John



--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,197
Messages
2,571,040
Members
47,634
Latest member
RonnyBoelk

Latest Threads

Top