对 gcc -static 选项或其在虚拟机中的行为感到困惑

标签 c gcc static-libraries valgrind static-linking

我写了一个简单的 c 程序 try.c

#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 初始化代码的内部错误,因此您应该忽略它。

关于对 gcc -static 选项或其在虚拟机中的行为感到困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42408344/

相关文章:

c++ - 如何在从顶级 add_subdirectory 添加的包上使用 find_package?

sdk - 如何在sdk中获取静态库?

c - 尝试访问结构中的空指针时出现段错误

c - 无法将用户输入数组传递给 execvp

c++ - fprintf()/std::cout 不会将字符串的一部分打印到 stdout/stderr

linux - -A 选项在 gcc 中有什么作用?

c++ - 如何使用 GCC 编译器创建新的 OpenCV Mat 对象?

objective-c - 在 Objective C 中声明和使用 C 函数

c - 使用 float 和 double 时,c 中的 -0.0000 是什么?

macos - 如何将 Objective C 静态库导入 Swift Framework?