Sorry guys I know my english is bad, but i made examples so that my question is more clearer.
a.cpp
#include <iostream>
using namespace std;
void funcfoo(){
cout << "test only" << endl;
}
int varfoo = 10;
b.cpp
#include <iostream>
using namespace std;
extern void funcfoo();
extern int varfoo;
int main(){
funcfoo();
cout << varfoo;
return 0;
}
Then I compile it like this "cl b.cpp a.cpp"
My question is. How come when I remove the "extern keyword before void funcfoo()" it works fine, but when i remove the extern keyword
before int var foo I get an error?
The issue is what each one of the lines of code means. int varfoo is a definition of a variable, while void funcfoo() is only a declaration. You can provide multiple declarations of an entity, but only one definition. The syntax to provide a declaration and only a declaration of a variable is by adding the extern keyword: extern int varfoo; is a declaration
3.1 [basic.def]/2 A declaration is a definition unless it declares a function without specifying the function’s body (8.4), it contains the extern specifier (7.1.1) or a linkage-specification25 (7.5) and neither an initializer nor a function body [...]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With