Thread: Extern functions?

  1. #1
    Beginning game programmer Petike's Avatar
    Join Date
    Jan 2008
    Posts
    64

    Question Extern functions?

    Hi all,
    I have these .CPP files ("Main.cpp" and "RandomFunction.cpp"):

    Code:
    /*
     * "Main.cpp"
     */
    
    #include "RandomFunction.cpp"
    
    int main()
    {
        randomFunction();
    }
    Code:
    /*
     * "RandomFunction.cpp"
     */
    
    void randomFunction()
    {
        // Do some stuff
    }
    But when I launch the program, the compiler gives me a linker error: "RandomFunction.obj : error LNK2005: "void __cdecl randomFunction(void)" (?randomFunction@@YAXXZ) already defined in Main.obj"

    So how should I rewrite the code to fix the linker error (I think I should define somewhere the extern function)?


    Thanks.

    Petike

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You don't include .cpp files. You include .h files (which should just contain the prototype of the function, not the function itself).

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by tabstop View Post
    You don't include .cpp files. You include .h files (which should just contain the prototype of the function, not the function itself).
    It means you have to have (additionally) the following file
    Code:
    /*
     * "RandomFunction.h"
     */
    
    void randomFunction();
    and include it instead of cpp
    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

  4. #4
    Beginning game programmer Petike's Avatar
    Join Date
    Jan 2008
    Posts
    64
    Aha, so .CPP files should NEVER be included.

    Now I can place the function prototype in some header file, let's say "RandomFunction.h".
    But where can I place the WHOLE DEFINITION of that function? Because I don't want to have it in the "Main.cpp" file. Could you please write here the code?

    Thanks

    Petike

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Petike View Post
    Aha, so .CPP files should NEVER be included.

    Now I can place the function prototype in some header file, let's say "RandomFunction.h".
    But where can I place the WHOLE DEFINITION of that function? Because I don't want to have it in the "Main.cpp" file. Could you please write here the code?

    Thanks

    Petike
    in RandomFunction.cpp, just like you have it now.

  6. #6
    Beginning game programmer Petike's Avatar
    Join Date
    Jan 2008
    Posts
    64
    OK,
    Thanks you very much.

    Petike

  7. #7
    Beginning game programmer Petike's Avatar
    Join Date
    Jan 2008
    Posts
    64

    One more question...

    Hi again,
    I would have one more question:
    Should I use in the "RandomFunction.cpp" also the #include "RandomFunction.h" directive or it will be enough to use it just in the "Main.cpp" file?

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It is always a good idea to include the "x.h" in "x.c" or "x.cpp".

    There are two reasons:
    1. You want to make sure that both declaration and definition of the function is the same. If you only have the one or the other, the compiler may not be able to spot the difference between the one in the header file and in the source. By including the header in the source, the compiler will say "Ah, I've seen this function before, let's make sure it matches".

    2. In more advanced programming, it is probably necessary to do so, because you would have type declarations (structs, classes, etc) that you need to have in both places [and the goal of programming is to reduce duplication].

    --
    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.

  9. #9
    Beginning game programmer Petike's Avatar
    Join Date
    Jan 2008
    Posts
    64
    All right,
    Thanks

    Petike

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is it legal to have functions within functions?
    By Programmer_P in forum C++ Programming
    Replies: 13
    Last Post: 05-25-2009, 11:21 PM
  2. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  3. Extern Question, really confused
    By SourceCode in forum C Programming
    Replies: 10
    Last Post: 03-26-2003, 11:11 PM
  4. extern symbols, what do these mean exactly?
    By Shadow12345 in forum C++ Programming
    Replies: 8
    Last Post: 11-02-2002, 03:14 PM
  5. extern keyword and structures
    By GuitGentlyWeeps in forum C Programming
    Replies: 2
    Last Post: 01-30-2002, 07:02 AM