用gcc 的-D 参数来调试代码

写代码免不了要加入调试信息,在程序头定义一个DEUBG开关很烦,不过gcc早就想到了,有-D选项。
man gcc
或者
man gcc | col -b > gcc.txt
相关信息如下:
GCC(1)                               GNU                               GCC(1)



NAME
       gcc - GNU project C and C++ compiler

SYNOPSIS
       gcc [-c|-S|-E] [-std=standard]
           [-g] [-pg] [-Olevel]
           [-Wwarn...] [-pedantic]
           [-Idir...] [-Ldir...]
           [-Dmacro[=defn]...] [-Umacro]
           [-foption...] [-mmachine-option...]
           [-o outfile] infile...

       Only the most useful options are listed here; see below for the
       remainder.  g++ accepts mostly the same options as gcc.

-----------------------

       Preprocessor Options
           -Aquestion=answer -A-question[=answer] -C  -dD  -dI  -dM  -dN
           -Dmacro[=defn]  -E  -H -idirafter dir -include file  -imacros file
           -iprefix file  -iwithprefix dir -iwithprefixbefore dir  -isystem
           dir -M  -MM  -MF  -MG  -MP  -MQ  -MT  -nostdinc -P  -fwork-
           ing-directory  -remap -trigraphs  -undef  -Umacro  -Wp,option
           -Xpreprocessor option
----------------------
       -D name
           Predefine name as a macro, with definition 1.

       -D name=definition
           Predefine name as a macro, with definition definition.  The con-
           tents of definition are tokenized and processed as if they
           appeared during translation phase three in a #define directive.
           In particular, the definition will be truncated by embedded new-
           line characters.

           If you are invoking the preprocessor from a shell or shell-like
           program you may need to use the shell's quoting syntax to protect
           characters such as spaces that have a meaning in the shell syntax.

           If you wish to define a function-like macro on the command line,
           write its argument list with surrounding parentheses before the
           equals sign (if any).  Parentheses are meaningful to most shells,
           so you will need to quote the option.  With sh and csh,
           -D'name(args...)=definition' works.

           -D and -U options are processed in the order they are given on the
           command line.  All -imacros file and -include file options are
           processed after all -D and -U options.


       -U name
           Cancel any previous definition of name, either built in or pro-
           vided with a -D option.
-----------------------------------
 -D 定义宏(D-define)

-D定义宏有两种情况,一种是-DMACRO 相当于程序中使用#define MACRO 另外可以-DMACRO=A 相当于程序中使用#define MACRO A 这只是一个编绎参数,在连接时没有意义
如: $gcc -c hello.c -o hello.o -DDEBUG
上面为hello.c定义了一个DEBUG宏,某些情况下使用-D 代替直接在文件中使用#define,也是为了避免修改源代码双。例如一个程序希望在开发调试的时候能打印出调试信息,而正式发布的时候就不用打印了,而且发布前不用修改源代码双。可以这样
#ifdefine DEBUG
printf("debug message/n");
#endif
对于这段代码,平时调试的时候就加上-DDEBUG 发布时不用-D选项
与之对应的是-UMACRO参数,相当于#undef MACRO,取消宏定义

============hello.c======
#include <stdio.h>
#include <stdlib.h>

int main()
{
#ifdef DEBUG
        printf("this is before/n");
#endif
        printf("helloworld/n");
#ifdef DEBUG
        printf("this is end/n");
#endif
        exit(0);
}

$gcc -c hello.c -o hello.o -DDEBUG -Wall
则./hello输出:
this is before
helloworld
this is end

$gcc -c hello.c -o hello.o -Wall
则./hello输出:
helloworld

方便!

 

转自:http://liuhongdan.bokee.com/6395572.html

 

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: gcc-.tar.gz是一个软件包的压缩文件格式,通常用于Linux或类Unix系统中的软件安装。它是以.tar.gz为扩展名的文件,其实际上是两个文件格式的组合。 首先,.tar(Tape Archive)是一种归档文件格式,用于将多个文件或目录组织在一起,以便于传输和存储。它类似于Windows系统中的zip文件。.tar文件通常不进行压缩,只是将多个文件或目录打包在一起。 而.gz是指使用GNU压缩程序gzip进行压缩生成的文件,它能够对单个文件进行压缩,减小文件大小。gzip是一种无损压缩算法,通过消除文件中的冗余信息来减小文件大小,但能够完全还原原始文件。 因此,当我们提到gcc-.tar.gz时,它指的是一个包含gcc编译器源代码或二进制执行文件等文件的归档文件,并通过gzip进行了压缩以减小文件大小。这种压缩的方式有助于在网络传输或存储时减少带宽和存储空间的占用。 要使用gcc-.tar.gz,我们通常需要将文件解压缩,并按照相应的安装指南进行安装和配置,以便在系统中编译和运行C和C++代码。这种文件格式在开源软件社区中非常常见,方便了软件的分发和安装,同时也使得开发者和用户能够更好地进行自定义调试工作。 ### 回答2: gcc-.tar.gz 是一个打包格式的文件。它通常用于将GCC编译器的源代码进行压缩和打包。这个文件格式是使用tar和gzip两个工具进行压缩和解压的。 首先,tar是一个在Unix和类Unix系统中常见的打包工具。它可以将多个文件或目录打包成一个单独的文件,以便进行传输或备份。tar的命令参数包括-c (create)用于创建打包文件,-z (gzip)用于使用gzip进行压缩,-f (file)用于指定要创建的打包文件的名称。 而gzip则是一个用于数据压缩和解压缩的工具。它通常与tar搭配使用,可以将tar打包的文件进行进一步的压缩,以减小文件大小。gzip的命令参数包括-d (decompress)用于解压缩文件,-c (stdout)用于将解压缩的数据输出到标准输出,而非生成一个新文件,-f (file)用于指定要解压缩的文件的名称。 因此,当我们看到一个文件名以".tar.gz"结尾时,我们可以理解它是一个经过tar打包并使用gzip压缩过的文件。要解压缩这样的文件,我们可以使用tar命令的-x (extract)参数解压缩文件,并使用gzip的-d参数对解压缩后的文件进行解压。 总而言之,gcc-.tar.gz是一个经过tar打包和gzip压缩的文件,通常用于存储和传输GCC编译器的源代码。 ### 回答3: gcc-.tar.gz是GCC编译器的压缩文件格式。GCC(GNU Compiler Collection)是一套使用广泛的免费开源编译器工具,用于编译并生成各种编程语言的可执行文件。 .tar.gz是一种Unix/Linux系统下的压缩文件格式,由两部分组合而成。首先,.tar是指使用tar命令创建的存档文件,它将多个文件和文件夹打包成一个容器。其次,.gz是指使用gzip命令进行压缩的文件,可以减小文件大小。所以,gcc-.tar.gz就是将GCC编译器的文件和文件夹打包并进行压缩后得到的一个压缩文件。 要使用gcc-.tar.gz文件,首先需要解压缩。在Linux系统中,可以使用tar命令进行解包,再使用gzip命令进行解压缩。具体的命令如下: tar -zxvf gcc-.tar.gz 这个命令将会解压缩gcc-.tar.gz文件,并将其还原为原始的GCC编译器文件和文件夹。解压后,可以按照GCC的安装说明进行安装。一般来说,需要在解压缩后的文件夹中执行configure脚本、make命令和make install命令完成安装过程。 总之,gcc-.tar.gz是GCC编译器的压缩文件格式,在使用之前需要进行解压缩,并按照相应的安装流程进行安装。这样就可以使用GCC编译器来编译程序了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值