Thread: inline function in header, multiple definition

  1. #1
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229

    inline function in header, multiple definition

    Say I have a header file, a.h
    Code:
    #ifndef A_H
    #define A_H
    
    int some_util_function() {
    ...
    }
    
    #endif
    and b.cpp, c.cpp, both needs to use this function.

    If I compile them separately then link them, I get a multiple definitions error.

    Code:
    gcc -c b.cpp
    gcc -c c.cpp
    gcc b.o c.o #multiple definitions
    What's the standard way to fix this? (Except moving it to another .c)

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You also need to declare the function as inline using the inline keyword.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Cool! Thanks!

    I thought "inline" is only a hint for the optimizer.

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    You need to use the 'inline' keyword when defining some_util_function. Otherwise, you'll have to compile it into it's own object file.

    EDIT:
    Nevermind
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by cyberfish View Post
    Cool! Thanks!

    I thought "inline" is only a hint for the optimizer.
    You thought wrong.

    "inline" has a couple of purposes.

    1) an instruction to the compiler that the same function definition may be seen by multiple compilation units (eg .c files). The compiler is required to do the necessary jiggery-pokery to ensure the program is correctly built (e.g. it links) in such circumstances. The programmer is also required to ensure a different definition of the function is not introduced elsewhere (i.e. to not violate the one-definition rule).

    2) a hint to the compiler that the body of the function may be placed inline at the call point, rather than using the more usual function call mechanism. In practice it is often the optimiser that accepts, or ignores, that hint but it doesn't have to be.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by cyberfish View Post
    Cool! Thanks!

    I thought "inline" is only a hint for the optimizer.
    If the compiler doesn't inline the function, it is obliged to accept the source code and produce a compileable program as if it had inlined the function. We can't have code suddenly fail to link because the compiler chose not to inline on any particular day. And so it also serves to prevent multiple definitions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  5. error LNK2001: unresolved external symbol
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 07-12-2002, 08:45 PM