Linker tool error LNK2005 when writing code with VS2019. There are two methods available online [1] :

  1. Change IDE, no Visual Studio;
  2. Add a line /FORCE:MULTIPLE to the link commandline; VS STL changed from import to static.

But in fact, it is a way to treat the symptoms rather than the root cause.

Wrong style

Error LNK2005"void __cdecl Merge(class std::vector<int,class std::allocator<int> > &,int,int,int)"(? Merge@@YAXAAV?$vector@HV?$allocator@H@std@@@std@@HHH@Z) has been defined in mergesort.objCopy the code

Code Style Examples

  • A.cpp
A()/ / define
Copy the code
  • B.cpp
#include"A.cpp"

main()
{
  A();
}
Copy the code

why

Official: Usually, this error means that a rule must be broken for definition, which allows only one definition for any template, function, type or object used in a given object file across the entire executable, and only one definition for externally visible objects or functions [2].

The compiler does not know that function A exists! It’s like knowing A class, but not knowing that little A is in that class.

We can look at declarations and definitions in action [3] :

  • The point of a declaration is to let the compiler know that it exists, and repeated declarations are ok;
  • The point of a definition is to let the compiler allocate space for it, or to create an object. Duplicate definitions are prohibited.

The compiler’s compilation unit is the individual C/CPP source files, and the compiler must be aware of the existence and form of the variables and functions used in each compilation unit. So all of the objects involved must be declared in each source file, but definitions can be placed in other source files, and the compiler automatically links them at the end.

The solution

Call b.cpp in a.cpp file [4] :

  1. There are complete function definitions in B.cpp, add the function declarations to a.cpp.
  2. Write the prototype of the fully defined function in b.cpp to a B.h file and add #include”B.h” to the a.cpp header.

Extension: The role of the c header file (.h) [5]

The.h file contains declarations of variables, arrays, and functions defined in the.c file of the same name.

  • .h file functions:

    1. Easy to develop: contains some common constants, structures, type definitions, functions, variable declarations required by the file;
    2. Make the scope of a function start at the place where the function is declared, not at the place where the function is defined (practice summary);
    3. Provide interface: A package can provide an interface to the outside world (for example, stdio.h).
  • What should be in the h file: constants, structures, type definitions, functions, variable declarations.

  • H files should not have: variable definitions, function definitions.

reference

[1] hongbin_xu.class STD ::vector. blog.csdn.net/hongbin_xu/… [2] Linker tool error lnk2005. docs.microsoft.com/zh-cn/cpp/e… [3] FreakZhang. C/C + + statement defined scope links. www.jianshu.com/p/cb96a82c1… [4] GoodPanpan. How in the c language source file calls another function of the source file. www.cnblogs.com/lpworkstudy… [5] Zqixiao_09.c header file (.h) function blog.csdn.net/zqixiao_09/…