Problems compiling two c programs together

This is a discussion on Problems compiling two c programs together within the C Programming forums, part of the General Programming Boards category; setup.c has the global variable login. How come this isn't variable visible in main.c? Here is the code and output ...

  1. #1
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329

    Problems compiling two c programs together

    setup.c has the global variable login. How come this isn't variable visible in main.c? Here is the code and output that I get.

    Code:
    $ more setup.c
    #include <pwd.h>
    #include <string.h>
    /*#include "me.h"*/
    
    #define MAXLOGIN 64
    
    char login[MAXLOGIN];
    
    void init(void)
    {
      struct passwd *pwd;
    
      if ((pwd = getpwuid(geteuid())) == NULL)
        strcpy(login, "unknown");
      else
        strcpy(login, pwd->pw_name);
    }

    Code:
    $ more main.c
    /*I gave up trying to read Dr. Thalers code*/
    
    #include <stdio.h>
    #include <stdlib.h>
    /*#include "me.h"*/
    
    int main(int argc, char *argv[]) 
    {
    
      /*begin the clownsuit and bra masturbation session*/
      init();
    
      printf("%s\n", login);
      exit(EXIT_SUCCESS);
    }

    $ gcc -g setup.c main.c -o yapp
    main.c: In function ‘main’:
    main.c:13: error: ‘login’ undeclared (first use in this function)
    main.c:13: error: (Each undeclared identifier is reported only once
    main.c:13: error: for each function it appears in.)

  2. #2
    Registered User
    Join Date
    Aug 2008
    Location
    Belgrade, Serbia
    Posts
    163
    Use
    Code:
    extern char login[MAXLOGIN];
    in main.c.
    Vanity of vanities, saith the Preacher, vanity of vanities; all is vanity.
    What profit hath a man of all his labour which he taketh under the sun?
    All the rivers run into the sea; yet the sea is not full; unto the place from whence the rivers come, thither they return again.
    For in much wisdom is much grief: and he that increaseth knowledge increaseth sorrow.

  3. #3
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    I don't think you need to specify the size.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Brafil View Post
    I don't think you need to specify the size.
    No, the extern declaration doesn't have to have a size. But as long as it is a constant declared in one place, why not make it obvious what the size is, rather than leaving it empty to then have to find the original definition of the variable to see what the actual size is. Obviously, it would be a bad idea to put a constant "magic" number in there, e.g. 100 - because it's hard to do that.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    Just in case the size might change and someone forgets this file... well, it really doesn't matter.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Brafil View Post
    Just in case the size might change and someone forgets this file... well, it really doesn't matter.
    That's why I said, phrazed slightly differently, something lie "as long as it's not a magic number, but a #define or const definition, it's fine" - obviously also assuming that the constant is defined in a single header-file that is included in both places, of course.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Aug 2008
    Location
    Belgrade, Serbia
    Posts
    163
    Yes, that's what I assumed.
    Vanity of vanities, saith the Preacher, vanity of vanities; all is vanity.
    What profit hath a man of all his labour which he taketh under the sun?
    All the rivers run into the sea; yet the sea is not full; unto the place from whence the rivers come, thither they return again.
    For in much wisdom is much grief: and he that increaseth knowledge increaseth sorrow.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems about gcc installation
    By kevin_cat in forum Linux Programming
    Replies: 4
    Last Post: 08-09-2005, 09:05 AM
  2. problems compiling
    By dnr72 in forum C Programming
    Replies: 3
    Last Post: 01-17-2005, 03:29 PM
  3. Singleton problems
    By techrolla in forum C++ Programming
    Replies: 16
    Last Post: 01-02-2005, 03:05 PM
  4. executing c++ programs on the web
    By gulti01 in forum C++ Programming
    Replies: 4
    Last Post: 08-12-2002, 03:12 AM
  5. Release MFC Programs & Dynamic MFC DLL :: MFC
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 05-18-2002, 06:42 PM

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