C++: Multiple definition error for global functions in the header file

后端 未结 5 1438
陌清茗
陌清茗 2021-02-12 17:52

This function is global and is defined in the header file (temporarily I want to keep it there).

The header file also constitutes a particular class which has i

相关标签:
5条回答
  • 2021-02-12 18:10

    mark it as inline:

     inline void globalfunc() { 
     }
    

    although doing so means that it will no longer strictly be global - you will get a copy in each translation unit that uses the header, but the linker won't object to this.

    0 讨论(0)
  • 2021-02-12 18:11

    For a global function defined in a header file, declaring it within an un-named namespace should/will also work. According to C++ How to Program by Deitel, in C++ an unnamed namespace is preferable to static.

    So you could do this:

    // \file GlobalFunctions.h
    
    namespace  // an un-named namespace
    {
    
    void GlobalFunctionOne() {...implementation...}
    
    } // end un named namespace
    
    0 讨论(0)
  • 2021-02-12 18:13

    If you put a function in the header it will be generated for each c/cpp file that includes that header leading to duplicates. Making it inline will help.

    Edit, explanation

    Header guards as the #ifndef, #define ... #endif construction is often called only prevent double and recursive inclusion in a single cpp file. This is relevant in the case where a source file includes headers A and B and B also includes A. Recursive inclusion would happen if A also included B.

    Your problem arises because you have multiple .cpp files. During compilation of one cpp the compiler doesn't know about the existence of the other cpp files.

    Notice that #include, #ifdef and friends are preprocessor directives. Preprocessing happens on source files before compilation (thought it is often regarded and done as part of the compilation process). The preprocessor basically is a text processor. For instance an #include is textually replaced with the contents of the header file. Contents of #ifdefs that evaluate to false are removed from the code. The actual compiler gets one single big file consisting of the cpp and all referenced include files which it translates into an object file.

    0 讨论(0)
  • 2021-02-12 18:13
    #ifndef SOMESTRING
    #define SOMESTRING
    ... header code
    #endif
    

    The code of the header will only be included the first time.

    0 讨论(0)
  • 2021-02-12 18:24

    You have 2 options:

    Mark it as inline, as explained by nbt, or as static.

    inline will take the implementation of the global function from the source and copy it into wherever the function is called.

    inline void global_func ()
    {
    ...
    }
    

    static will tell the linker to not copy the code into the new object file but rather only reference it in the original.

    static void global_func ()
    {
    ...
    }
    
    0 讨论(0)
提交回复
热议问题