LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Blogs > divyavyas
User Name
Password

Notices


Rate this Entry

HOw to create static executable of a C program ex:hello.c

Posted 08-08-2011 at 01:08 AM by divyavyas

HOw to create static executable of a C program ex:hello.c
Posted in Uncategorized
Views 8779 Comments 1
« Prev     Main     Next »
Total Comments 1

Comments

  1. Old Comment
    This is a question that would best be asked in the programming forum, rather than as a blog post. But, since I'm a nice guy (this is on OpenBSD-CURRENT, but for Linux it should be quite similar):

    Code:
    $ cat test.c
    #include <stdio.h>
    
    /* stupid test program */
    int main()
    {
            int a,b;
            a = 7814;
            b = 5612;
            printf("a: %i, b: %i\n", a, b);
            a = a ^ b;
            printf("a: %i, b: %i\n", a, b);
            b = a ^ b;
            printf("a: %i, b: %i\n", a, b);
            a = a ^ b;
            printf("a: %i, b: %i\n", a, b);
            return 0;
    }
    
    # compile normally...
    $ gcc test.c         
    $ ls -lha a.out      
    -rwxr-xr-x  1 user  user   6.9K Aug  8 09:36 a.out
    $ ldd a.out  
    a.out:
            Start            End              Type Open Ref GrpRef Name
            0000000000400000 0000000000801000 exe  1    0   0      a.out
            00000002038cf000 0000000203db0000 rlib 0    1   0      /usr/lib/libc.so.60.1
            0000000205000000 0000000205000000 rtld 0    1   0      /usr/libexec/ld.so
    $ objdump -T a.out  
    
    a.out:     file format elf64-x86-64
    
    DYNAMIC SYMBOL TABLE:
    00000000004006d0      DF *UND*  00000000000000a6 printf
    0000000000600b80 g    DO *ABS*  0000000000000000 _DYNAMIC
    0000000000700d40 g    D  .gotpad1       0000000000000000 __got_end
    0000000000700cc8 g    D  .gotpad0       0000000000000000 __got_start
    0000000000800d80 g    DO .bss   0000000000000008 environ
    0000000000600a48 g    DO .data  0000000000000008 __progname
    0000000000800d40 g    D  *ABS*  0000000000000000 __bss_start
    0000000000800da0 g    DO .bss   0000000000000100 __progname_storage
    00000000004006e0      DF *UND*  0000000000000009 atexit
    00000000004009e0 g    DF .fini  0000000000000000 __fini
    0000000000800d40 g    D  *ABS*  0000000000000000 _edata
    0000000000700cc8 g    DO *ABS*  0000000000000000 _GLOBAL_OFFSET_TABLE_
    0000000000800ea0 g    D  *ABS*  0000000000000000 _end
    00000000004006f0      DF *UND*  0000000000000011 exit
    0000000000600a48 g    D  .data  0000000000000000 __data_start
    0000000000000000  w   D  *UND*  0000000000000000 _Jv_RegisterClasses
    
    # compile statically...
    $ gcc -static test.c 
    $ ls -lha a.out      
    -rwxr-xr-x  1 user  user   316K Aug  8 09:36 a.out
    $ ldd a.out          
    ldd: a.out: not a dynamic executable
    $ objdump -T a.out   
    
    a.out:     file format elf64-x86-64
    
    objdump: a.out: not a dynamic object
    DYNAMIC SYMBOL TABLE:
    no symbols
    Posted 08-08-2011 at 09:37 AM by rocket357 rocket357 is offline
    Updated 08-08-2011 at 09:47 AM by rocket357
 

  



All times are GMT -5. The time now is 02:41 PM.

Main Menu
Advertisement
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