LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 06-16-2010, 02:37 AM   #1
ryan858
Member
 
Registered: Feb 2009
Distribution: Slackware64-14.1
Posts: 43

Rep: Reputation: 17
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!
 
Old 06-16-2010, 03:11 AM   #2
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 139Reputation: 139
Quote:
Originally Posted by ryan858 View Post
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?
 
Old 06-16-2010, 03:36 AM   #3
ryan858
Member
 
Registered: Feb 2009
Distribution: Slackware64-14.1
Posts: 43

Original Poster
Rep: Reputation: 17
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.
 
Old 06-16-2010, 05:06 AM   #4
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 139Reputation: 139
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.
 
Old 06-16-2010, 06:01 AM   #5
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,516

Rep: Reputation: 239Reputation: 239Reputation: 239
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.
;-)
 
Old 06-18-2010, 01:54 AM   #6
ryan858
Member
 
Registered: Feb 2009
Distribution: Slackware64-14.1
Posts: 43

Original Poster
Rep: Reputation: 17
Quote:
Originally Posted by bigearsbilly View Post
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 View Post
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.
 
Old 06-18-2010, 02:08 AM   #7
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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
 
Old 06-18-2010, 04:43 AM   #8
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 139Reputation: 139
Quote:
Originally Posted by ryan858 View Post
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.
 
Old 06-18-2010, 12:24 PM   #9
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,516

Rep: Reputation: 239Reputation: 239Reputation: 239
Quote:
Originally Posted by ryan858 View Post
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");
}
 
Old 06-18-2010, 12:32 PM   #10
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,516

Rep: Reputation: 239Reputation: 239Reputation: 239
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.
 
Old 06-18-2010, 12:43 PM   #11
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
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.
 
Old 06-19-2010, 01:55 PM   #12
ryan858
Member
 
Registered: Feb 2009
Distribution: Slackware64-14.1
Posts: 43

Original Poster
Rep: Reputation: 17
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?
 
Old 06-19-2010, 02:07 PM   #13
ryan858
Member
 
Registered: Feb 2009
Distribution: Slackware64-14.1
Posts: 43

Original Poster
Rep: Reputation: 17
Quote:
Originally Posted by bigearsbilly View Post
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.
 
Old 06-19-2010, 02:21 PM   #14
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by ryan858 View Post
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.
 
Old 06-20-2010, 04:54 PM   #15
ryan858
Member
 
Registered: Feb 2009
Distribution: Slackware64-14.1
Posts: 43

Original Poster
Rep: Reputation: 17
Thank you very much, everyone! I got all the info I needed, and then some.

I'll mark it as solved now.

Thanks again!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Including c++ header files cbabbage Programming 6 10-28-2009 10:36 PM
undelete folder including files p41elvis Linux - General 6 10-20-2006 03:56 AM
Including header files and source files for classes Feenix Programming 8 09-28-2005 11:53 AM
Including files and permissions kidestranged Linux - General 2 07-27-2004 08:31 PM
including files with Kdevelop shyam_d_sundar Linux - Newbie 1 03-14-2004 02:54 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:20 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration