Solved

Compiled size problem

Posted on 2004-03-22
6
249 Views
Last Modified: 2010-04-15
Hi,

I compiled the same C source in two linux machines, both machines are RedHat 9.0.

I installed the same GCC compiler and MySQL library.

But why the compiled size is very different ?

In machine 1, 18 KB .....

In machine 2, 180 KB .....

What could be wrong ?

Here is my compile script :

#!/bin/sh

echo "building $1"

gcc -o $1 $1.c -I/usr/include/mysql -L/usr/lib/mysql -lmysqlclient -lz

It's probably in the second machine, the gcc put all the library in the resulting binary ?

Anyone can give me any idea why this happen ?

Thanks
0
Comment
Question by:muis2002
6 Comments
 
LVL 23

Assisted Solution

by:brettmjohnson
brettmjohnson earned 35 total points
ID: 10653441
There are many reasons the output file may be of different sizes.

If the link phase bound a static library rather than a dynamic library,
the library code will be included in your executable.  You can verify
this by comparing the output of 'nm -u' command for the two executables.
The executable with statically bound libraries should have fewer unresolved
symbols.

The executable may include a symbol table useful in debugging.
Symbols can be removed using the 'strip -S' command.

Different compiler optimization levels may have been specified.

0
 

Expert Comment

by:nova2002
ID: 10653986
Yes, I found that the result of nm -u on both binary are different (see below).

But why this happen ? I installed the same way ... and I use the same compile script.

How can I configure this ? I meant to make it smaller ... like in the MACHINE_1

MACHINE_1 ---> binary size = 20 KB

$ nm -u cgi_a
atoi@@GLIBC_2.0
connect@@GLIBC_2.0
__errno_location@@GLIBC_2.0
exit@@GLIBC_2.0
fcntl@@GLIBC_2.0
getenv@@GLIBC_2.0
gethostbyname@@GLIBC_2.0
__gmon_start__
htons@@GLIBC_2.0
_Jv_RegisterClasses
__libc_start_main@@GLIBC_2.0
localtime@@GLIBC_2.0
mysql_close
mysql_fetch_row
mysql_init
mysql_num_rows
mysql_real_connect
mysql_real_query
mysql_store_result
printf@@GLIBC_2.0
select@@GLIBC_2.0
socket@@GLIBC_2.0
sprintf@@GLIBC_2.0
sscanf@@GLIBC_2.0
strcat@@GLIBC_2.0
strcmp@@GLIBC_2.0
strcpy@@GLIBC_2.0
strftime@@GLIBC_2.0
strlen@@GLIBC_2.0
strpbrk@@GLIBC_2.0
strtok@@GLIBC_2.0
time@@GLIBC_2.0


MACHINE_2 ---> binary size = 181 KB

$ nm -u cgi_a
atoi@@GLIBC_2.0
bcmp@@GLIBC_2.0
bzero@@GLIBC_2.0
chdir@@GLIBC_2.0
closedir@@GLIBC_2.0
close@@GLIBC_2.0
compress
connect@@GLIBC_2.0
endpwent@@GLIBC_2.0
__errno_location@@GLIBC_2.0
exit@@GLIBC_2.0
fclose@@GLIBC_2.1
fcntl@@GLIBC_2.0
fdopen@@GLIBC_2.1
fflush@@GLIBC_2.0
fgets@@GLIBC_2.0
fileno@@GLIBC_2.0
fopen64@@GLIBC_2.1
fprintf@@GLIBC_2.0
fputc@@GLIBC_2.0
fputs@@GLIBC_2.0
free@@GLIBC_2.0
__fxstat64@@GLIBC_2.2
getcwd@@GLIBC_2.0
getenv@@GLIBC_2.0
geteuid@@GLIBC_2.0
gethostbyname@@GLIBC_2.0
gethostbyname_r@@GLIBC_2.1.2
getlogin@@GLIBC_2.0
getpeername@@GLIBC_2.0
getpwnam@@GLIBC_2.0
getpwuid@@GLIBC_2.0
getrusage@@GLIBC_2.0
getservbyname@@GLIBC_2.0
__gmon_start__
htons@@GLIBC_2.0
inet_addr@@GLIBC_2.0
inet_ntoa@@GLIBC_2.0
_Jv_RegisterClasses
__libc_start_main@@GLIBC_2.0
localtime@@GLIBC_2.0
__lxstat64@@GLIBC_2.2
malloc@@GLIBC_2.0
memchr@@GLIBC_2.0
memcpy@@GLIBC_2.0
memmove@@GLIBC_2.0
__mempcpy@@GLIBC_2.0
open64@@GLIBC_2.1
opendir@@GLIBC_2.0
poll@@GLIBC_2.0
printf@@GLIBC_2.0
puts@@GLIBC_2.0
qsort@@GLIBC_2.0
readdir64@@GLIBC_2.2
read@@GLIBC_2.0
readlink@@GLIBC_2.0
realloc@@GLIBC_2.0
realpath@@GLIBC_2.3
select@@GLIBC_2.0
setsockopt@@GLIBC_2.0
shutdown@@GLIBC_2.0
signal@@GLIBC_2.0
socket@@GLIBC_2.0
sprintf@@GLIBC_2.0
sscanf@@GLIBC_2.0
__stpcpy@@GLIBC_2.0
strcat@@GLIBC_2.0
strchr@@GLIBC_2.0
strcmp@@GLIBC_2.0
strcpy@@GLIBC_2.0
strftime@@GLIBC_2.0
strlen@@GLIBC_2.0
strpbrk@@GLIBC_2.0
strrchr@@GLIBC_2.0
strstr@@GLIBC_2.0
strtok@@GLIBC_2.0
__strtol_internal@@GLIBC_2.0
symlink@@GLIBC_2.0
time@@GLIBC_2.0
uncompress
vsprintf@@GLIBC_2.0
write@@GLIBC_2.0
__xstat64@@GLIBC_2.2


0
 

Expert Comment

by:nova2002
ID: 10654096
I found this in MACHINE_1 (binary size = 20 KB)

$ ldd cgi_a
        libmysqlclient.so.12 => /usr/lib/libmysqlclient.so.12 (0x4001c000)
        libz.so.1 => /usr/lib/libz.so.1 (0x40052000)
        libc.so.6 => /lib/libc.so.6 (0x40060000)
        libcrypt.so.1 => /lib/libcrypt.so.1 (0x40187000)
        libnsl.so.1 => /lib/libnsl.so.1 (0x401b3000)
        libm.so.6 => /lib/libm.so.6 (0x401c7000)
        libnss_files.so.2 => /lib/libnss_files.so.2 (0x401e8000)
        libnss_dns.so.2 => /lib/libnss_dns.so.2 (0x401f3000)
        libresolv.so.2 => /lib/libresolv.so.2 (0x401f7000)
        /lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000)

And in MACHINE_2 (binary size = 181 KB)

$ ldd cgi_a
        libz.so.1 => /usr/lib/libz.so.1 (0x4001d000)
        libc.so.6 => /lib/i686/libc.so.6 (0x4002c000)
        /lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000)

But I verified all the libraries are exists .... how to configure GCC so it can dynamically link ?
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 8

Accepted Solution

by:
ssnkumar earned 30 total points
ID: 10655311
Are both nova2002 & muis2002 same persons!?

GCC by default links to dynamic library. If it is not available then only it will looks for static libraries.

Looking at the contents of binaries on both the macines, I think the compilation commands given were different. That's the reason for difference in the size!

-ssnkumar
0
 
LVL 6

Expert Comment

by:joghurt
ID: 10656861
I think the two machines can be different in what libraries they contain:
Machine 1 has the MySQL and other development libraries installed,
Machine 2 has only the core glibc libraries.

Just to make it clear: Are there a libmysqlclient.so.12 on Machine 2, too?
0
 

Author Comment

by:muis2002
ID: 10657721
Fixed. :) wrong search folder ...
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now