inline function in header, multiple definition

This is a discussion on inline function in header, multiple definition within the C++ Programming forums, part of the General Programming Boards category; Say I have a header file, a.h Code: #ifndef A_H #define A_H int some_util_function() { ... } #endif and b.cpp, ...

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

    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
    21,662
    You also need to declare the function as inline using the inline keyword.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,183
    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,707
    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 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,266
    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%.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,302
    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, 06: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, 01:53 AM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  5. error LNK2001: unresolved external symbol
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 07-12-2002, 08:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21