Thread: global var problems

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    51

    global var problems

    Hello, I've come across an interesting problem I haven't had before.

    Here is an example:

    I have a variable called testVar which should be available to any .c file that #includes test.h. For some reason though I receive this error:

    /tmp/ccKsh4uo.o: In function `myFunction':
    test.c: (.text+0x5): undefined reference to `testVar'
    collect2: ld returned 1 exit status
    Here is my code:

    Code:
    // File: test.h
    
    extern char testVar;
    Code:
    // File: test.c
    
    #include <stdio.h>
    #include "test.h"
    
    void myFunction()
    {
            testVar = 'b';
    }
    
    int main()
    {
            char testVar = 'a';
    
            myFunction();
    
            printf("%c", testVar);
    
            return 0;
    }
    Any ideas why this would happen?

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    did you compile test.o as a separate object (gcc -c if you're using gcc)? otherwise your compiler expects some source to contain the effective definition of testVar (the global one).

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    51
    Quote Originally Posted by root4 View Post
    did you compile test.o as a separate object (gcc -c if you're using gcc)? otherwise your compiler expects some source to contain the effective definition of testVar (the global one).
    I'm using:

    root@c64:/root/testarea# gcc test.c test.h

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    and where is defined testVar? it's not defined in test.c, so as expected your compiler does not agree.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    51
    Quote Originally Posted by root4 View Post
    and where is defined testVar? it's not defined in test.c, so as expected your compiler does not agree.
    testVar is defined in test.c in the main() function as indicated above

    char testVar = 'a';

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    test.h _declares_ a _global_ variable named testVar, defined nowhere in your code
    The testVar='a' you're speaking about is a _local_ variable defined in main(), not related at all to the other one...
    plus myfunction() uses obviously the global one, so check what you really want to do and fix it. Don't name local and global variables identically, despite this is authorized, it leads to misunderstandings like this one.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    And don't compile header files; only source files.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Global Variable Usage -- Failure in assignment
    By jake123 in forum C Programming
    Replies: 7
    Last Post: 02-15-2008, 02:30 PM
  2. Global or not Global
    By c_geek in forum C Programming
    Replies: 6
    Last Post: 12-19-2007, 02:03 PM
  3. Creating a global linked list?
    By TwistedJester in forum C Programming
    Replies: 1
    Last Post: 11-12-2007, 03:23 AM
  4. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  5. Problems with global hook! HELP!
    By LMZ in forum Windows Programming
    Replies: 3
    Last Post: 08-22-2006, 08:09 AM