problem with extern variables

This is a discussion on problem with extern variables within the C Programming forums, part of the General Programming Boards category; This is the first time I've used extern variables so I may be doing something wrong. I have a 4 ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    31

    problem with extern variables

    This is the first time I've used extern variables so I may be doing something wrong. I have a 4 program file with a header file "hotel.h". In hotel.h I have the following:
    Code:
    #define NLINE 41
    #define MAXROOM 10
    extern int fd;
    extern char[NLINE];
    When I try to compile this (on Linux) I get errors saying there are undefined reference to each place I tried to use either of the extern variables. I tried putting these in the main file before the main function and still got errors. Any ideas?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    So do you actually define these variables anywhere?

    [edit]http://c-faq.com/decl/decldef.html
    Last edited by Dave_Sinkula; 05-04-2006 at 08:00 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    31
    Yeah, they are currently defined in hotel.h.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    *ahem*

    Where?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    31
    Sorry, didn't read your post carefully enough. I use the int fd as a file descriptor for the open() system call and buf is used to get data from the read() system call.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No, he means in what file do those variables actually exist?


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    31
    There are three files that these two extern variables exist in. Each time one of the two is called I get an error saying they are undefined references to them. In the file openfile.c
    I use fd to try to open a file (i.e. fd = open("file", O_RDWR); ). It is used in a couple other files for reading from the file opened in openfile.c.

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,530
    > extern char[NLINE];
    But what is it's name?

    Like
    extern char myString[NLINE];
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    UmeŚ, Sweden
    Posts
    555
    An extern variable is much like a function prototype, you're just telling the compiler that the variable exists somewhere so when you compile your program the compiler expects to find this extern variable in some other source file. If the compiler can't find it then you've just made references to objects that don't exist. Hence, "undefined reference" errors.

  10. #10
    Registered User
    Join Date
    Apr 2006
    Posts
    31
    sorry, verbatim the hotel.h file is:

    Code:
    #define NLINE 41
    #define MAXROOMS 10
    
    extern int fd;
    extern char buf[NLINE];
    I do use these variables in other files. The errors from the compilation are:
    in file createfile.c
    undefined reference to fd
    in file openfile.c
    undefined reference to buf
    ...

    This indicates to me that the variables were found in those files and are. I looked. If this doesn't help I can post the entire program. The files aren't that big.

  11. #11
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    This is not a definition:
    Code:
    extern int fd;
    extern char buf[NLINE];
    This is:
    Code:
    int fd;
    char buf[NLINE];
    The linker is telling you that it doesn't exist because it doesn't exist. Make it exist exactly once.

    You may actually want to follow the link I posted and read what's there.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  12. #12
    Registered User
    Join Date
    Apr 2006
    Posts
    31
    I got it figured out. Thanks a lot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extern "C" problem
    By CodeBugs in forum C++ Programming
    Replies: 11
    Last Post: 06-10-2009, 09:14 AM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. Replies: 4
    Last Post: 10-17-2002, 10:09 PM
  5. extern keyword and structures
    By GuitGentlyWeeps in forum C Programming
    Replies: 2
    Last Post: 01-30-2002, 06:02 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21