Thread: how to make sure g++ will not inline this?

  1. #31
    Banned
    Join Date
    Nov 2007
    Posts
    678
    I don't worry too much! Just a little curious though.
    I will use -Os. Done! Decided!

  2. #32
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    Quote Originally Posted by matsp View Post
    However, if you put functions in a header file, they should be marked static, or part of a class.

    If the inline keyword gets ignored and definitions for non-static functions inside header files will cause linker problems, a non-static function can never be inlined. Is this correct?

  3. #33
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Global functions CAN be inlined, but you always need to also have a global copy too (because some source that doesn't include the source may be calling the function). If the function is marked static, the compiler can avoid generating the code for the function if it's not called within the current unit.

    By the way, the keyword "inline" does not allow the function to be defined twice. It needs to be "static inline" to allow it to be declared twice.

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

  4. #34
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    inline forces the compiler to allow multiple definitions across compilation units, whether the function is static or not. (And in this sense, the compiler isn't allowed to ignore inline.) However, if it's not static, all definitions must be exactly the same (no diagnostic required), or undefined behaviour will result. Effectively, this means you can have one definition in a header file, included by whoever wants the functionality.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #35
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by CornedBee View Post
    inline forces the compiler to allow multiple definitions across compilation units, whether the function is static or not. (And in this sense, the compiler isn't allowed to ignore inline.) However, if it's not static, all definitions must be exactly the same (no diagnostic required), or undefined behaviour will result. Effectively, this means you can have one definition in a header file, included by whoever wants the functionality.
    Actually, I tried "inline func()" in a header file, and then included that header file twice, and gcc complained about it.

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

  6. #36
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    Thanks, that is good to know!
    It would be good if matsp issue could be solved though. Could you post your full example please?

  7. #37
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    I don't understand the original problem. The question is, how to prevent the compiler from inlining a function. The answer is, define the function in a .cpp file, not a .h file. Simple as that.

    If the function is in a .cpp file, then other modules don't have access to its source implementation, and the ONLY choice is to call the function.

    Writing the code in the .h file, either as an in-class-definition inline or outside the class using the "inline" keyword, still doesn't guarantee that the compiler will inline it.

    In sum, it is easy to PREVENT inlining, although there is no (portable) way to FORCE it. I don't see what the source of the original question was.

  8. #38
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    The question is, how to prevent the compiler from inlining a function. The answer is, define the function in a .cpp file, not a .h file. Simple as that.
    And indeed, that solution was suggested in post #5, as I noted earlier.
    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

  9. #39
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by matsp View Post
    Actually, I tried "inline func()" in a header file, and then included that header file twice, and gcc complained about it.
    "allow multiple definitions across compilation units"

    Not very clear, that. No, you can't include the header twice, but there's absolutely no reason to do that anyway. That's what header guards are for.

    The issue I'm talking about is when you have two source files that include the header containing the definition.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #40
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by brewbuck View Post
    I don't understand the original problem. The question is, how to prevent the compiler from inlining a function. The answer is, define the function in a .cpp file, not a .h file. Simple as that.

    If the function is in a .cpp file, then other modules don't have access to its source implementation, and the ONLY choice is to call the function.
    I'm pretty sure that the "Link-time code generation" setting in newer versions of VS specifically enables things like inlining functions from other compilation units, assuming you have it turned on.

    Anyway, I see what the misunderstanding manav had was. Inlining is not directly related to code size. Inlining can make the executable bigger, and in other cases it can also make it smaller. I think he is aware of this now.
    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"

  11. #41
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by iMalc View Post
    I'm pretty sure that the "Link-time code generation" setting in newer versions of VS specifically enables things like inlining functions from other compilation units, assuming you have it turned on.

    Anyway, I see what the misunderstanding manav had was. Inlining is not directly related to code size. Inlining can make the executable bigger, and in other cases it can also make it smaller. I think he is aware of this now.
    Yes, I think both gcc and Visual Studio allows "whole program optimization" [that's the gcc term], which means that the compiler will take "all" source code into account when performing optimization. Although you have to request that [at least in the versioins of gcc that I've been using]

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

  12. #42
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Whole Program Optimization is MS's term. GCC doesn't support it yet, except via the -combine switch, which, frankly, is a hack and doesn't play well with the structure of traditional makefiles.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inline Definitions and Declarations?
    By legit in forum C++ Programming
    Replies: 1
    Last Post: 06-15-2009, 01:59 PM
  2. Win32 Common Controls in C++, how do i make and use them?
    By C+noob in forum Windows Programming
    Replies: 6
    Last Post: 01-09-2006, 11:53 AM
  3. Inline functions and inheritance
    By hpy_gilmore8 in forum C++ Programming
    Replies: 3
    Last Post: 01-14-2004, 06:46 PM
  4. make all rule
    By duffy in forum C Programming
    Replies: 9
    Last Post: 09-11-2003, 01:05 PM
  5. Question about atheists
    By gcn_zelda in forum A Brief History of Cprogramming.com
    Replies: 160
    Last Post: 08-11-2003, 11:50 AM