gcc -static 命令

原文网址:http://blog.csdn.net/newchenxf/article/details/51743181
转载请注明出处。


刚发现,这是一个神奇的命令!
比如,现在有个简单的程序。该程序依赖于动态库libpthread.so

#include <stdio.h>
#include <pthread.h>

/* this function is run by the second thread */
void *thread_exe(void *x_void_ptr)
{
    /* increment x to 100 */
    int *x_ptr = (int *)x_void_ptr;
    while(++(*x_ptr) < 100);
        printf("x increment finished\n");

    return NULL;
}

int testFunc(int param)
{
    printf(" testFunc %i\n",param);
    pthread_t inc_x_thread;
    int x = 0, y = 0;
    /* create a second thread which executes thread_exe(&y) */
    if(pthread_create(&inc_x_thread, NULL, thread_exe, &x)) {
        fprintf(stderr, "Error creating thread\n");
        return 1;
    }
    /* increment y to 100 in the first thread */
    while(++y < 100);

    printf("y increment finished\n");

    /* wait for the second thread to finish */
    if(pthread_join(inc_x_thread, NULL)) {
        fprintf(stderr, "Error joining thread\n");
       return 2;
    }

    /* show the results - x is now 100 thanks to the second thread */
    printf("x: %d, y: %d\n", x, y);
    return 0;
}

int main(int argc, char* argv[])
{
    int a = 100;
    testFunc(a);
    return 1;
}

加static编译的情况

编译:

gcc test_main.c -static -o test_main -lpthread

结果,你会发现test_main文件很大!

chenxf@chenxf-PC:~/temp/test_main$ ll
-rwxrwxr-x  1 chenxf chenxf 1131418  623 14:45 test_main*
-rw-rw-r--  1 chenxf chenxf    1081  623 14:38 test_main.c

这是因为,test_main是静态的程序,它已经把各种依赖的函数,比如pthread_create()函数,以及所有pthread_create()依赖的任何东西,都包含进来了。跑test_main不需要依赖任何库了!

不信??你可以用这个命令看:

nm test_main

结果一大堆,我截取一下:

0000000000404fb0 T __pthread_cleanup_push
0000000000404fb0 T _pthread_cleanup_push
                 w _pthread_cleanup_push_defer
                 w __pthread_cleanup_upto
0000000000402fb0 W pthread_create
0000000000402fb0 T __pthread_create_2_1
0000000000405870 T __pthread_current_priority
00000000006d6dd8 B __pthread_debug
0000000000405060 T __pthread_disable_asynccancel
0000000000405000 T __pthread_enable_asynccancel
00000000006d6df0 B __pthread_force_elision
0000000000405fe0 T __pthread_get_minstack
0000000000404cf0 T __pthread_getspecific
0000000000404cf0 T pthread_getspecific
00000000006ceec0 D __pthread_init_array
0000000000405cc0 T __pthread_initialize_minimal
0000000000405cc0 T __pthread_initialize_minimal_internal
0000000000402790 T __pthread_init_static_tls
0000000000404070 T pthread_join
0000000000404c50 T __pthread_key_create
0000000000404c50 T pthread_key_create
0000000000404cb0 T pthread_key_delete
00000000006d0d80 B __pthread_keys
00000000006d6e24 B __pthread_multiple_threads
00000000004041b0 T __pthread_mutex_lock
00000000004041b0 T pthread_mutex_lock
0000000000400390 t __pthread_mutex_lock_full
0000000000404430 T __pthread_mutex_trylock
0000000000404430 T pthread_mutex_trylock
0000000000404b60 T __pthread_mutex_unlock
0000000000404b60 T pthread_mutex_unlock
0000000000400917 t __pthread_mutex_unlock_full
0000000000404a80 T __pthread_mutex_unlock_usercnt
0000000000404f10 T __pthread_once
0000000000404f10 T pthread_once
                 w __pthread_rwlock_destroy
                 w __pthread_rwlock_init
                 w __pthread_rwlock_rdlock
                 w __pthread_rwlock_unlock
                 w __pthread_rwlock_wrlock
                 w pthread_setcancelstate
0000000000404d70 T __pthread_setspecific
0000000000404d70 T pthread_setspecific
0000000000405550 T __pthread_tpp_change_priority
00000000004061c0 T __pthread_unwind
0000000000406200 W __pthread_unwind_next
00000000004200c0 t ptmalloc_init.part.7
0000000000419fd0 t ptmalloc_lock_all
000000000041aac0 t ptmalloc_unlock_all
000000000041a0e0 t ptmalloc_unlock_all2
00000000004131c0 W puts
0000000000420b00 W pvalloc
0000000000420b00 T __pvalloc
0000000000411c30 T qsort
0000000000411900 T qsort_r
000000000044faa0 T _quicksort
000000000044fa30 T raise
000000000042d2f0 T rawmemchr
000000000042d2f0 T __rawmemchr
00000000006d7500 B _r_debug
000000000043ec60 W read
000000000043ec60 W __read
000000000040f190 t read_alias_file
0000000000469010 W readdir
0000000000469010 T __readdir
0000000000469010 W readdir64
0000000000469010 T __readdir64
00000000004099f0 t read_encoded_value_with_base
000000000040b410 t read_encoded_value_with_base
0000000000400e79 t read_int
00000000004013cb t read_int
000000000043ec69 T __read_nocancel
0000000000469aa0 T __readonly_area
0000000000406a40 t read_sleb128
0000000000409790 t read_sleb128
000000000041f830 T realloc
000000000041f830 T __realloc
000000000041ec50 t realloc_check
00000000006cf870 V __realloc_hook
00000000004204d0 t realloc_hook_ini
00000000006d6b40 b receiver
0000000000401ef0 T __reclaim_stacks
000000000049d3e0 W recvmsg
000000000049d3e0 W __recvmsg
000000000049d3e9 T __recvmsg_nocancel
0000000000441cf0 T __register_atfork
000000000040af60 T __register_frame
000000000040af50 T __register_frame_info
000000000040aed0 T __register_frame_info_bases
000000000040b010 T __register_frame_info_table
000000000040af90 T __register_frame_info_table_bases
000000000040b020 T __register_frame_table
0000000000459490 T __register_printf_function
0000000000459490 W register_printf_function
000000000045b000 T __register_printf_modifier
000000000045b000 W register_printf_modifier
00000000004593a0 T __register_printf_specifier
00000000004593a0 W register_printf_specifier
000000000045b380 T __register_printf_type
000000000045b380 W register_printf_type
0000000000401900 t register_tm_clones
00000000004002c8 r __rela_iplt_end
00000000004001d8 r __rela_iplt_start
00000000006d67e0 b release_handle
0000000000474590 t remove_slotinfo
00000000006d6500 B _res
000000000046aa30 T __res_iclose
0000000000442310 T __res_init
00000000006d74f8 B __res_initstamp
00000000004423b0 T __res_maybe_init
000000000046ab60 T __res_nclose
000000000046aa00 T __res_ninit
0000000000000000 D __resp
000000000046aa10 T __res_randomid
0000000000469c60 t res_setoptions.isra.0
000000000049fbb0 t res_thread_freeres
0000000000406220 t __restore_rt
0000000000469fd0 T __res_vinit
0000000000469120 T rewinddir
00000000004644e0 W rindex
00000000006d5490 b root
000000000048c9b0 t round_and_return
000000000048f790 t round_and_return
0000000000492190 t round_and_return
00000000006cefa0 d rtld_search_dirs
00000000006d6a80 b rule_dstoff
00000000006d6a90 b rule_stdoff
0000000000411d20 T __run_exit_handlers
00000000006d59e0 b run_fp
00000000006d6ca0 b running
00000000006d6dd0 b samples
00000000006d5b10 b save_arena
00000000004173a0 t save_for_backup
0000000000462b90 t save_for_wbackup.isra.0
00000000006d5b20 b save_free_hook
00000000006d5b30 b save_malloc_hook
000000000043f6a0 W sbrk
000000000043f6a0 T __sbrk
00000000006cf0c0 D __sched_fifo_max_prio
00000000006cf0d0 D __sched_fifo_min_prio
000000000043ea50 T __sched_getparam
000000000043ea50 W sched_getparam
000000000043ead0 T __sched_get_priority_max
000000000043ead0 W sched_get_priority_max
000000000043eaf0 T __sched_get_priority_min
000000000043eaf0 W sched_get_priority_min
000000000043ea90 T __sched_getscheduler
000000000043ea90 W sched_getscheduler
000000000043ea70 T __sched_setscheduler
000000000043ea70 W sched_setscheduler
000000000043eab0 T __sched_yield
000000000043eab0 W sched_yield
000000000040a270 t search_object
0000000000451080 W secure_getenv
00000000006d4f88 b seen_objects
000000000049d440 W sendto
000000000049d440 W __sendto
000000000049d449 T __sendto_nocancel
0000000000450d70 W setenv
0000000000450d70 T __setenv
000000000044f980 T __setfpucw
000000000049dea0 W setitimer
000000000049dea0 T __setitimer
00000000004111f0 T _setjmp
000000000044de10 T setlocale
0000000000406440 W sigaction
0000000000406440 T __sigaction
0000000000405b60 t sigcancel_handler
0000000000405bf0 t sighandler_setxid
000000000044fa00 T __sigjmp_save
0000000000411200 W siglongjmp
00000000004112b0 W sigprocmask
00000000004112b0 T __sigprocmask
000000000044f9a0 T __sigsetjmp
0000000000409980 t size_of_encoded_value
00000000004a6320 r slashdot.9308
000000000049d4a0 W socket
000000000049d4a0 T __socket
00000000004b6bba r sort_mask_chars
000000000045b500 T sscanf
000000000045b500 T __sscanf
00000000006cf0a0 d stack_cache
00000000006d0d60 b stack_cache_actsize
00000000006d0d50 b stack_cache_lock
00000000006cefe0 D __stack_prot
00000000006cf090 d stack_used
00000000006d0d30 B __stack_user
00000000006d5560 b stage
000000000040189e T _start
00000000004bf3d0 R __start___libc_atexit
00000000004bf3d8 R __start___libc_thread_subfreeres
0000000000402c40 t start_thread
00000000006d694c b state
00000000006d6954 b state
00000000006d695c b state
00000000006d6c08 b state
00000000006d6bc0 b static_buf
00000000006d4fa0 b static_slotinfo
00000000006d6df8 B __static_tls_align_m1
00000000006d6e00 B __static_tls_size
00000000006cf808 D stderr
00000000006cf818 D stdin
00000000006cf810 D stdout
00000000004b44e0 r step0_jumps.11680
00000000004b4e00 r step0_jumps.11706
00000000004b43e0 r step1_jumps.11711
00000000004b4d00 r step1_jumps.11737
00000000004b42e0 r step2_jumps.11712
00000000004b4c00 r step2_jumps.11738
00000000004b41e0 r step3a_jumps.11713
00000000004b4b00 r step3a_jumps.11739
00000000004b3fe0 r step3b_jumps.11715
00000000004b4900 r step3b_jumps.11741
00000000004b40e0 r step4_jumps.11716
00000000004b4a00 r step4_jumps.11742
00000000004b3ee0 r step4_jumps.11870
00000000004b4800 r step4_jumps.11895
00000000004bf3d8 R __stop___libc_atexit
00000000004bf3e8 R __stop___libc_thread_subfreeres
0000000000427310 i stpcpy
0000000000427310 i __stpcpy
0000000000427350 T __stpcpy_sse2
000000000043b0e0 T __stpcpy_sse2_unaligned
00000000004392a0 T __stpcpy_ssse3
0000000000427480 i strcasecmp
0000000000427480 i __strcasecmp
000000000042b2d0 T __strcasecmp_avx
0000000000427430 i __strcasecmp_l
0000000000427430 i strcasecmp_l
000000000042b2e0 T __strcasecmp_l_avx
000000000043dd00 T __strcasecmp_l_nonascii
00000000004274f0 T __strcasecmp_l_sse2
0000000000429730 T __strcasecmp_l_sse42
00000000004359b0 T __strcasecmp_l_ssse3
00000000004274e0 T __strcasecmp_sse2
0000000000429720 T __strcasecmp_sse42
00000000004359a0 T __strcasecmp_ssse3
0000000000421910 i strchr
000000000042d500 W strchrnul
000000000042d500 T __strchrnul
0000000000421940 T __strchr_sse2
000000000043b790 T __strchr_sse2_no_bsf
0000000000421b60 i strcmp
0000000000421ba0 T __strcmp_sse2
000000000042ea10 T __strcmp_sse2_unaligned
0000000000422fe0 T __strcmp_sse42
000000000042d7b0 T __strcmp_ssse3
0000000000423d90 i strcpy
0000000000423dd0 T __strcpy_sse2
000000000043aab0 T __strcpy_sse2_unaligned
0000000000437af0 T __strcpy_ssse3
0000000000423eb0 W strdup
0000000000423eb0 T __strdup
00000000004816d0 T strerror
0000000000464140 T __strerror_r
0000000000464140 W strerror_r
00000000006d7590 b string_space
00000000006d5528 b string_space_act
00000000006d5520 b string_space_max
0000000000400e07 t strip
0000000000423f00 T strlen
0000000000494da0 i strncasecmp
0000000000494da0 i __strncasecmp
0000000000499420 T __strncasecmp_avx
0000000000494d50 i __strncasecmp_l
0000000000494d50 i strncasecmp_l
0000000000499430 T __strncasecmp_l_avx
000000000049d330 T __strncasecmp_l_nonascii
0000000000494e10 T __strncasecmp_l_sse2
0000000000497460 T __strncasecmp_l_sse42
000000000049ade0 T __strncasecmp_l_ssse3
0000000000494e00 T __strncasecmp_sse2
0000000000497450 T __strncasecmp_sse42
000000000049add0 T __strncasecmp_ssse3
00000000004240c0 T strncmp
0000000000481750 i strncpy
0000000000481810 T __strncpy_sse2
0000000000484490 T __strncpy_sse2_unaligned
0000000000481910 T __strncpy_ssse3
00000000004640f0 W strndup
00000000004640f0 T __strndup
00000000004642c0 W strnlen
00000000004642c0 T __strnlen
0000000000494cb0 T strpbrk
00000000004644e0 T strrchr
0000000000481790 W strsep
0000000000481790 T __strsep
0000000000481790 T __strsep_g
00000000004263d0 i strstr
0000000000425e10 T __strstr_sse2
000000000043d220 T __strstr_sse2_unaligned
000000000048c690 W strtod
000000000048c680 T __strtod_internal
0000000000491eb0 W __strtod_l
0000000000491eb0 W strtod_l
000000000048fc60 T ____strtod_l_internal
000000000048c660 W strtof
000000000048c650 T __strtof_internal
000000000048f4b0 W __strtof_l
000000000048f4b0 W strtof_l
000000000048ce40 T ____strtof_l_internal
00000000004510b0 T strtol
000000000048c6c0 W strtold
000000000048c6b0 T __strtold_internal
0000000000494780 W __strtold_l
0000000000494780 W strtold_l
0000000000492600 T ____strtold_l_internal
00000000004510a0 T __strtol_internal
00000000004510b0 W strtoll
0000000000451580 W __strtol_l
0000000000451580 W strtol_l
00000000004510d0 T ____strtol_l_internal
00000000004510a0 T __strtoll_internal
0000000000451580 W __strtoll_l
0000000000451580 W strtoll_l
00000000004510d0 T ____strtoll_l_internal
00000000004b3c40 R __strtol_ul_max_tab
00000000004b3c00 R __strtol_ul_rem_tab
000000000048c6e0 t str_to_mpn.isra.0
000000000048f4c0 t str_to_mpn.isra.0
0000000000491ec0 t str_to_mpn.isra.0
00000000004510b0 W strtoq
00000000004120b0 T strtoul
00000000004120a0 T __strtoul_internal
00000000004120b0 W strtoull
0000000000412520 W __strtoul_l
0000000000412520 W strtoul_l
00000000004120d0 T ____strtoul_l_internal
00000000004120a0 T __strtoull_internal
0000000000412520 W __strtoull_l
0000000000412520 W strtoull_l
00000000004120d0 T ____strtoull_l_internal
00000000004120b0 W strtouq
00000000006d4dd0 b subs.9002
000000000040bb10 T __syscall_error
000000000040bb13 T __syscall_error_1
000000000043e360 W sysconf
000000000043e360 T __sysconf
000000000043e280 t __sysconf_check_spec
00000000004bd980 V _sys_errlist
00000000004bd980 V sys_errlist
00000000004bd980 R __sys_errlist_internal
00000000004bd980 R _sys_errlist_internal
00000000004bddb8 V _sys_nerr
00000000004bddb8 V sys_nerr
00000000004bddb8 R __sys_nerr_internal
00000000004bddb8 R _sys_nerr_internal
00000000004b7540 r system_dirs
00000000004b7520 r system_dirs_len
000000000041ab70 t systrim.isra.1
0000000000469810 W tcgetattr
0000000000469810 T __tcgetattr
00000000004400f0 W tdelete
00000000004400f0 T __tdelete
0000000000441410 W tdestroy
0000000000441410 T __tdestroy
000000000043f890 t tdestroy_recurse
00000000004b90e0 R __tens
00000000004bf040 R _tens_in_limb
00000000006d4dc8 b terminator.8845
00000000004019ee T testFunc
00000000006d6c30 b textsize
00000000004400a0 W tfind
00000000004400a0 T __tfind
000000000049fe68 R _thread_db_const_thread_area
000000000049fe98 R _thread_db_dtv_dtv
000000000049fe8c R _thread_db_dtv_t_pointer_val
000000000049fea4 R _thread_db_link_map_l_tls_modid
000000000049ff4c R _thread_db_list_t_next
000000000049ff40 R _thread_db_list_t_prev
000000000049fef8 R _thread_db___nptl_initial_report_events
000000000049ff04 R _thread_db___nptl_last_event
000000000049ff10 R _thread_db___nptl_nthreads
000000000049ffac R _thread_db_pthread_cancelhandling
000000000049fe80 R _thread_db_pthread_dtvp
000000000049ff7c R _thread_db_pthread_eventbuf
000000000049ff70 R _thread_db_pthread_eventbuf_eventmask
000000000049ff64 R _thread_db_pthread_eventbuf_eventmask_event_bits
000000000049febc R _thread_db_pthread_key_data_data
000000000049feb0 R _thread_db_pthread_key_data_level2_data
000000000049fec8 R _thread_db_pthread_key_data_seq
000000000049feec R _thread_db___pthread_keys
000000000049fed4 R _thread_db_pthread_key_struct_destr
000000000049fee0 R _thread_db_pthread_key_struct_seq
000000000049ffe8 R _thread_db_pthread_list
000000000049ff58 R _thread_db_pthread_nextevent
000000000049ffc4 R _thread_db_pthread_pid
000000000049ffdc R _thread_db_pthread_report_events
000000000049ff94 R _thread_db_pthread_schedparam_sched_priority
000000000049ffa0 R _thread_db_pthread_schedpolicy
000000000049ff88 R _thread_db_pthread_specific
000000000049ffb8 R _thread_db_pthread_start_routine
000000000049ffd0 R _thread_db_pthread_tid
000000000049fe70 R _thread_db_sizeof_list_t
000000000049fe7c R _thread_db_sizeof_pthread
000000000049fe70 R _thread_db_sizeof_pthread_key_data
000000000049fe6c R _thread_db_sizeof_pthread_key_data_level2
000000000049fe70 R _thread_db_sizeof_pthread_key_struct
000000000049fe74 R _thread_db_sizeof_td_eventbuf_t
000000000049fe78 R _thread_db_sizeof_td_thr_events_t
000000000049ff1c R _thread_db_td_eventbuf_t_eventdata
000000000049ff28 R _thread_db_td_eventbuf_t_eventnum
000000000049ff34 R _thread_db_td_thr_events_t_event_bits
00000000004019ae T thread_exe
0000000000465f60 T time
0000000000486520 W timelocal
00000000006d5ba8 b timestamp.10239
00000000006d6980 V timezone
00000000006d6980 B __timezone
00000000006d7540 B _tmbuf
00000000006d0cc8 D __TMC_END__
00000000004a4280 r to_mb
000000000041b1a0 t top_check
00000000006d6c70 b tos
00000000004a4300 r to_wc
0000000000469a50 W towctrans
0000000000469a50 T __towctrans
000000000040c320 t transcmp
00000000006d75a8 b transitions
00000000004b1760 r translit_from_idx
00000000004aed00 r translit_from_tbl

......

你会发现,和thread相关的各种各样的东西,全部来了!而不仅仅是pthread_create().

不加-static的情况

而如果你不加-static,结果就不同了。

gcc test_main.c -o test_main -lpthread

文件要小很多!

chenxf@chenxf-PC:~/temp/test_main$ ll
-rwxrwxr-x  1 chenxf chenxf  8891  623 14:39 test_main*
-rw-rw-r--  1 chenxf chenxf  1081  623 14:38 test_main.c

用nm查看,会发现东西就少很多。

chenxf@chenxf-PC:~/temp/test_main$ nm test_main
0000000000601060 B __bss_start
0000000000601068 b completed.6973
0000000000601050 D __data_start
0000000000601050 W data_start
00000000004006b0 t deregister_tm_clones
0000000000400720 t __do_global_dtors_aux
0000000000600e08 t __do_global_dtors_aux_fini_array_entry
0000000000601058 D __dso_handle
0000000000600e18 d _DYNAMIC
0000000000601060 D _edata
0000000000601070 B _end
0000000000400934 T _fini
0000000000400740 t frame_dummy
0000000000600e00 t __frame_dummy_init_array_entry
0000000000400b30 r __FRAME_END__
                 U fwrite@@GLIBC_2.2.5
0000000000601000 d _GLOBAL_OFFSET_TABLE_
                 w __gmon_start__
00000000004005d8 T _init
0000000000600e08 t __init_array_end
0000000000600e00 t __init_array_start
0000000000400940 R _IO_stdin_used
                 w _ITM_deregisterTMCloneTable
                 w _ITM_registerTMCloneTable
0000000000600e10 d __JCR_END__
0000000000600e10 d __JCR_LIST__
                 w _Jv_RegisterClasses
0000000000400930 T __libc_csu_fini
00000000004008c0 T __libc_csu_init
                 U __libc_start_main@@GLIBC_2.2.5
000000000040088c T main
                 U printf@@GLIBC_2.2.5
                 U pthread_create@@GLIBC_2.2.5
                 U pthread_join@@GLIBC_2.2.5
                 U puts@@GLIBC_2.2.5
00000000004006e0 t register_tm_clones
0000000000400680 T _start
0000000000601060 B stderr@@GLIBC_2.2.5
00000000004007ad T testFunc
000000000040076d T thread_exe
0000000000601060 D __TMC_END__

可见

U printf@@GLIBC_2.2.5
U pthread_create@@GLIBC_2.2.5
U pthread_join@@GLIBC_2.2.5

U表示在本程序只是调用,没有定义,需要其他库支持。
T表示本程序定义。

总结

gcc -static 用于编译一个程序时,会使此程序静态编译(把动态库的函数和所依赖的任何的东西,都编译进本程序),编译好后,文件会非常大,但是,运行时就不需要依赖任何动态库。

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/newchenxf/article/details/51743181
文章标签: gcc static
个人分类: 编译处理
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭