困惑gcc的-static选项,或它在虚拟机
问题描述:
行为我写了一个天真的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(虚拟机) 上试过它也许它与虚拟机有关?
答
此错误是Valgrind默认情况下可能为suppresses的调用堆栈的一部分。抑制是特定于调用所属的共享库(即libc)的 - 因此,将程序构建为静态可执行文件会阻止Valgrind识别它应该忽略此错误。
无论如何,这个错误是libc初始化代码内部的,所以你应该忽略它。
它与虚拟机无关,但依赖于你的'libc'和GCC。 –
如果您将'main()'更改为'main(void)',是否会发生任何变化?这两个在语义上是不同的,我只是猜测,但这可能有助于建立一个答案... – Sebivor
它不会改变任何东西。 –