multiple definition error c++

前端 未结 2 1247
星月不相逢
星月不相逢 2020-12-09 00:43

My \'Headers.h\' file includes basic c++ Headers

#include 
#include 
// and many header files.

wrote a funct

2条回答
  •  醉话见心
    2020-12-09 01:39

    Before actually compiling source the compilation unit is generated from .cpp files. This basically means that all preprocessor directives are computed: all #include will be replaces with content of the included files, all #define'd values will be substituted with corresponding expressions, all #if 0 ... #endif will be removed, etc. So after this step in your case you'll get two pieces of C++ code without any preprocessor directives that will both have definition of same function bool ifFileExist() that is why you get this multiple definition error.

    The fast solution is to mark it as inline bool ifFileExist(). Basically you ask compiler to replace all corresponding function calls with content of the function itself.

    Another approach is to live the declaration of your function in common_utility.h and move definition to common_utility.cpp

提交回复
热议问题