LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 01-21-2002, 09:25 PM   #1
wsimmons
LQ Newbie
 
Registered: Sep 2001
Posts: 10

Rep: Reputation: 0
output file size Linux GNU gcc vs. DJGPP


I wrote the simplest program:

#include <stdio.h>

int main()
{
printf("test line one\n");
printf("test line two\n");
return 0;
}



and compiled it on my RH Linux 7.0 machine using gcc and on my Windows machine using DJGPP gcc. The Linux file was about 17000 bytes and the Windows file was about 81000 bytes. That's a big difference (both seem like huge programs for such a small function though)! Why? How, without going to assembly do I make the Windows files smaller? What can I cut out of the library (stdio.h and stdio.c; I assume they are both there) to cut out the excess?
 
Old 01-21-2002, 09:45 PM   #2
barazor
Member
 
Registered: Dec 2001
Distribution: slack 9.1
Posts: 44

Rep: Reputation: 15
i think it could be djgpp. when i used mingw32(other port of gcc) on windows, the programs were usually pretty small
 
Old 01-26-2002, 05:49 PM   #3
kervin
Member
 
Registered: Jan 2002
Location: Melbourne, FL.
Distribution: redhat
Posts: 168

Rep: Reputation: 30
program size is dependant on a lot of variables. Here are a few...

(i) Use the 'strip' command. This removes all symbols from your program. This means that debuggers will have a hard time debugging that binary, and core dumps won't say much, but the resulting binary will be considerabley less in size.

Using your helloworld example...
[kervin@bashful kervin]$ gcc hello.c
[kervin@bashful kervin]$ ls -la a.out
-rwxrwxr-x 1 kervin kervin 13718 Jan 26 18:38 a.out
[kervin@bashful kervin]$ strip a.out
[kervin@bashful kervin]$ ls -la a.out
-rwxrwxr-x 1 kervin kervin 3212 Jan 26 18:38 a.out

The stripped binariy is about 20% the original's size.

(ii) Given the choice of a shared library and a static library, gcc will use the shared. This acually may make programs bigger, depending on the size of the library being linked, but is almost always the right thing to do. This is because the overhead code of the dynamic linker, though small, might be bigger than the library being linked.

Still on the subject of dynamic libraries, see if you have dynamic versions of all the libraries you link against. if you specify your library using the '-l' option, eg. ' gcc -lmylib ...", then gcc will look for 'libmylib.so' then if it does not find that, it'll check for 'libmylib.a'. so make sure that the .so shared object version is available.

(iii) Check out the various gcc optimization switches in the gcc man page and other documention. I got slightly smaller code, even with hello world when I used the '-O' switch for optimization
 
Old 01-26-2002, 09:19 PM   #4
gui10
Member
 
Registered: Mar 2001
Distribution: enigma, slack8
Posts: 677

Rep: Reputation: 30
amazing! i tried it. there's a helluva difference!

but does this mean that the executable file will not work on another person's machine? i'm not clear of the implications. would appreciate if you could shed some light on this.
 
Old 01-27-2002, 09:29 PM   #5
kervin
Member
 
Registered: Jan 2002
Location: Melbourne, FL.
Distribution: redhat
Posts: 168

Rep: Reputation: 30
strip won't harm the code's portability, and actually, many of the programs installed on your system are probably stripped.

to find out if a particular file is stripped, use the 'file' command. eg. I tried the file command on 'ls' binary...

[kervin@bashful kervin]$ file /bin/ls
/bin/ls: ELF 32-bit LSB executable, Intel 80386, version 1, dynamically linked (uses shared libs), stripped
[kervin@bashful kervin]$

So you see that the 'ls' binary is stripped.

If you have a program that you would like to help out in, maybe by providing the developers with crash dump data using gdb debugger and maybe the resulting 'core' files, then you *don't* want to strip the binaries. Stripping removes the symbols that the debugger needs to figure out what went wrong. But if you do not intend to participate in development, then strip binaries are most likely ok.
 
Old 01-27-2002, 10:20 PM   #6
gui10
Member
 
Registered: Mar 2001
Distribution: enigma, slack8
Posts: 677

Rep: Reputation: 30
thanks kervin!
i learn new things everyday...
 
  


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
Specifying the output file for a gnu tool bnj Linux - Newbie 4 06-02-2005 07:52 AM
Introduction to GCC - The GNU Compiler Collection jeremy Programming 2 03-28-2005 04:17 AM
How to install GNU g++ and gcc compilers on Red Hat 9.0? naveenkoneru Linux - Newbie 1 07-20-2004 06:11 AM
where is GNU gcc compiler? karfei00 Linux - Newbie 5 06-16-2004 12:55 PM
GNU gcc 3 compiler Craneology Linux - Software 3 03-04-2003 04:35 AM

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

All times are GMT -5. The time now is 09:37 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration