“Multiple definition”, “first defined here” errors

后端 未结 5 739
有刺的猬
有刺的猬 2020-12-30 23:44

I have 3 projects: Server, Client and Commons. Making header & source pairs in Commons doesn\'t cause any problems and I can access t

5条回答
  •  借酒劲吻你
    2020-12-31 00:46

    You should not include commands.c in your header file. In general, you should not include .c files. Rather, commands.c should include commands.h. As defined here, the C preprocessor is inserting the contents of commands.c into commands.h where the include is. You end up with two definitions of f123 in commands.h.

    commands.h

    #ifndef COMMANDS_H_
    #define COMMANDS_H_
    
    void f123();
    
    #endif
    

    commands.c

    #include "commands.h"
    
    void f123()
    {
        /* code */
    }
    

提交回复
热议问题