LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   C++: Not #including files? (https://www.linuxquestions.org/questions/programming-9/c-not-including-files-814429/)

ryan858 06-16-2010 01:37 AM

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!

JohnGraham 06-16-2010 02:11 AM

Quote:

Originally Posted by ryan858 (Post 4005050)
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?

Header files provide declarations not definitions, and hence won't make your program larger or smaller. Not including a needed header will only result in bad. Also, in C++, you need declarations of classes you want to use.

What are you doing to make your program smaller? Are you using -Os and stripping it?

ryan858 06-16-2010 02:36 AM

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.

JohnGraham 06-16-2010 04:06 AM

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.

bigearsbilly 06-16-2010 05:01 AM

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.
;-)

ryan858 06-18-2010 12:54 AM

Quote:

Originally Posted by bigearsbilly (Post 4005205)
there's nothing to prevent you declaring printf yourself.
the linker will probably find it.
though I haven't tried it.

And how would I do this?

Quote:

Originally Posted by bigearsbilly (Post 4005205)
17.8Kb,
how big is your hard drive? mine is 500,000,000 Kb.
;-)

It's 80gb, and I have several other large drives installed... It's not so much a practical application, as it is a theoretical one. Though I will use some of these techniques for practical application of my programs. Smaller size is always better as long as it doesn't sacrifice speed (in most cases).

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.

paulsm4 06-18-2010 01:08 AM

Quote:

I'm trying to make my program as small as possible...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?
As JohnGraham already replied:
"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:

is C generally faster than C++
There's no technical reason why C++ needs to be any different than C.

Quote:

OK, I got it to 6.00 KB by stripping it.
As you probably learned from your reading, object files and binary executables contain a lot of "meta data" (for example, program symbols) that consume space. Running "strip" removes this metadata - at the expense of making runtime problems (aka " bugs" ;-)) more difficult to troubleshoot.

'Hope that helps .. PSM

JohnGraham 06-18-2010 03:43 AM

Quote:

Originally Posted by ryan858 (Post 4007192)
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.

Programmers can be fast or slow in any language - it depends both on how good the programmer is and, more importantly, if they're even trying to be fast. Most of the time, speed is something you can only gain if you sacrifice code clarity and quality, and a fast program is no good if it's buggy. Neither C or C++ is faster or slower, so long as you use it right.

bigearsbilly 06-18-2010 11:24 AM

Quote:

Originally Posted by ryan858 (Post 4007192)
And how would I do this?

man 3 printf or vi stdio.h
<cut and paste>

i.e.
Code:

//#include <stdio.h>
int printf(const char * format, ...);

int main (void)
{
        printf("hello\n");
}


bigearsbilly 06-18-2010 11:32 AM

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

MTK358 06-18-2010 11:43 AM

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.

ryan858 06-19-2010 12:55 PM

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?

ryan858 06-19-2010 01:07 PM

Quote:

Originally Posted by bigearsbilly (Post 4007727)
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

That's insane man!

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.

MTK358 06-19-2010 01:21 PM

Quote:

Originally Posted by ryan858 (Post 4008669)
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?

AFAIK Yes.

ryan858 06-20-2010 03:54 PM

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.