Thread: question about declaring function extern

  1. #1
    Linux is where it's at movl0x1's Avatar
    Join Date
    May 2007
    Posts
    72

    question about declaring function extern

    Hi. I see people declare functions as 'extern' in header (.h) files.
    Is this what I should be doing?

    someheader.h
    Code:
    /* someheader.h */
    
    extern int func(int a, int b);
    func.c
    Code:
    /* func.c */
    #include "someheader.h"
    
    int func(int a, int b)
    {
      return a * b;
    }
    I usually do the same as above, but don't put the 'extern' in front
    of the function declaration in the header file, and it seems to work.
    Is this ok, or should I prepend it with extern?

    thanks alot
    Remember that all that code you write turns into this:

    0100100100110010010011100100111001001
    0010100100100001001111100010010010010 ....

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    functoins are by default extern - so you do not need to specify it explicitely...

    When function has a static linkage - you need to specify it explicitely... but in this case - you do not want to put the function prototype in the header.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Linux is where it's at movl0x1's Avatar
    Join Date
    May 2007
    Posts
    72
    thanks vart
    Remember that all that code you write turns into this:

    0100100100110010010011100100111001001
    0010100100100001001111100010010010010 ....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. CreateThread() function question
    By chiefmonkey in forum C++ Programming
    Replies: 5
    Last Post: 05-15-2009, 07:52 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM