Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
|
06-16-2010, 02:37 AM
|
#1
|
Member
Registered: Feb 2009
Distribution: Slackware64-14.1
Posts: 43
Rep:
|
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!
|
|
|
06-16-2010, 03:11 AM
|
#2
|
Member
Registered: Oct 2009
Posts: 467
Rep:
|
Quote:
Originally Posted by ryan858
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?
|
|
|
06-16-2010, 03:36 AM
|
#3
|
Member
Registered: Feb 2009
Distribution: Slackware64-14.1
Posts: 43
Original Poster
Rep:
|
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.
Last edited by ryan858; 06-16-2010 at 04:21 AM.
|
|
|
06-16-2010, 05:06 AM
|
#4
|
Member
Registered: Oct 2009
Posts: 467
Rep:
|
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.
|
|
|
06-16-2010, 06:01 AM
|
#5
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,516
|
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.
;-)
|
|
|
06-18-2010, 01:54 AM
|
#6
|
Member
Registered: Feb 2009
Distribution: Slackware64-14.1
Posts: 43
Original Poster
Rep:
|
Quote:
Originally Posted by bigearsbilly
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
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.
Last edited by ryan858; 06-18-2010 at 02:04 AM.
|
|
|
06-18-2010, 02:08 AM
|
#7
|
LQ Guru
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Rep:
|
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
|
|
|
06-18-2010, 04:43 AM
|
#8
|
Member
Registered: Oct 2009
Posts: 467
Rep:
|
Quote:
Originally Posted by ryan858
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.
|
|
|
06-18-2010, 12:24 PM
|
#9
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,516
|
Quote:
Originally Posted by ryan858
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");
}
|
|
|
06-18-2010, 12:32 PM
|
#10
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,516
|
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
Last edited by bigearsbilly; 06-18-2010 at 12:34 PM.
|
|
|
06-18-2010, 12:43 PM
|
#11
|
LQ 5k Club
Registered: Sep 2009
Posts: 6,443
|
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.
|
|
|
06-19-2010, 01:55 PM
|
#12
|
Member
Registered: Feb 2009
Distribution: Slackware64-14.1
Posts: 43
Original Poster
Rep:
|
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?
|
|
|
06-19-2010, 02:07 PM
|
#13
|
Member
Registered: Feb 2009
Distribution: Slackware64-14.1
Posts: 43
Original Poster
Rep:
|
Quote:
Originally Posted by bigearsbilly
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.
|
|
|
06-19-2010, 02:21 PM
|
#14
|
LQ 5k Club
Registered: Sep 2009
Posts: 6,443
|
Quote:
Originally Posted by ryan858
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.
|
|
|
06-20-2010, 04:54 PM
|
#15
|
Member
Registered: Feb 2009
Distribution: Slackware64-14.1
Posts: 43
Original Poster
Rep:
|
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 09:20 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|