【问题标题】:Is there a tool or compiler option for gcc that counts “lines of code compiled”?gcc 是否有计算“已编译代码行数”的工具或编译器选项?
【发布时间】:2019-02-18 22:04:11
【问题描述】:

为了比较 Linux 内核的编译时间,如果之后(实际上)编译的代码行数会非常有用。 在编译之前对内核源执行“cloc”只是解决方案的一部分,因为对于每个内核配置,这个值都是相等的,无论是否配置,例如,“tinyconfig”或“allyesconfig”或“localmodconfig”。

向 gcc 邮件列表添加了可能支持此问题的请求
(https://gcc.gnu.org/ml/gcc-help/2019-03/msg00057.html)

编辑(来自 Mike Kinghan 03/09/2019 回答下面的评论):
“这个数字应该是一个标准化因素,用于比较不同版本的 linux 内核与以前版本和未来几年的编译时间。”

【问题讨论】:

    标签: gcc compilation linux-kernel


    【解决方案1】:

    您似乎想计算编译器消耗的代码行数 预处理后。究竟是什么意思是有争议的,但我 假设一个合理的计数方式对你来说已经足够了 目的。一个 GNU Make 生成文件,例如:

    生成文件

    %.o: %.c    # Cancel built-in rule
    %.o: %.i
        $(CC) $(CFLAGS) -c $< -o $@
        @loc=$$(wc -l $<); echo "Compiled $${loc%% *} LOC from $<"
    
    %.i: %.c
        $(CC) $(CPPFLAGS) -E -P $< > $@
    

    说明了一种方法:-

    • 首先预处理源文件。
    • 计算预处理输出的行数。
    • 编译预处理的输出。

    Makefile 避免了两次预处理源代码的浪费, 因为 gcc 将扩展名 .i 识别为 signifying a file of already-preprocessed C source 并将 不再对其进行预处理。也不需要清理中间体 .i 文件,因为 GNU make 将它们识别为中间和自动删除 他们。示例:

    $ cat foo.c
    #ifdef FOO
    
    /* A
     * B
     * C
     */
    int foo(int x, int y)
    {
        while(x < y) {
            if (++x == y) {
                return y;
            }
            --y;
        }
        while(y < x) {
            if (++y == x) {
                return x;
            }
            --x;
        }
        return y;
    }
    
    #else
    
    /* D */
    int bar(int x) {
        return x * x;
    }
    
    #endif
    
    $ make foo.o
    cc  -E -P foo.c > foo.i
    cc  -c foo.i -o foo.o
    Compiled 3 LOC from foo.i
    rm foo.i
    
    $ rm foo.o
    $ make CPPFLAGS=-DFOO foo.o
    cc -DFOO -E -P foo.c > foo.i
    cc  -c foo.i -o foo.o
    Compiled 16 LOC from foo.i
    rm foo.i
    

    您可能希望在预处理步骤中传递-P option,如图所示,以抑制 生产线标记的输出会增加行数。例如。你可能不希望 foo.i 成为:

    $ cc -DFOO -E foo.c > foo.i
    $ cat foo.i
    # 1 "foo.c"
    # 1 "<built-in>"
    # 1 "<command-line>"
    # 1 "/usr/include/stdc-predef.h" 1 3 4
    # 1 "<command-line>" 2
    # 1 "foo.c"
    
    
    
    
    
    
    int foo(int x, int y)
    {
        while(x < y) {
            if (++x == y) {
                return y;
            }
            --y;
        }
        while(y < x) {
            if (++y == x) {
                return x;
            }
            --x;
        }
        return y;
    }
    

    而是希望它是:

    $ cc -DFOO -E -P foo.c > foo.i
    $ cat foo.i
    int foo(int x, int y)
    {
        while(x < y) {
            if (++x == y) {
                return y;
            }
            --y;
        }
        while(y < x) {
            if (++y == x) {
                return x;
            }
            --x;
        }
        return y;
    }
    

    显然,对于编译许多源文件的 make-system,您会设计 更多或不同的设备从添加每个文件的 LOC 报告。

    【讨论】:

    • 一个非常彻底的答案。
    • Thx wallyk,Thx @Mike Kinghan:在编译过程中(一次)测量/计算已处理代码的行时,这并不重要。这个数字应该是一个标准化因素,用于比较不同版本的 linux 内核在以前版本和未来几年的编译时间。这导致在对稳定的参考源代码进行一些有用的编译时,产生了一个真实世界(可比较的整体)硬件基准测试编号(只要 gcc 停留在 cpu 而不是 gpu stackoverflow.com/questions/8417053/…)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-02
    • 1970-01-01
    相关资源
    最近更新 更多