[Turn] use anonymous namespace in C ++ to solve the problem of duplicate definitions

Original: https://blog.csdn.net/pi9nc/article/details/11267031

The use of anonymous namespace in C ++ to solve the problem of duplicate definitions

Today write code when they met in the C ++ compiler multi-unit result in duplicate definitions (multi definition) link problem. In fact, this problem previously encountered several times, anxious to compile the code did not go to get to the bottom of some knowledge behind it. Today, the system read some information, be considered to be completely clear up this problem. Here to do a simple summary of it:

  • C ++ compiler has problems due to the separation of the stencil, often resulting in the need to add header file or define the variables defined function code, thereby causing a problem of multi-definition redefined at link multiple compilation unit.
  • static key traditional C language in C ++ for the other members of the class of semantics, leading to the limitations of its function.
  • Recommended in C ++ class implements the use of anonymous namespace defines a function or variable limited within a compilation unit, to avoid the problem of multi-definition.

In C ++, and the introduction of the concept of object-oriented, resulting in a header file and sometimes have a function to make or tagging variable definition. For example, the template does not support separate compilation on most compilers, resulting in many implementations of template classes only in header files, libraries, etc. as boost uses a lot of library hpp header format fully documented.

However, this method often leads to a problem is multi-definition redefinition, in larger projects often take the form of more than a compilation unit generates multiple .o files, and then build the executable file and then ld link. If multiple .o both include the same hpp file, which hpp file also contains the global variables, define some variables static class members, etc., then it will lead to a multi-definition gcc error. In traditional C program you can declare a static variable or function does not generate a global symbol to solve this problem, but the static keyword in C ++ for the other members of the class with semantics. So in C ++ it is recommended to use an anonymous namespace instead of static avoid the problem of multi-definition. A look at the following example:

----------- test.hpp----------

#include <string>

class
A

{

public:

    static
std::string y;

};

std::string A::y = std::string();

 

----------- test_comm.cpp----------

#include "test.hpp"

void
func() { }

 

----------- test_main.cpp----------

#include "test.hpp"

void
func();

int
main(int
argc, char
*argv)

{

    func();

}

We have the following error appears on the compile g ++:

leoxiang@SEC38_64_sles10:~$ g++ -c test_comm.cpp

leoxiang@SEC38_64_sles10:~$ g++ -c test_main.cpp

leoxiang@SEC38_64_sles10:~$ g++ -o test
test_comm.o test_main.o

test_main.o:(.bss+0x0): multiple definition of `A::y'

test_comm.o:(.bss+0x0): first defined here

collect2: ld returned 1 exit
status

Here to solve this problem through an anonymous namespace, just need test.hpp realization surrounded by anonymous namespace to avoid the problem of duplicate definitions:

----------- test.hpp----------

#include <string>

namespace
{

class
A

{

public:

    static
std::string y;

};

std::string A::y = std::string();

}

In fact the role of the anonymous namespace where the variables are placed in a random name space, and ensure that the name change is the only space in multiple translation units. Because anonymous namespace declared variable or function is defined globally visible, so will not have to compile the file where their impact, which is the realization of the role before the static keyword in C language, and has better usability.

But the anonymous namespace is not perfect, the following article "C ++ engineering practice (1): caution anonymous namespace" introduces two issues will lead to the use of anonymous namespace:

  • Which it is difficult to set a breakpoint function, if you're like me using gdb such text mode debugger.
  • With some versions of g ++, the same file each time compiled binary files change, which makes some of the build tool failure.
  • At the same time the use of anonymous space in the header file will also cause some libraries use this trap, similar to the use of static variables in C header files.

Even so, Google C ++ Programming Specification 2.1 are also encouraged to use the namespace instead of the static keyword in C language. So personally feel some special cases if the class must be defined in the header file static variables or functions, you can consider the entire file code in an anonymous namespace wrap, can better solve the problem of duplicate definitions.

Guess you like

Origin www.cnblogs.com/daryl-blog/p/11003108.html