I have something like this:
--includes.h
extern int count;
--main.cpp
#include "includes.h"
int count = 4;
--other.cpp
#include "includes.h"
cout<<count; // will output 4
but when I did this, the compiler errors out with the following message:
error LNK2001: unresolved external symbol "int count" (?count##3HA)
Any idea why I am getting this?
What is the best way to share variables across different files?
How can I define use a variable in one file, and modify that same variable in another file?
main.cpp
#include <iostream>
int y;
int testy();
int _tmain(int argc, _TCHAR* argv[])
{
std::cout << y;
std::cout<<testy();
std::cout << y;
return 0;
}
source.cpp
extern int y;
int testy(){return y++;}
This should help understand your issue...
You can try to put in into unnamed namespace
namespace{
extern int count = -1;
};
cpp:
std::cout << count;
You should define an extern int in a header and a int into one file, but this file should be without any reference to the header
Related
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 2 years ago.
endianness.cpp
#include "endianness.h"
#include <cstdint>
using namespace io;
void switch_endianness(uint16_t* n)
{
*n = (*n >> 8) | (*n << 8);
}
void switch_endianness(uint32_t* n)
{
...
}
void switch_endianness(uint64_t* n)
{
...
}
endianness.h
#ifndef ENDIANNESS_H
#define ENDIANNESS_H
#include <cstdint>
namespace io
{
void switch_endianness(uint16_t* n);
void switch_endianness(uint32_t* n);
void switch_endianness(uint64_t* n);
}
#endif
Trying to test out my switch_endianness function in app.cpp, I get an unresolved symbol error:
LNK2019 unresolved external symbol "void __cdecl io::switch_endianness(unsigned short *)" (?switch_endianness#io##YAXPEAG#Z) referenced in function main
app.cpp
#ifndef TEST_BUILD
#include <iostream>
#include "io/endianness.h"
int main(int argn, char** argv)
{
std::uint16_t y = 0x0000;
io::switch_endianness(&y);
std::cout << y;
}
#endif
How I understand and read thus far, its a linking/reference problem. I think my code is fine and should compile and run as intended, I did 'include in project' to my endianness files, perhaps there is something trivial I'm missing or doing wrong with referencing? I really can't seem to solve this.
In my endianness.cpp file I changed the function declarations to include the folder directory it was in and now it works.. (As it was complaining about this in the header file not being able to find function definition)
void io::switch_endianness(uint16_t* n)
{
...
}
I have wrote program to test static and extern keywords in C++.
source1.cpp
#include "Header.h"
using namespace std;
static int num;
int main(){
num = 1;
cout << num << endl;
func();
}
source2.cpp
#include "Header.h"
using namespace std;
extern int num;
void func(){
num = 100;
cout << num << endl;
}
Header.h
#ifndef HEADER_H
#define HEADER_H
#include <iostream>
void func();
#endif
When i compile this program it gives me a link error.
error LNK2001, LNk1120 unresolved externals.
What is the reason to causes this Link error?
This Link Error causes because of num variable declared as a static variable.
Even though the variable num is declared as an extern in the source2.cpp file, the linker won’t find it because it has been declared static in source1.cpp.
When you declared variable static, it is local to the file; it has file scope. That variable is unavailable outside of this file.
I found this:
How do I use extern to share variables between source files?
and its main answer is rather clear to me.
However I do not understand why this gives me an error:
x.h :
#pragma once
namespace x {
class A {
public: void func() const;
};
// extern A const a; // cannot move this out of the include file !!!
// extern int xi; // fine to remove from here
}
--- main.cpp ---
#include "stdafx.h"
#include "x.h"
namespace x { extern int xi; extern const A a ; } // instead of include file
extern int i;
int _tmain(int argc, _TCHAR* argv[])
{
std::cout << i << std::endl; // works
std::cout << x::xi << std::endl; // works
x::a.func();
return 0;
}
--- x.cpp ---
#include "stdafx.h"
#include "x.h"
namespace x
{
void A::func() const
{ std::cout << "x::A::func() called" << std::endl; }
const A a; // Problem if const
int xi = 234; // works
}
int i = 123; // works
error LNK2001: unresolved external symbol "class x::A const x::a" (?a#x##3VA#1#B)
(VisualStudio 2013)
Compiling the two files is fine, and I can build and run it if I remove the const keyword, or if I move the extern statement into the include file.
Thanks for an explanation (can't believe in a compiler bug) ;)
Namespace-scope const variables default to internal linkage (i.e., visible only within that translation unit). The extern is needed to override the default and give it external linkage (so that it can be accessed from a different translation unit).
I have this simple example and I can't get it to compile:
Three files: my.h, my.cpp, and use.cpp:
//my.h
extern int foo;
void print_foo();
void print(int);
//my.cpp
#include "my.h"
#include "../../stb_lib_facilities.h" //inlcudes cout, cin, etc
void print_foo(){
cout << foo << endl;
}
void print(int i){
cout << i << endl;
}
//use.cpp
#include <iostream>
#include "my.h"
int main(){
foo = 7;
print_foo();
print(99);
return 0;
}
When I try to compile it I get three errors:
LNK2001: extern "int foo"..
LNK2019: extern "int foo"..
LNK1120:
What am I doing wrong?
Thanks for any help
You do not have any definition of your global variable. In any of your .cpp files, but just in one of them, you should add this:
int foo = 0; // This is a definition
Your declaration:
extern int foo; // This is a declaration
Only tells the compiler that such a global variable exists, but then there is no place where you actually define it. Therefore, the linker will eventually complain that you have an undefined referenced symbol.
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
When to use extern in C++
(4 answers)
Closed 8 years ago.
Can please someone explain me how to link the functions # functions.cpp to main.cpp
note: I want both files functions.cpp and main.cpp to use the same variables from header.h
Thank you!
main.cpp
#include "Header.h"
#include <iostream>
using namespace std;
int multi();
int printOutRanomdNumber();
int main()
{
cout << "Eneter a number you want to multiply" << endl;
cout << multi() <<endl;
cout << printOutRanomdNumber();
system("pause");
return 0;
}
header.h
#ifndef _HEADER_
#define _HEADER_
#include <iostream>
using namespace std;
extern int randomNumber;
int multi();
int printOutRanomdNumber();
#endif
functions.cpp
#include "Header.h"
#include <iostream>
using namespace std;
int multi()
{
int x;
cin >> x;
return(x=x*x);
}
int printOutRanomdNumber()
{
cout << "Please enter a random number" << endl;
cin >> randomNumber;
return (randomNumber);
}
The error is because you've not defined int randomNumber in any of your files.
You need to define randomNumber in one of the .cpp files, I'm guessing functions.cpp makes more sense here.
Also you can get rid of these lines in main.cpp since you're including Header.h which provides the prototypes already.
int multi();
int printOutRanomdNumber();