how could i declare & define function in header file

Discussion in 'C++' started by thinktwice, Jan 13, 2006.

  1. thinktwice

    thinktwice Guest

    i have just made a test project :(win32 console)

    //file : func.h
    #ifndef _FUNC_H_
    #define _FUNC_H_

    void func1()
    {
    return;
    };

    void func2()
    {
    return;
    };

    #endif

    //file : use1.cpp
    #include "func.h"

    void user1()
    {
    func1();
    func2();
    }

    //file : use2.cpp
    #include "func.h"
    void user1()
    {
    func1();
    func2();
    }

    //file : main.cpp
    int main(int argc, char* argv[])
    {
    return 0;
    };

    but it failed when linking
    Linking...
    use2.obj : error LNK2005: "void __cdecl func1(void)" (?func1@@YAXXZ)
    already defined in use1.obj
    use2.obj : error LNK2005: "void __cdecl func2(void)" (?func2@@YAXXZ)
    already defined in use1.obj
    Debug/HelloWorld.exe : fatal error LNK1169: one or more multiply
    defined symbols found
    Error executing link.exe.

    but it works if the function is template function. why?
     
    thinktwice, Jan 13, 2006
    #1
    1. Advertising

  2. Use the 'inline' keyword, that's what it's for.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Jan 13, 2006
    #2
    1. Advertising

  3. thinktwice

    thinktwice Guest

    thanks , it works :)
     
    thinktwice, Jan 13, 2006
    #3
  4. thinktwice

    Guest

    Alf P. Steinbach wrote:
    > Use the 'inline' keyword, that's what it's for.


    Well, the inline keyword wasn't made so you could define functions in a
    header instead of a source file...they were made so that you could give
    the compiler the hint to inline that function sort of like a safer
    macro. You of course know this, but the OP might not. You don't
    inline everything so that you can write the code in a header instead of
    source file...you inline things that are short and you want to be
    fast...no cost of function call. Normally you declare in a header and
    define in source file. You don't do this with inline functions because
    the compiler needs to know about the body in order to inline the code
    and it needs to know this for each object file it compiles....the
    linker is not involved.
     
    , Jan 13, 2006
    #4
  5. * :
    >
    > Alf P. Steinbach wrote:
    > > Use the 'inline' keyword, that's what it's for.

    >
    > Well, the inline keyword wasn't made so you could define functions in a
    > header instead of a source file...they were made so that you could give
    > the compiler the hint to inline that function sort of like a safer
    > macro. You of course know this, but the OP might not. You don't
    > inline everything so that you can write the code in a header instead of
    > source file...you inline things that are short and you want to be
    > fast...no cost of function call. Normally you declare in a header and
    > define in source file. You don't do this with inline functions because
    > the compiler needs to know about the body in order to inline the code
    > and it needs to know this for each object file it compiles....the
    > linker is not involved.


    I'm sorry, but that's incorrect in almost all respects except the
    historical.

    However, it's a very very common misconception, and so it's nothing to be
    ashamed of not knowing.

    First, the inline keyword does not, in practice, give any reliable
    optimization hint: it is primarily in support of the one-definition rule,
    and not for optimization.

    Second, with a modern optimizing compiler there's no point in inlining
    things that are short, because the compiler does that if you instruct it
    optimize for speed and such inlining will increase speed (the compiler is
    generally much better at determining that than the programmer).

    Third, to do reliable machine code level inlining you'll have to use
    compiler-specific means instead of the 'inline' keyword -- however, as
    explained in the previous paragraph, that may not always be wise.

    Fourth, with a modern compiler the linker is indeed involved in inlining
    (look up "whole program optimization"), which then has nothing to do with
    the keyword 'inline'.

    Fifth, the compiler (+ linker) need not then have access to the textual
    function definition to do machine code inlining.

    And so on.

    The upshot is to use 'inline' for the case of function definitions in header
    files, and possibly also for self-documenting code (telling the _programmer_
    that "I expect this to be inlined, use freely without thinking of cost") and
    not for misguided optimization where it generally doesn't work, and where if
    it works it may have the opposite effect of what you want, _preventing_ the
    old compiler from doing a reasonable optimization.

    Hope this helps,

    - Alf

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Jan 13, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Philipp
    Replies:
    5
    Views:
    6,537
    Ron Natalie
    Oct 16, 2003
  2. robin liu
    Replies:
    3
    Views:
    865
    Robin Liu
    Apr 21, 2006
  3. =?gb2312?B?wfXquw==?=
    Replies:
    10
    Views:
    717
    Victor Bazarov
    Aug 1, 2007
  4. beet
    Replies:
    5
    Views:
    550
  5. beet
    Replies:
    2
    Views:
    1,974
    Jim Langston
    Jun 17, 2008
Loading...

Share This Page