Thread: Q about inline functions

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    127

    Q about inline functions

    If I want to use inline functions defined in file A in file B, do I have to define the function in a header?

    I saw a post on another forum asking about putting source in a header. One of the responses said sometimes you're forced to, like when using inline functions. Is this true?

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    If I want to use inline functions defined in file A in file B, do I have to define the function in a header?
    Probably.
    I saw a post on another forum asking about putting source in a header. One of the responses said sometimes you're forced to, like when using inline functions. Is this true?
    Probably.

    The thing with inline functions is that the caller generally has to have the code available, or it won't be able to inline it. This is similar, broadly, to templates in C++, where the template code is in a header so the compiler can have access to it.

    Modern compilers, however, do have the ability to do link-time optimization, which can inline functions across translation units without anything special being done in the code. They do this, typically, by generating an intermediate form of code that is stashed in the object files along with (or replacing) the object code; when everything is linked, the source code (or the intermediate form code) is available all at once. This would be similar to putting everything in one huge source file, allowing a compiler to optimize it all at once.

    At least the following compilers (with included options) support link-time optimization. Note that you'll typically have to use the compiler to link, and pass it the same optimization flags as when compiling--linkers tend not to be smart enough to understand link-time optimization:
    • gcc -flto
    • icc -ipo
    • suncc -xipo=2
    • clang -O4 (may require the gold linker with appropriate plugin)

    You might also have to instruct the compiler specifically to inline, which typically happens at a high optimization level (-O3 with gcc, for example).

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by homer_3 View Post
    If I want to use inline functions defined in file A in file B, do I have to define the function in a header?

    I saw a post on another forum asking about putting source in a header. One of the responses said sometimes you're forced to, like when using inline functions. Is this true?
    You're asking the wrong question. The real question is:
    Should these functions be declared as inline at all anyway? A likely answer is "no", in which case they can stay where they are and a prototype of them can be put in a header file.
    Don't prematurely optimise. The compiler is often smart enough to inline them for you when it's worth it.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inline Functions
    By EvilGuru in forum C Programming
    Replies: 4
    Last Post: 11-07-2005, 04:03 PM
  2. inline functions
    By robid1 in forum C++ Programming
    Replies: 1
    Last Post: 06-13-2004, 05:00 AM
  3. Inline functions
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 08-06-2002, 03:35 AM
  4. What should I really use inline functions?
    By GrNxxDaY in forum C++ Programming
    Replies: 6
    Last Post: 07-27-2002, 04:49 PM
  5. Inline functions
    By tim545666 in forum C++ Programming
    Replies: 2
    Last Post: 03-09-2002, 12:34 AM