美文网首页操场上玩电脑系列博客
C语言玩耍02 - include conflicts

C语言玩耍02 - include conflicts

作者: 绿月 | 来源:发表于2019-08-03 12:39 被阅读0次

    今天玩耍的是include conflicts
    如果你include多个header文件,要保证里面没有冲突的定义。
    比如这样:
    header1.h

    typedef char yes_t[6];
    

    header2.h

    typedef char yes_t[7];
    

    main.c

    #include <header1.h>
    #include <header2.h>
    #include <stdio.h>
    
    int main(int argc, char **argv) {
        yes_t a = "Yes!";
        puts(a);
        return 0;
    }
    

    gcc -o test -I. test.c

    In file included from test.c:2:0:
    ./header2.h:1:14: error: conflicting types for ‘yes_t’
     typedef char yes_t[7];
                  ^~~~~
    In file included from test.c:1:0:
    ./header1.h:1:14: note: previous declaration of ‘yes_t’ was here
     typedef char yes_t[6];
                  ^~~~~
    

    但是如果都是yes_t[6];,那就没事了。

    相关文章

      网友评论

        本文标题:C语言玩耍02 - include conflicts

        本文链接:https://www.haomeiwen.com/subject/jsljdctx.html