gcc -static 命令

来源:互联网 发布:淘宝上可信的澳洲代购 编辑:程序博客网 时间:2024/06/16 16:49

原文网址: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_push0000000000404fb0 T _pthread_cleanup_push                 w _pthread_cleanup_push_defer                 w __pthread_cleanup_upto0000000000402fb0 W pthread_create0000000000402fb0 T __pthread_create_2_10000000000405870 T __pthread_current_priority00000000006d6dd8 B __pthread_debug0000000000405060 T __pthread_disable_asynccancel0000000000405000 T __pthread_enable_asynccancel00000000006d6df0 B __pthread_force_elision0000000000405fe0 T __pthread_get_minstack0000000000404cf0 T __pthread_getspecific0000000000404cf0 T pthread_getspecific00000000006ceec0 D __pthread_init_array0000000000405cc0 T __pthread_initialize_minimal0000000000405cc0 T __pthread_initialize_minimal_internal0000000000402790 T __pthread_init_static_tls0000000000404070 T pthread_join0000000000404c50 T __pthread_key_create0000000000404c50 T pthread_key_create0000000000404cb0 T pthread_key_delete00000000006d0d80 B __pthread_keys00000000006d6e24 B __pthread_multiple_threads00000000004041b0 T __pthread_mutex_lock00000000004041b0 T pthread_mutex_lock0000000000400390 t __pthread_mutex_lock_full0000000000404430 T __pthread_mutex_trylock0000000000404430 T pthread_mutex_trylock0000000000404b60 T __pthread_mutex_unlock0000000000404b60 T pthread_mutex_unlock0000000000400917 t __pthread_mutex_unlock_full0000000000404a80 T __pthread_mutex_unlock_usercnt0000000000404f10 T __pthread_once0000000000404f10 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_setcancelstate0000000000404d70 T __pthread_setspecific0000000000404d70 T pthread_setspecific0000000000405550 T __pthread_tpp_change_priority00000000004061c0 T __pthread_unwind0000000000406200 W __pthread_unwind_next00000000004200c0 t ptmalloc_init.part.70000000000419fd0 t ptmalloc_lock_all000000000041aac0 t ptmalloc_unlock_all000000000041a0e0 t ptmalloc_unlock_all200000000004131c0 W puts0000000000420b00 W pvalloc0000000000420b00 T __pvalloc0000000000411c30 T qsort0000000000411900 T qsort_r000000000044faa0 T _quicksort000000000044fa30 T raise000000000042d2f0 T rawmemchr000000000042d2f0 T __rawmemchr00000000006d7500 B _r_debug000000000043ec60 W read000000000043ec60 W __read000000000040f190 t read_alias_file0000000000469010 W readdir0000000000469010 T __readdir0000000000469010 W readdir640000000000469010 T __readdir6400000000004099f0 t read_encoded_value_with_base000000000040b410 t read_encoded_value_with_base0000000000400e79 t read_int00000000004013cb t read_int000000000043ec69 T __read_nocancel0000000000469aa0 T __readonly_area0000000000406a40 t read_sleb1280000000000409790 t read_sleb128000000000041f830 T realloc000000000041f830 T __realloc000000000041ec50 t realloc_check00000000006cf870 V __realloc_hook00000000004204d0 t realloc_hook_ini00000000006d6b40 b receiver0000000000401ef0 T __reclaim_stacks000000000049d3e0 W recvmsg000000000049d3e0 W __recvmsg000000000049d3e9 T __recvmsg_nocancel0000000000441cf0 T __register_atfork000000000040af60 T __register_frame000000000040af50 T __register_frame_info000000000040aed0 T __register_frame_info_bases000000000040b010 T __register_frame_info_table000000000040af90 T __register_frame_info_table_bases000000000040b020 T __register_frame_table0000000000459490 T __register_printf_function0000000000459490 W register_printf_function000000000045b000 T __register_printf_modifier000000000045b000 W register_printf_modifier00000000004593a0 T __register_printf_specifier00000000004593a0 W register_printf_specifier000000000045b380 T __register_printf_type000000000045b380 W register_printf_type0000000000401900 t register_tm_clones00000000004002c8 r __rela_iplt_end00000000004001d8 r __rela_iplt_start00000000006d67e0 b release_handle0000000000474590 t remove_slotinfo00000000006d6500 B _res000000000046aa30 T __res_iclose0000000000442310 T __res_init00000000006d74f8 B __res_initstamp00000000004423b0 T __res_maybe_init000000000046ab60 T __res_nclose000000000046aa00 T __res_ninit0000000000000000 D __resp000000000046aa10 T __res_randomid0000000000469c60 t res_setoptions.isra.0000000000049fbb0 t res_thread_freeres0000000000406220 t __restore_rt0000000000469fd0 T __res_vinit0000000000469120 T rewinddir00000000004644e0 W rindex00000000006d5490 b root000000000048c9b0 t round_and_return000000000048f790 t round_and_return0000000000492190 t round_and_return00000000006cefa0 d rtld_search_dirs00000000006d6a80 b rule_dstoff00000000006d6a90 b rule_stdoff0000000000411d20 T __run_exit_handlers00000000006d59e0 b run_fp00000000006d6ca0 b running00000000006d6dd0 b samples00000000006d5b10 b save_arena00000000004173a0 t save_for_backup0000000000462b90 t save_for_wbackup.isra.000000000006d5b20 b save_free_hook00000000006d5b30 b save_malloc_hook000000000043f6a0 W sbrk000000000043f6a0 T __sbrk00000000006cf0c0 D __sched_fifo_max_prio00000000006cf0d0 D __sched_fifo_min_prio000000000043ea50 T __sched_getparam000000000043ea50 W sched_getparam000000000043ead0 T __sched_get_priority_max000000000043ead0 W sched_get_priority_max000000000043eaf0 T __sched_get_priority_min000000000043eaf0 W sched_get_priority_min000000000043ea90 T __sched_getscheduler000000000043ea90 W sched_getscheduler000000000043ea70 T __sched_setscheduler000000000043ea70 W sched_setscheduler000000000043eab0 T __sched_yield000000000043eab0 W sched_yield000000000040a270 t search_object0000000000451080 W secure_getenv00000000006d4f88 b seen_objects000000000049d440 W sendto000000000049d440 W __sendto000000000049d449 T __sendto_nocancel0000000000450d70 W setenv0000000000450d70 T __setenv000000000044f980 T __setfpucw000000000049dea0 W setitimer000000000049dea0 T __setitimer00000000004111f0 T _setjmp000000000044de10 T setlocale0000000000406440 W sigaction0000000000406440 T __sigaction0000000000405b60 t sigcancel_handler0000000000405bf0 t sighandler_setxid000000000044fa00 T __sigjmp_save0000000000411200 W siglongjmp00000000004112b0 W sigprocmask00000000004112b0 T __sigprocmask000000000044f9a0 T __sigsetjmp0000000000409980 t size_of_encoded_value00000000004a6320 r slashdot.9308000000000049d4a0 W socket000000000049d4a0 T __socket00000000004b6bba r sort_mask_chars000000000045b500 T sscanf000000000045b500 T __sscanf00000000006cf0a0 d stack_cache00000000006d0d60 b stack_cache_actsize00000000006d0d50 b stack_cache_lock00000000006cefe0 D __stack_prot00000000006cf090 d stack_used00000000006d0d30 B __stack_user00000000006d5560 b stage000000000040189e T _start00000000004bf3d0 R __start___libc_atexit00000000004bf3d8 R __start___libc_thread_subfreeres0000000000402c40 t start_thread00000000006d694c b state00000000006d6954 b state00000000006d695c b state00000000006d6c08 b state00000000006d6bc0 b static_buf00000000006d4fa0 b static_slotinfo00000000006d6df8 B __static_tls_align_m100000000006d6e00 B __static_tls_size00000000006cf808 D stderr00000000006cf818 D stdin00000000006cf810 D stdout00000000004b44e0 r step0_jumps.1168000000000004b4e00 r step0_jumps.1170600000000004b43e0 r step1_jumps.1171100000000004b4d00 r step1_jumps.1173700000000004b42e0 r step2_jumps.1171200000000004b4c00 r step2_jumps.1173800000000004b41e0 r step3a_jumps.1171300000000004b4b00 r step3a_jumps.1173900000000004b3fe0 r step3b_jumps.1171500000000004b4900 r step3b_jumps.1174100000000004b40e0 r step4_jumps.1171600000000004b4a00 r step4_jumps.1174200000000004b3ee0 r step4_jumps.1187000000000004b4800 r step4_jumps.1189500000000004bf3d8 R __stop___libc_atexit00000000004bf3e8 R __stop___libc_thread_subfreeres0000000000427310 i stpcpy0000000000427310 i __stpcpy0000000000427350 T __stpcpy_sse2000000000043b0e0 T __stpcpy_sse2_unaligned00000000004392a0 T __stpcpy_ssse30000000000427480 i strcasecmp0000000000427480 i __strcasecmp000000000042b2d0 T __strcasecmp_avx0000000000427430 i __strcasecmp_l0000000000427430 i strcasecmp_l000000000042b2e0 T __strcasecmp_l_avx000000000043dd00 T __strcasecmp_l_nonascii00000000004274f0 T __strcasecmp_l_sse20000000000429730 T __strcasecmp_l_sse4200000000004359b0 T __strcasecmp_l_ssse300000000004274e0 T __strcasecmp_sse20000000000429720 T __strcasecmp_sse4200000000004359a0 T __strcasecmp_ssse30000000000421910 i strchr000000000042d500 W strchrnul000000000042d500 T __strchrnul0000000000421940 T __strchr_sse2000000000043b790 T __strchr_sse2_no_bsf0000000000421b60 i strcmp0000000000421ba0 T __strcmp_sse2000000000042ea10 T __strcmp_sse2_unaligned0000000000422fe0 T __strcmp_sse42000000000042d7b0 T __strcmp_ssse30000000000423d90 i strcpy0000000000423dd0 T __strcpy_sse2000000000043aab0 T __strcpy_sse2_unaligned0000000000437af0 T __strcpy_ssse30000000000423eb0 W strdup0000000000423eb0 T __strdup00000000004816d0 T strerror0000000000464140 T __strerror_r0000000000464140 W strerror_r00000000006d7590 b string_space00000000006d5528 b string_space_act00000000006d5520 b string_space_max0000000000400e07 t strip0000000000423f00 T strlen0000000000494da0 i strncasecmp0000000000494da0 i __strncasecmp0000000000499420 T __strncasecmp_avx0000000000494d50 i __strncasecmp_l0000000000494d50 i strncasecmp_l0000000000499430 T __strncasecmp_l_avx000000000049d330 T __strncasecmp_l_nonascii0000000000494e10 T __strncasecmp_l_sse20000000000497460 T __strncasecmp_l_sse42000000000049ade0 T __strncasecmp_l_ssse30000000000494e00 T __strncasecmp_sse20000000000497450 T __strncasecmp_sse42000000000049add0 T __strncasecmp_ssse300000000004240c0 T strncmp0000000000481750 i strncpy0000000000481810 T __strncpy_sse20000000000484490 T __strncpy_sse2_unaligned0000000000481910 T __strncpy_ssse300000000004640f0 W strndup00000000004640f0 T __strndup00000000004642c0 W strnlen00000000004642c0 T __strnlen0000000000494cb0 T strpbrk00000000004644e0 T strrchr0000000000481790 W strsep0000000000481790 T __strsep0000000000481790 T __strsep_g00000000004263d0 i strstr0000000000425e10 T __strstr_sse2000000000043d220 T __strstr_sse2_unaligned000000000048c690 W strtod000000000048c680 T __strtod_internal0000000000491eb0 W __strtod_l0000000000491eb0 W strtod_l000000000048fc60 T ____strtod_l_internal000000000048c660 W strtof000000000048c650 T __strtof_internal000000000048f4b0 W __strtof_l000000000048f4b0 W strtof_l000000000048ce40 T ____strtof_l_internal00000000004510b0 T strtol000000000048c6c0 W strtold000000000048c6b0 T __strtold_internal0000000000494780 W __strtold_l0000000000494780 W strtold_l0000000000492600 T ____strtold_l_internal00000000004510a0 T __strtol_internal00000000004510b0 W strtoll0000000000451580 W __strtol_l0000000000451580 W strtol_l00000000004510d0 T ____strtol_l_internal00000000004510a0 T __strtoll_internal0000000000451580 W __strtoll_l0000000000451580 W strtoll_l00000000004510d0 T ____strtoll_l_internal00000000004b3c40 R __strtol_ul_max_tab00000000004b3c00 R __strtol_ul_rem_tab000000000048c6e0 t str_to_mpn.isra.0000000000048f4c0 t str_to_mpn.isra.00000000000491ec0 t str_to_mpn.isra.000000000004510b0 W strtoq00000000004120b0 T strtoul00000000004120a0 T __strtoul_internal00000000004120b0 W strtoull0000000000412520 W __strtoul_l0000000000412520 W strtoul_l00000000004120d0 T ____strtoul_l_internal00000000004120a0 T __strtoull_internal0000000000412520 W __strtoull_l0000000000412520 W strtoull_l00000000004120d0 T ____strtoull_l_internal00000000004120b0 W strtouq00000000006d4dd0 b subs.9002000000000040bb10 T __syscall_error000000000040bb13 T __syscall_error_1000000000043e360 W sysconf000000000043e360 T __sysconf000000000043e280 t __sysconf_check_spec00000000004bd980 V _sys_errlist00000000004bd980 V sys_errlist00000000004bd980 R __sys_errlist_internal00000000004bd980 R _sys_errlist_internal00000000004bddb8 V _sys_nerr00000000004bddb8 V sys_nerr00000000004bddb8 R __sys_nerr_internal00000000004bddb8 R _sys_nerr_internal00000000004b7540 r system_dirs00000000004b7520 r system_dirs_len000000000041ab70 t systrim.isra.10000000000469810 W tcgetattr0000000000469810 T __tcgetattr00000000004400f0 W tdelete00000000004400f0 T __tdelete0000000000441410 W tdestroy0000000000441410 T __tdestroy000000000043f890 t tdestroy_recurse00000000004b90e0 R __tens00000000004bf040 R _tens_in_limb00000000006d4dc8 b terminator.884500000000004019ee T testFunc00000000006d6c30 b textsize00000000004400a0 W tfind00000000004400a0 T __tfind000000000049fe68 R _thread_db_const_thread_area000000000049fe98 R _thread_db_dtv_dtv000000000049fe8c R _thread_db_dtv_t_pointer_val000000000049fea4 R _thread_db_link_map_l_tls_modid000000000049ff4c R _thread_db_list_t_next000000000049ff40 R _thread_db_list_t_prev000000000049fef8 R _thread_db___nptl_initial_report_events000000000049ff04 R _thread_db___nptl_last_event000000000049ff10 R _thread_db___nptl_nthreads000000000049ffac R _thread_db_pthread_cancelhandling000000000049fe80 R _thread_db_pthread_dtvp000000000049ff7c R _thread_db_pthread_eventbuf000000000049ff70 R _thread_db_pthread_eventbuf_eventmask000000000049ff64 R _thread_db_pthread_eventbuf_eventmask_event_bits000000000049febc R _thread_db_pthread_key_data_data000000000049feb0 R _thread_db_pthread_key_data_level2_data000000000049fec8 R _thread_db_pthread_key_data_seq000000000049feec R _thread_db___pthread_keys000000000049fed4 R _thread_db_pthread_key_struct_destr000000000049fee0 R _thread_db_pthread_key_struct_seq000000000049ffe8 R _thread_db_pthread_list000000000049ff58 R _thread_db_pthread_nextevent000000000049ffc4 R _thread_db_pthread_pid000000000049ffdc R _thread_db_pthread_report_events000000000049ff94 R _thread_db_pthread_schedparam_sched_priority000000000049ffa0 R _thread_db_pthread_schedpolicy000000000049ff88 R _thread_db_pthread_specific000000000049ffb8 R _thread_db_pthread_start_routine000000000049ffd0 R _thread_db_pthread_tid000000000049fe70 R _thread_db_sizeof_list_t000000000049fe7c R _thread_db_sizeof_pthread000000000049fe70 R _thread_db_sizeof_pthread_key_data000000000049fe6c R _thread_db_sizeof_pthread_key_data_level2000000000049fe70 R _thread_db_sizeof_pthread_key_struct000000000049fe74 R _thread_db_sizeof_td_eventbuf_t000000000049fe78 R _thread_db_sizeof_td_thr_events_t000000000049ff1c R _thread_db_td_eventbuf_t_eventdata000000000049ff28 R _thread_db_td_eventbuf_t_eventnum000000000049ff34 R _thread_db_td_thr_events_t_event_bits00000000004019ae T thread_exe0000000000465f60 T time0000000000486520 W timelocal00000000006d5ba8 b timestamp.1023900000000006d6980 V timezone00000000006d6980 B __timezone00000000006d7540 B _tmbuf00000000006d0cc8 D __TMC_END__00000000004a4280 r to_mb000000000041b1a0 t top_check00000000006d6c70 b tos00000000004a4300 r to_wc0000000000469a50 W towctrans0000000000469a50 T __towctrans000000000040c320 t transcmp00000000006d75a8 b transitions00000000004b1760 r translit_from_idx00000000004aed00 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_main0000000000601060 B __bss_start0000000000601068 b completed.69730000000000601050 D __data_start0000000000601050 W data_start00000000004006b0 t deregister_tm_clones0000000000400720 t __do_global_dtors_aux0000000000600e08 t __do_global_dtors_aux_fini_array_entry0000000000601058 D __dso_handle0000000000600e18 d _DYNAMIC0000000000601060 D _edata0000000000601070 B _end0000000000400934 T _fini0000000000400740 t frame_dummy0000000000600e00 t __frame_dummy_init_array_entry0000000000400b30 r __FRAME_END__                 U fwrite@@GLIBC_2.2.50000000000601000 d _GLOBAL_OFFSET_TABLE_                 w __gmon_start__00000000004005d8 T _init0000000000600e08 t __init_array_end0000000000600e00 t __init_array_start0000000000400940 R _IO_stdin_used                 w _ITM_deregisterTMCloneTable                 w _ITM_registerTMCloneTable0000000000600e10 d __JCR_END__0000000000600e10 d __JCR_LIST__                 w _Jv_RegisterClasses0000000000400930 T __libc_csu_fini00000000004008c0 T __libc_csu_init                 U __libc_start_main@@GLIBC_2.2.5000000000040088c 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.500000000004006e0 t register_tm_clones0000000000400680 T _start0000000000601060 B stderr@@GLIBC_2.2.500000000004007ad T testFunc000000000040076d T thread_exe0000000000601060 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 用于编译一个程序时,会使此程序静态编译(把动态库的函数和所依赖的任何的东西,都编译进本程序),编译好后,文件会非常大,但是,运行时就不需要依赖任何动态库。

0 0