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.
Related
ConsoleApplication1.cpp
#include <iostream>
#include "Test.h"
#include "Head1.h"
int main()
{
std::cout << "Hello World!\n";
}
Test.h
#pragma once
int AAA = 1;
Head1.h
#pragma once
#include "Test.h"
Head2.h
#pragma once
class Head2C
{
public:
void Print();
};
Head2.cpp
#include "Head2.h"
#include "Head1.h"
#include <iostream>
using namespace std;
void Head2C::Print()
{
cout << "Head2::Print() " << endl;
}
Why #pragma once can't work for this sample code?
The linker error : error LNK2005: "int AAA" (?AAA##3HA) already defined in ConsoleApplication1.obj
Every non-constant variable should be defined only once in the entire program. #pragma once only ensures that it is defined once for each translation unit (cpp file). With two translation units, you are defining the variable twice in the entire program.
Make AAA constant or move the definition into a cpp file.
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 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
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
const and global
This code will produce error in c++
// Foo.cpp
const int Foo = 99;
// Main.cpp
extern const int Foo;
int main()
{
cout << Foo << endl;
return 0;
}
Reason as given by many is global const has internal scope and it is default static.
solution to this is :-
//Foo.h
extern const int Foo;
// Foo.cpp
#include "Foo.h"
const int Foo = 99;
// Main.cpp
#include "Foo.h"
int main()
{
cout << Foo << endl;
}
I used to think that extern is used to tell compiler that memory for the indentifer is already allocated somewhere in other files.
Applying same logic on above code can anyone explain what is happening here or extern has different meaning in c++??
enter link description here
Also consider this page it is spoiling my all intuitions..
Added an extern ... line to the CPP, which - I think - kills the internal linkage behavior of the next line.
// Foo.cpp
extern const int Foo;
const int Foo = 99;
Also made some unrelated corrections to Main:
// Main.cpp
#include <iostream>
extern const int Foo;
int main()
{
using namespace std;
cout << Foo << endl;
return 0;
}
They are #include <iostream> and using namespace std;.
This answer is not carefully reasoned theoretically, but works for me with g++.