How can I avoid the LNK2005 linker error for variables defined in a header file?

无人久伴 提交于 2019-11-29 10:54:51

Keep in mind that a #include is roughly like cutting and pasting the included file inside the source file that includes it (this is a rough analogy, but you get the point). That means if you have:

int x;  // or "slider" or whatever vars are conflicting

in the header file and that header file is included by three source files in a program, then they will all have a global named x defined that will conflict.

What you want to do is define the variable as extern so that the .cpp files will all get the declaration, and then in ONE of your .cpp files give the actual definition.

in Variables.h:

extern int x;

in SomeSourceFile.cpp

int x;

Of course, I'd recommend against globals, but if you must use them this would keep them from conflicting.

This is because the compiler compiles each .cpp file separately, creating a .obj file for each one. Your header appears to have something like:

int slider;

When this is included into each of your three .cpp file, you get three copies of the int slider variable, just as if you had declared it in each .cpp file. The linker complains about this because you haven't have three different things with the same name.

What you probably want to do is change your header file to read:

extern int slider;

This tells the compiler that there is a slider variable somewhere, but possibly not here, and lets the linker figure it out. Then, in one .cpp file:

int slider;

gives the linker one actual variable to link.

Because "int slider" is already defined in another file? Check that you have header guards...

#ifndef _VARIABLES_H_
#define _VARIABLES_H_

int slider;

#endif

If it is across multiple translation units, and you do want the variables to be different (ie not global), then maybe declare them in an anonymous namespace:

namespace {
    int slider;
}

If you do want them global, look to James' solution.

Jess

What is happening is that each of the variables from Variables.h are given global scope for each of the individual c files. When the linker compiles all the c files, it sees multiple variables with the same name.

If you are wanting to use variables from the header file as global variables, then you will have to use the keyword "extern" in front of all of them, and in the main file don't use the keyword extern.

main c:

int n_MyVar;

other files:

extern int n_MyVar;

You can create two files Variables.h and EVariables.h, or just declare the variables in the main.cpp file.

A much better way to do this is to create a class of Variables and pass a reference to the class.

I know that this is an old thread, but I came across this as one of the first search results from Google. I solved the problem by placing the variable static.

namespace Vert
{
   static int i;
}

I tried extern and in my situation that didn't seem to solve the problem.

This linking error can also be avoided if the variables included multiple times via the "Variables.h" are declared as const.

I had this error too although I work with extern definitions. The problem was initializing the variables in the extern definitions too:

ID3D11VertexShader*         g_pVertexShader = nullptr;
...
extern ID3D11VertexShader*  g_pVertexShader = nullptr;  // here's the problem 

=> error

ID3D11VertexShader*         g_pVertexShader = nullptr;
...
extern ID3D11VertexShader*  g_pVertexShader;           // without initializing  

=> no error, problem solved

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!