Thread: linker error

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    24

    linker error

    Code:
    #include<stdio.h>
    #include<conio.h>
    void fun();
    void main()
    {
    int x=100;
    clrscr();
    printf("%d",x);
    fun();
    getch();
    }
    void fun(){
    extern int x;
    printf("%d",x);
    }

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Change the name of your external x and it will be clearer what the problem is:

    Code:
    void fun() {
        extern int x_extern;
        printf("%d", x_extern);
    }
    What happens when you try to compile with this definition? Notice that the x in your main function is not related to x_extern.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    24
    Quote Originally Posted by c99tutorial View Post
    Change the name of your external x and it will be clearer what the problem is:

    Code:
    void fun() {
        extern int x_extern;
        printf("%d", x_extern);
    }
    What happens when you try to compile with this definition? Notice that the x in your main function is not related to x_extern.

    same error

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Yes, the error occurs because you haven't provided a definition for x_extern. Renaming x to x_extern was just to make it clear that it's not the same x. Extern means to the compiler that you (usually) intend to link the definition of that variable in from an external file. For example, here is a suitable file, that, if compiled, will satisfy the linking requirement:

    Code:
    int x_extern = 12345;

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    c99tut's point is that, if a variable is local to one function, another function can't declare it as extern to gain access to it. In your code these is no relationship, whatsoever, between the x in main() and the x in fun().

    The reason for the linker error is that extern tells the compiler that the variable is defined elsewhere. There is no "elsewhere" in your program that defines your extern variable.

    If you want to pass a variable between two functions, either make it global (outside the scope of a function) or - preferable - define fun() so it accepts an argument of type int.

    Also, main() returns int, not void. <conio.h> and getch() are also not standard C.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linker error help
    By bonks in forum C Programming
    Replies: 14
    Last Post: 03-17-2011, 08:18 AM
  2. Linker error
    By TriKri in forum C Programming
    Replies: 3
    Last Post: 11-22-2006, 02:30 PM
  3. [Linker error]
    By Overlord in forum C++ Programming
    Replies: 3
    Last Post: 07-29-2006, 05:44 AM
  4. Linker error
    By w274v in forum C++ Programming
    Replies: 2
    Last Post: 03-13-2006, 08:01 AM
  5. Linker error
    By curlious in forum Linux Programming
    Replies: 3
    Last Post: 10-01-2004, 11:48 PM