• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 262
  • Last Modified:

Compiled size problem

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
muis2002
Asked:
muis2002
2 Solutions
 
brettmjohnsonCommented:
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
 
nova2002Commented:
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
 
nova2002Commented:
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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
ssnkumarCommented:
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
 
joghurtCommented:
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
 
muis2002Author Commented:
Fixed. :) wrong search folder ...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Simple Misconfiguration =Network Vulnerability

In this technical webinar, AlgoSec will present several examples of common misconfigurations; including a basic device change, business application connectivity changes, and data center migrations. Learn best practices to protect your business from attack.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now