I don't worry too much! Just a little curious though.
I will use -Os. Done! Decided!
I don't worry too much! Just a little curious though.
I will use -Os. Done! Decided!
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.
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
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.
Thanks, that is good to know!
It would be good if matsp issue could be solved though. Could you post your full example please?
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.
And indeed, that solution was suggested in post #5, as I noted earlier.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.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
"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
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"
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.
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