linker error extern...

  • Follow


Dear All,
 
I am trying to run a following small piece of code under vc++ compiler
ver 6, but getting linker error

main()
{
extern int i;
i = 20;
printf("%d",sizeof(i));
}

What can be the reason for linker error ?

Regards,
Rohit

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply rohit_dhamija 7/26/2004 5:09:21 PM

Rohit Dhamija wrote:
> 
> main()

This should be 'int main()', the "implicit int" rule inherited from K&R C
has been abolished both in Standard C++ and in C99 (but that has nothing to
to with your problem).

> {
> extern int i;

This is just a declaration of 'i', saying it is defined somewhere else in
the program.

> i = 20;
> printf("%d",sizeof(i));

Another nitpicking: "%d" is for printing 'int' values, but 'sizeof' yields
a result of type 'std::size_t' which is a 'typedef' for some unsigned type -
could be for instance 'unsigned int' (requiring "%u" in format string) or
'unsigned long' (requiring "%lu"). Your code may happen to work now by
accident on your target platform, but could break for instance when passing
onto a 64-bit machine.
The C++ way of printing is 'std::cout << sizeof(i);' which doesn't suffer
from the problem I described since the right output operator is chosen
based on the type of the expression you want to print.

> }
> 
> What can be the reason for linker error ?

2 possible solutions:
- Provide a definition of 'i' as global variable (i.e. outside any function).
  You can even put it into another compilation unit linked together with the
  unit containing your 'main' function; or
- Declare *AND* define 'i' as a local variable in 'main', i.e. get rid of the
  'extern'. (Furthermore, it is best to initialize variables on their definition,
  so put 'int i = 20;' into your main function. This is my preferred solution
  as global variables are best avoided if possible.

So your program should probably become

  #include <iostream>
  #include <ostream>
  int main()
  {
    int i = 20;
    std::cout << sizeof(i) << '\n';
    return 0; // Not mandatory, but personally I prefer it.
  }

Falk


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply ISO 7/26/2004 10:42:48 PM


Rohit Dhamija wrote:
> Dear All,
>  
> I am trying to run a following small piece of code under vc++ compiler
> ver 6, but getting linker error
> 
> main()

In standard C++ you cannot use "implicit int" but must explicitly
specify the return type of every function.  VC++ 6 tolerates this,
though.

> {
> extern int i;

This declares but does not define a global variable, i.

> i = 20;
> printf("%d",sizeof(i));

This is unsafe because the type of sizeof(i) is not int.  It is
safer to use the iostream library for output.

> }
> 
> What can be the reason for linker error ?

The fact that i is not defined.  There needs to be a definition of it
(a declaration without the "extern" and possibly with an initialiser)
at global scope.

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply Ben 7/26/2004 10:44:59 PM

rohit_dhamija@rediffmail.com (Rohit Dhamija) writes:

> main()

Implicit int times are gone for some time now.


> {
> extern int i;
> i = 20;
> printf("%d",sizeof(i));
> }
>
> What can be the reason for linker error ?

Please always copy&paste the linker error in posts like this.


The line

extern int i;

tells the compiler: there is a global variable i of type int; its definition
can be found somewhere else (e.g. in a different translation unit or father
down or farther up in this one).


It seems that your pogram doesn't live up to that promise.

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply Thomas 7/26/2004 10:47:20 PM

Rohit Dhamija wrote:

> Dear All,
>  
> I am trying to run a following small piece of code under vc++ compiler
> ver 6, but getting linker error
> 
> main()
> {
> extern int i;
> i = 20;
> printf("%d",sizeof(i));
> }
> 
> What can be the reason for linker error ?

You need to define the variable 'i' somewhere.  The 'extern' declaration
is essentially a promise that you will, at link time, provide an object
file that will resolve the name 'i'.
-- 
Antoun Kanawati
antounk.at@comcast.dot.net
[remove .dot and .at before use]

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply Antoun 7/26/2004 10:53:26 PM

Rohit Dhamija wrote:
 > extern int i;

In plain english: Compiler, I tell you now that there is an int called 'i'
somewhere.

 > What can be the reason for linker error ?

Where is above mentioned int?

Uli

-- 
Questions ?
see  C++-FAQ Lite: http://parashift.com/c++-faq-lite/  first !


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply Ulrich 7/27/2004 10:44:17 AM

Rohit Dhamija wrote:

 > Dear All,
 >
 > I am trying to run a following small piece of code under vc++ compiler
 > ver 6, but getting linker error
 >
 > main()

Syntax error, return type required, and for main it must be int.

 > {
 > extern int i;

Says that i is defined in another source file/translation unit,
and the linker will complain if not.

 > i = 20;
 > printf("%d",sizeof(i));

Another error: you must not call functions with no declaration
for them in scope in C++ (and even in C, you must not call a
variadic function with no declaration in scope).

 > }
 >
 > What can be the reason for linker error ?

Probably the fact that it was told by "extern" to look elsewhere
for a definition of i, and found none -- if you post the error
text it will be easy for people to know which error is causing
your trouble.

-- James.

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply James 7/27/2004 10:45:12 AM

6 Replies
99 Views

(page loaded in 0.143 seconds)


Reply: