C++: Not #including files?
I'm trying to make my program as small as possible... Currently it's down to 17.8 KB, but I'd like to get it smaller (I know, I'm insane).
Is there a way to not use the "#include" directive to include the entire header, but rather to just load only the stuff I need? For example, I just want to include 'sprintf' and 'printf' from stdio.h, not the entire header. Is this possible? Or am I misunderstanding some fundamental element here? Thanks in advance! |
Quote:
What are you doing to make your program smaller? Are you using -Os and stripping it? |
Ah, so basically it already puts in the bare minimum? And if I don't include unnecessary headers, then that's about as much as I can do as far as includes go?
Anyway, what is -Os, and how do I strip it? (I think I know what stripping it is, just not how to do it) Edit: I figured out what -Os is. Thanks for mentioning that. The size is now 17.3kb. I will do some reading on stripping a binary. OK, I got it to 6.00 KB by stripping it. |
Oh, and -fwhole-program can help as well, but you may as well just make sure you've declared all your functions and data except main() to be static.
|
there's nothing to prevent you declaring printf yourself.
the linker will probably find it. though I haven't tried it. 17.8Kb, how big is your hard drive? mine is 500,000,000 Kb. ;-) |
Quote:
Quote:
Oh, quick question, is C generally faster than C++, or vice versa, or about equal? I do know the program always comes out much smaller when written in C, or such is my experience at least. I know C++ is more flexible/extensive and probably more stable/safe. |
Quote:
"Header files provide declarations not definitions, and hence won't make your program larger or smaller" That's not to say that the compiler somehow "optimizes" header references - it means compile time headers are UNRELATED to program size! Quote:
Quote:
'Hope that helps .. PSM |
Quote:
|
Quote:
<cut and paste> i.e. Code:
//#include <stdio.h> |
a C program is (almost always) also a C++ program.
so they are identical speed. basically C++ is blisteringly fast. it's a compiled language. today I've been playing about. I can load the book don quixote (428,905 words) as strings, counting word occurences into a large map. copy that map into a vector to enable a different sorting. sort that vector on word count. print out the results redirected into a file 0.58 seconds C++ is not slow. I could probably beat that if I memory mapped the file rather than reading from cin |
The header files simply tell the compiler that the functions exist so that it won't complain, they do NOT contain the code that makes up the function!!!
It only contains the prototypes of printf(), etc., not the code itself. During compilation, the code is linked with the standard C library, this is closer to what you're interested in. |
I must have gotten the idea from (dot) in shell scripting... I believe '.' does include the entire contents what what it's referencing into memory when the script is executed.
Anyway, basically, putting the prototype of printf directly in my source would be == to using #include <stdio.h>? But either way it's pointless, right? |
Quote:
I'm glad I'm getting into C++, I was wary at first because I've heard some bad things about it, but I haven't really liked any other languages, not "real" ones anyway, I love shell scripting. I don't mind if it's hard to learn or whatever, I like the challenge. And it pays off that it's so solid, and making good solid programs. |
Quote:
|
Thank you very much, everyone! I got all the info I needed, and then some.
I'll mark it as solved now. Thanks again! |
All times are GMT -5. The time now is 03:44 PM. |