How to prevent multiple definitions in C?

后端 未结 7 1053
失恋的感觉
失恋的感觉 2020-12-08 00:36

I\'m a C newbie and I was just trying to write a console application with Code::Blocks. Here\'s the (simplified) code: main.c:

#include 
#incl         


        
7条回答
  •  感情败类
    2020-12-08 00:51

    Including the implementation file (test.c) causes it to be prepended to your main.c and complied there and then again separately. So, the function test has two definitions -- one in the object code of main.c and once in that of test.c, which gives you a ODR violation. You need to create a header file containing the declaration of test and include it in main.c:

    /* test.h */
    #ifndef TEST_H
    #define TEST_H
    void test(); /* declaration */
    #endif /* TEST_H */
    

提交回复
热议问题