Thread: extern outside of a function

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    167

    extern outside of a function

    if i hava somewhere in a code:
    Code:
    extern int yyparse();
    what does extern do ? I really couldn't find anything relevant. Can somebody please explain me what does this do of redirect me to some usefull link?

    Thank you!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    In this instance, it does nothing. A function prototype / declaration is assumed extern to begin with.

    The extern keyword is most often used when referring to external global data.
    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.

  3. #3
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    Function declarations are extern by default, so the given code is explicitly stating the default: therefore doing nothing.

  4. #4
    Registered User
    Join Date
    Dec 2005
    Posts
    167
    i don't understand it very well
    external in what way ?
    does this mean it will get the function content from a file in the header ?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Extern just means the definition - the code which does the real work - is somewhere else (not in this file).

    The somewhere else is either
    - another source file in your project.
    - in an object file in a pre-compiled library.
    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.

  6. #6
    Registered User
    Join Date
    Dec 2005
    Posts
    167
    ok... i understand now. and if I use gcc how can I link an object to the program I use?
    If it is in another source file this means that the file will be included in the header?

    Sorry for the possibly dumb question and thank you for being so patient when answering me!

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Code:
    // func.c
    void func ( void ) {
      printf("hello from func\n");
    }
    Code:
    // main.c
    extern void func ( void );
    int main ( ) {
      func();
      return 0;
    }
    Which we could compile with gcc using
    gcc main.c func.c

    For large projects, we compile each module separately, usually with the help of "make"
    gcc -c main.c
    gcc -c func.c
    gcc main.o func.o


    Now if func were part of a library (say libfunc.a), all that would change would be
    gcc main.c -lfunc
    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.

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by spank View Post
    ok... i understand now. and if I use gcc how can I link an object to the program I use?
    If it is in another source file this means that the file will be included in the header?
    Since the function is yyparse() I suspect its definition can be found in some file with a name like y.tab.c (or something_else.tab.c). yyparse() is the entry point of an LR(1) parser created by a program called yacc or bison. This program creates .tab.c files from its inputs.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM