1
行爲我寫了一個天真的C程序try.c困惑gcc的-static選項,或它在虛擬機
#include <stdlib.h>
int main() {return 0;}
然後我嘗試編譯並用shell腳本來運行它下面
CFLAGS='-Wpedantic -Wall -Wextra -Werror -std=c89'
gcc -o try ${CFLAGS} try.c -static
valgrind ./try -v --track-origins=yes
然後輸出是相當混亂:
==16641== Memcheck, a memory error detector
==16641== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==16641== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==16641== Command: ./try -v --track-origins=yes
==16641==
==16641== Conditional jump or move depends on uninitialised value(s)
==16641== at 0x419349: _int_free (in /home/su/ca/hw/1_try/try_static/trytrytry/try)
==16641== by 0x41D296: free (in /home/su/ca/hw/1_try/try_static/trytrytry/try)
==16641== by 0x46CCAE: fillin_rpath (in /home/su/ca/hw/1_try/try_static/trytrytry/try)
==16641== by 0x46D57A: _dl_init_paths (in /home/su/ca/hw/1_try/try_static/trytrytry/try)
==16641== by 0x44282B: _dl_non_dynamic_init (in /home/su/ca/hw/1_try/try_static/trytrytry/try)
==16641== by 0x443557: __libc_init_first (in /home/su/ca/hw/1_try/try_static/trytrytry/try)
==16641== by 0x400B77: (below main) (in /home/su/ca/hw/1_try/try_static/trytrytry/try)
但是,如果我刪除選項「-static」,一切順利。 我在ubuntu 16.04(虛擬機)和ubuntu 14.04(虛擬機) 上試過它也許它與虛擬機有關?
它與虛擬機無關,但依賴於你的'libc'和GCC。 –
如果您將'main()'更改爲'main(void)',是否會發生任何變化?這兩個在語義上是不同的,我只是猜測,但這可能有助於建立一個答案... – Sebivor
它不會改變任何東西。 –