Error LNK2005, already defined?

I have 2 files, A.cpp and B.cpp, in a Win32 console application.

Both 2 files contain only the following 2 lines of code:

#include "stdafx.h" int k; 

When compiling, it throws an error

 Error 1 error LNK2005: "int k" (?a@@3HA) already defined in A.obj 

I do not understand what's going on.

Can someone explain this to me?

+58
c ++
Apr 6 2018-12-16T00:
source share
8 answers

Why is this a mistake?

You have violated one determination rule and, therefore, a binding error.

Suggested solutions:




If you need the same named variable in two cpp files, you need to use the Nameless namespace (anonymous namespace) to avoid the error.

 namespace { int k; } 



If you need to use the same variable for multiple files, you need to use extern .

hijras

 extern int k; 

a.cpp

 #include "Ah" int k = 0; 

B.cpp

 #include "Ah" //Use `k` anywhere in the file 
+90
Apr 6 '12 at 16:50
source share

In the project settings, add /FORCE:MULTIPLE to the Linkers command line options.

From MSDN : "Use / FORCE: MULTIPLE to create the output file, regardless of whether LINK finds multiple definitions for the character."

+67
Nov 02 '13 at 10:19
source share

If you want both to refer to the same variable, one of them must have int k; , and the other should have extern int k;

In this situation, you usually put the definition ( int k; ) in a single .cpp file and put the declaration ( extern int k; ) in the header, which should be included wherever you need access to this variable.

If you want each k be a separate variable that has the same name, you can mark it as static , for example: static int k; (in all files, or at least all but one file). Alternatively, you can anonymous namespace:

 namespace { int k; }; 

Again, in all, but not more than one of the files.

In C, the compiler is usually not so picky. In particular, C has the concept of "preliminary definition", so if you have something like int k; twice (in the same or separate source file), each will be considered a preliminary definition, and there will be no conflict between them. This can be a bit confusing, however, since you still don't have two definitions that include initializers - a definition with an initializer is always a complete definition, not a preliminary definition. In other words, int k = 1; appearing twice will be an error, but int k; in one place and int k = 1; otherwise it will not. In this case, int k; will be considered as a preliminary definition and int k = 1; as a definition (and both refer to the same variable).

+13
Apr 6 2018-12-12T00:
source share

Assuming you want "k" to be a different value in different .cpp files (hence declaring them twice), try changing both files to

 namespace { int k; } 

This ensures that the name "k" uniquely identifies "k" for the translation units. Old version of static int k; outdated.

If you want them to point to the same value, change it to extern int k; .

+6
Apr 6 '12 at 16:50
source share

Both files define the variable k as an integer ( int ).

As a result, the linker sees two variables with the same name and is not sure which one it should use if you ever refer to k .

To fix this, change one of the declarations to:

 extern int k; 

This means: "k is an integer declared here, but defined externally (that is, another file)."

Now there is only one variable k , which can be correctly mentioned by two different files.

+6
Apr 6 2018-12-12T00:
source share

And if you want these translation units to share this variable, define int k; in A.cpp and put extern int k; in B.cpp.

+3
Apr 6 2018-12-12T00:
source share

The presence of int k; in the header file, causes the definition of the character k in each translation unit that includes this header, while the linker expects it to be defined only once (the so-called violation of the definition rule).

Although the extern related suggestion is not wrong, extern is C-ism and should not be used.

A solution before C ++ 17 that would allow you to define a variable in the header file in several translation units without violating ODR would be a template conversion:

 template<typename x_Dummy = void> class t_HeaderVariableHolder { public: static int s_k; }; template<typename x_Dummy> int t_HeaderVariableHolder<x_Dummy>::s_k{}; // Getter is necessary to decouple variable storage implementation details from access to it. inline int & Get_K() noexcept { return t_HeaderVariableHolder<>::s_k; } 

With C ++ 17, things get a lot simpler as it allows inline variables:

 inline int g_k{}; // Getter is necessary to decouple variable storage implementation details from access to it. inline int & Get_K() noexcept { return g_k; } 
+2
Apr 12 '19 at 20:59
source share

The component reports that you specified the variable k several times. Indeed, you have a definition in A.cpp and another in B.cpp. Both compilation units create the corresponding object file, which the linker uses to create your program. The problem is that in your case, the linker does not know which definition of k use. In C ++, you can only have one definition of the same construct (variable, type, function).

To fix this, you need to decide what your goal is.

  • If you want to have two variables, both named k , you can use an anonymous namespace in both .cpp files, and then call k , as you are doing now:

.

 namespace { int k; } 
  • You can rename one of k to something else, thereby avoiding duplication of the fix.
  • If you want to have k definition only once and use it in both .cpp files, you need to declare one as extern int k; and leave it the same as in the other. This will allow the linker to use one definition (invariable version) in both cases - extern means that the variable is defined in another compiler.
+1
Apr 6 2018-12-12T00:
source share



All Articles