How to prevent multiple definitions in C?

后端 未结 7 1049
失恋的感觉
失恋的感觉 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: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

提交回复
热议问题