简体   繁体   English

何时对GCC使用-g标志

[英]When to use the -g flag to GCC

I'm trying to force ValGrind to tell me what's wrong with my program. 我试图迫使ValGrind告诉我程序有什么问题。 Every shred of documentation on the face of the Internet says that you must supply the -g option to GCC, but not one single document says whether you need this flag at compile-time or link-time (or both). Internet上的每一份文档都说您必须向GCC提供-g选项,但是没有一个文档说明您是在编译时还是在链接时 (或两者同时)使用此标志。 So which is it?? 那是什么?

The GNU ld documentation says that -g will be ignored, so it doesn't make much sense to pass it. GNU ld文档说-g将被忽略,因此传递它没有多大意义。 In general you pass -g to gcc (which really is a front-end for the whole compilation process and not just a compiler) and it will take care of it. 通常,您将-g传递给gcc (它实际上是整个编译过程的前端,而不仅仅是编译器),它将由它来处理。

GCC provides -g flag to get the debugging, So one you compile the program like Consider a code of example.c like: GCC提供了-g标志来进行调试,因此您可以像下面那样考虑example.c的代码来编译程序:

#include <stdio.h>
/* Warning: This program is wrong on purpose. */
int main()
{
int age = 10;
int height;
printf("I am %d years old.\n");
printf("I am %d inches tall.\n", height);
return 0;
}

By default if you compile say using make example It will trigger command 默认情况下,如果您使用make example说,它将触发命令

cc     example.c   -o example

Now you run command like 现在,您运行类似的命令

cc -g example.c -o example1

then you will find the size of the file example1 is greater than the size of example because -g flag enabled the debugging information. 那么您会发现size of the file example1size of example大于size of the file example1size of example因为-g标志启用了调试信息。

While running valgrind is -g flag is not required. 运行valgrind时,不需要-g标志。 -g is only required in compilation process. -g仅在编译过程中是必需的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM