Static Variable Link Error

static variable link error

You must define the statics in the cpp file.

Log.cpp

#include "Log.h"
#include <ostream>

string Log::theString; // <---- define static here

void Log::method(string arg){
theString = "hola";
cout << theString << endl;
}

You should also remove using namespace std; from the header. Get into the habit while you still can. This will pollute the global namespace with std wherever you include the header.

why static variable in a class gives a linking error?

First of all the definition of static variable is wrong.
Instead of my_class::m_fid = 0; you should define as int my_class::m_fid = 0; when you do that there will be no more linker error..

Another thing as per standard...

The definition for a static data member shall appear in a namespace
scope enclosing the member’s class definition.

Static variable link error in MSVC even though it is created in the cpp file

Jack Dingler's answer here

https://www.codeproject.com/Questions/585271/Aplusstaticplusmemberplusvariableplusexportpluserr

worked. So the issue is that the Windows linker needs to know that if it has to import or export the symbol. So we have to declare __declspec(dllexport) when we export (when we build the library) and __declspec(dllimport) for using it. We can switch that using a compilation flag. I am not sure why they designed the linker like this but anyway it works now.

Static variable link error, C++

The issue with the static member variable is that you have the definition occur in the header file. If you #include the file in multiple source files, you have multiple definitions of the static member variable.

To fix this, the header file should consist only of this:

#ifndef HEADER_H
#define HEADER_H
// In the header file
class A
{
public:
void f(){}
static int a;
};
#endif

The definition of the static variable a should be in one and only one module. The obvious place for this is in your main.cpp.

#include "header.h"
int A::a = 0; // defined here
int main()
{
}

linking error : multiple definition of static variable

So, this is a common mistake of misunderstanding how the header guards work.

Header guards save multiple declarations for one compilation unit, but not from errors during linking. One compilation unit implies a single cpp file.

E.g. apple.cpp includes apple.h and grapes.h, and apple.h in turn includes grapes.h. Then header guards will prevent the inclusion of the file grapes.h again during compilation.

But when the process of compilation is over, and the linker is doing its job of linking the files together, then in that case it sees two memory locations for the same static variables, since the header file was included in a separate translation unit, say apple2.cpp to which its trying to link, thus causing the multiple definition error.

The only way to resolve it is to move the definition of the static variable to a cpp file.

How can a static variable from other file accessed here on the C program below?

This statement

static int b=20;

means the variable b will only be visible in its translation unit i.e. in file1.c, in which you have defined it.

In the main(), which is supposed to be in a different file, you have only declared b but not used it. Try to use it and you will find that linker will fail because it will not find the definition of b.

int main()
{
int a=1;
extern int b; // note the type int in declaration. You have missed it.
printf("access b = %d\n", b); // <==== this won't work
return 0;
}

Try to compile your program with above changes and you will see that the linker will throw error.

With above mentioned changes in main(), just remove the static keyword from variable b definition in file1.c file and above program will link successfully because then b will be a global variable which has external linkage.

From the value() you are returning the value that variable b hold which has nothing to do with whether b is static or non-static.

The lifetime of a static variable is the entire run of the program. You can access the static variable outside of it's scope/translation unit if you have the address of that static variable. Check this:

File file1.c :

#include <stdio.h>

static int b = 20;

int * retaddr_b (void) {
return &b;
}

void print_b (void) {
printf ("b = %d\n", b);
}

File main.c:

int * retaddr_b (void);
void print_b (void);

int main(void)
{
print_b(); // print static variable b value
int * ptr_b = retaddr_b(); // ptr_b pointing to b
*ptr_b = 99; // dereference ptr_b and assign different value
print_b(); // print static variable b value
return 0;
}

Output:

b = 20
b = 99

Linker error when static member function access static private variable

1.Static variable means it is common for all the objects created for that class.
2.Whatever written in header file acts as a blueprint ie it doesnt allocate memory for it. For this reason all static variables are defined in cpp implementation file. Compiler allocates memory for those defined in cpp files.



Related Topics



Leave a reply



Submit