1

I have been trying a few things out with 'extern' keyword. I wrote this basic function and I am not sure why my print function is not working. Kindly help me in understanding it.

test1.h

    #pragma once
    #include<iostream>
    using namespace std;
    extern int a;
    extern void print();


test1.cpp

    #include "test1.h"
    extern int a = 745;
    extern void print() {

        cout << "hi "<< a <<endl;
    }

test2.cpp

    #include"test1.h"
    extern int a;
    extern void print();
    int b = ++a;
    int main()
    {
        cout << "hello a is " << b << endl;
        void print();
        return 0;

    }

Actual output  :

    hello a is 746

Expected output:

    hello a is 746
    hi 746
  • Have a read about [storage class specifiers](https://en.cppreference.com/w/cpp/language/storage_duration) – paddy May 21 '19 at 04:39
  • From the link above: _"The extern specifier is only allowed in the declarations of variables and functions (except class members or function parameters). It specifies external linkage, and does not technically affect storage duration, but it cannot be used in a definition of an automatic storage duration object, so all extern objects have static or thread durations. In addition, a variable declaration that uses extern and has no initializer is not a definition"_ – paddy May 21 '19 at 04:41
  • Possible duplicate of [Access extern variable in C++ from another file](https://stackoverflow.com/questions/12290451/access-extern-variable-in-c-from-another-file) – paddy May 21 '19 at 04:43
  • The issue has nothing to with `extern`– you would observe the same missing output if all the code were in the same file. Read about how to call functions in your favourite C++ book. – molbdnilo May 21 '19 at 05:24
  • BTW: functions are `extern` by default in C++. – molbdnilo May 21 '19 at 05:24
  • @molbdnilo in continuation to your statement " functions are extern by default in c++", does it mean all member functions/global functions have external linkage and we do need to use the extern keyword? I think static/const functions must use extern keyword when multiple files need to use it? – Coding beginner May 21 '19 at 08:12
  • @Codingbeginner You never need `extern` on a function. Functions can't be both `static` and `extern`, and they can't be `const`. – molbdnilo May 21 '19 at 08:17
  • @molbdnilo - Got it! Functions have external linkage by default unless you declare them static. Thanks for your time. Variables that are const for those we have to use extern to make external linkage . – Coding beginner May 21 '19 at 08:38

2 Answers2

1

test1.cpp

#include "test1.h"
int a = 745; //< don't need extern here
void print() { //< or here

    cout << "hi "<< a <<endl;
}

test2.cpp

#include"test1.h"
/* we don't need to redefine the externs here - that's
 what the header file is for... */
int b = ++a;
int main()
{
    cout << "hello a is " << b << endl;
    print(); //< don't redeclare the func, call it instead
    return 0;
}
robthebloke
  • 9,331
  • 9
  • 12
0

You need to use extern only when you are declaring variable/function, and define the variable in one of the cpp files,which include the header.

So, what you want to do is

test1.h

#pragma once
#include<iostream>
using namespace std;
extern int a;
extern void print();

test1.cpp

#include "test1.h"
int a = 745;
void print() {

    cout << "hi "<< a <<endl;
}

test2.cpp

#include"test1.h"
int b = ++a;
int main()
{
    cout << "hello a is " << b << endl;
    print();
    return 0;

}
akib khan
  • 451
  • 4
  • 9