How to prevent multiple definitions in C?

后端 未结 7 1025
失恋的感觉
失恋的感觉 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 */
    
    0 讨论(0)
  • 2020-12-08 00:52

    I had similar problem and i solved it following way.

    Solve as follows:

    Function prototype declarations and global variable should be in test.h file and you can not initialize global variable in header file.

    Function definition and use of global variable in test.c file

    if you initialize global variables in header it will have following error

    multiple definition of `_ test'| obj\Debug\main.o:path\test.c|1|first defined here|

    Just declarations of global variables in Header file no initialization should work.

    Hope it helps

    Cheers

    0 讨论(0)
  • 2020-12-08 00:56

    If you're using Visual Studio you could also do "#pragma once" at the top of the headerfile to achieve the same thing as the "#ifndef ..."-wrapping. Some other compilers probably support it as well .. .. However, don't do this :D Stick with the #ifndef-wrapping to achieve cross-compiler compatibility. I just wanted to let you know that you could also do #pragma once, since you'll probably meet this statement quite a bit when reading other peoples code.

    Good luck with it

    0 讨论(0)
  • 2020-12-08 01:05

    You shouldn't include other source files (*.c) in .c files. I think you want to have a header (.h) file with the DECLARATION of test function, and have it's DEFINITION in a separate .c file.

    The error is caused by multiple definitions of the test function (one in test.c and other in main.c)

    0 讨论(0)
  • 2020-12-08 01:12

    You actually compile the source code of test.c twice:

    • The first time when compiling test.c itself,
    • The second time when compiling main.c which includes all the test.c source.

    What you need in your main.c in order to use the test() function is a simple declaration, not its definition. This is achieved by including a test.h header file which contains something like:

    void test(void);
    

    This informs the compiler that such a function with input parameters and return type exists. What this function does ( everything inside { and } ) is left in your test.c file.

    In main.c, replace #include "test.c" by #include "test.h".

    A last point: with your programs being more complex, you will be faced to situations when header files may be included several times. To prevent this, header sources are sometimes enclosed by specific macro definitions, like:

    #ifndef TEST_H_INCLUDED
    #define TEST_H_INCLUDED
    
    void test(void);
    
    #endif
    
    0 讨论(0)
  • 2020-12-08 01:12

    If you have added test.c to your Code::Blocks project, the definition will be seen twice - once via the #include and once by the linker. You need to:

    • remove the #include "test.c"
    • create a file test.h which contains the declaration: void test();
    • include the file test.h in main.c

    0 讨论(0)
提交回复
热议问题