I want to force a little function not to be compiled as inline function even if it's very simple. I think this is useful for debug purpose. Is there any keyword to do this?
-
14The debugger can handle inlined functions just fine. No need to prevent inlining because of that. – jalf Jul 25 '10 at 12:48
-
13Not really. Great way to hang the debugger for many minutes while it sets thousands of breakpoints. – Hans Passant Jul 25 '10 at 16:41
-
10When stepping through optimized code (which you basically have to do at the assembly language level) it's sometimes nice to see calls to functions sometimes so you can follow where you are, and step over them in one go -- since the compiler's view of "simple" may not be yours. (The debugger may cope fine with all of this, but it's the computer operator that has to actually do the work...) – Jul 25 '10 at 18:18
-
4In addition to debugging, I also wanted to do this for profiling purposes. A function was being inlined without me indicating that it should be. However, that was the specific function that I had wanted to see in the profile. – Justin Peel May 02 '11 at 17:20
-
1You should not be debugging optimised code. Debugging implies you are trying to fix you code, only once it no longer needs fixing should you the optimise. Even if you declare a function as 'inline' (which is in fact only a suggestion) VS adds data to the debug build that let's it step through the executable how the lines of code execute. – thecoshman Oct 17 '12 at 09:34
-
7@thecoshman There are times where you have to debug release code, or you have dumps of release code – Ghita Jan 05 '13 at 18:47
-
@Ghita indeed, there are times when code is so badly written and tested that when it breaks, you can logically work out where in the code the fault lies or have to simply rely on memory dumps. I pity the man who has to maintain such code, and scorn the fecker who wrote it in the first place. – thecoshman Jan 07 '13 at 08:33
-
NASA: fly what you test, test what you fly – Vincent Alex Nov 07 '22 at 18:47
9 Answers
In Visual Studio 2010, __declspec(noinline)
tells the compiler to never inline a particular member function, for instance:
class X {
__declspec(noinline) int member_func() {
return 0;
}
};
edit: Additionally, when compiling with /clr
, functions with security attributes never get inlined (again, this is specific to VS 2010).
I don't think it will prove at all useful at debugging, though.

- 39,737
- 6
- 87
- 123
-
The documentation says it's for member functions only: https://msdn.microsoft.com/en-us/library/kxybs02x.aspx . Is there a way to make a free-standing function non-inline? – Serge Rogatch Aug 22 '16 at 06:37
-
Something to note is that inline functions exist in the same translation unit where they are called from, meaning that, for example, any global static variable included into that translation unit is used in place of the same global static variable also included into the file where the inline function is declared/defined(non-inline functions use the copy of the global static variable included into their definition file ). So it could possibly make a difference between compilations whether the function is inlined or not. – Matias Chara Jul 01 '20 at 14:24
__declspec(noinline)
for VC++. Contrary to the man page, this appears to work for freestanding functions, and I don't think I've ever used it for a member function. You may -- though note that I never have -- want to consider playing with the optimization flags too, so that only inline
functions are considered for inlining, though of course this has a global effect and that may not be what you want.
__attribute__((noinline))
for gcc (and a number of less-common compilers that support the gcc attribute syntax). I must admit, I don't think I've ever actually used this, but it appears to be there.
(Of course, these two styles of annotation go in different places, so it's a bit annoying to construct code that's palatable to both.)
I'm not sure how either of these interact with the inline
C++ keyword; I've only used them when debugging (when I just want a particular non-inline function left not inline after optimization) or when examining generated code (and I'm getting confused because random stuff is being inlined).

- 13,051
- 4
- 61
- 89
-
2it works for member functions too. One interesting note to take is that it seems to make non inline calls to containing functions also (even though they would be inline otherwise) - VS 2012 – Ghita Jan 05 '13 at 18:49
-
1how check via cmake what compiler used:vc++ or gcc and make define `noinline`? – ilw Nov 17 '17 at 13:51
Please remember that inlining is relevant at the function call site, the same function can be inlined in some situations and not inlined in other.
If your function is visible outside the compilation unit then even if it's inlined in all the current places it's used, the body of the function must still be available for anyone who wants to call it later on (by linking with the object file).
In order to have a call site not inlined you can use a pointer to a function.
void (*f_ptr)(int); // pointer to function
volatile bool useMe = true; // disallow optimizations
if (useMe)
f_ptr = myFunc;
else
f_ptr = useOtherFunc;
f_ptr(42); // this will not be inlined

- 110,860
- 49
- 189
- 262
[[gnu::noinline]]
attribute
We can also use the C++11 attribute specifier syntax with the non-standard gnu::noinline
attribute: https://en.cppreference.com/w/cpp/language/attributes
It is just a matter of time until that gnu::
part gets dropped a future C++ standard to give a standardized [[noinline]]
:-)
main.cpp
[[gnu::noinline]]
int my_func() {
return 1;
}
int main() {
return my_func();
}
Compile and disassemble:
g++ -ggdb3 -O3 -o main.out -std=c++11 -Wall -Wextra -pedantic-errors main.cpp
gdb -batch -ex 'disassemble/r main' main.out
With [[gnu::noinline]]
:
0x0000000000001040 <+0>: f3 0f 1e fa endbr64
0x0000000000001044 <+4>: e9 f7 00 00 00 jmpq 0x1140 <my_func()>
Without [[gnu::noinline]]
:
0x0000000000001040 <+0>: f3 0f 1e fa endbr64
0x0000000000001044 <+4>: b8 01 00 00 00 mov $0x1,%eax
0x0000000000001049 <+9>: c3 retq
Tested on Ubuntu 19.10.

- 347,512
- 102
- 1,199
- 985
Simple: Don't let the compiler see the definition of the function. Then it cannot possibly be inlined. Of course, that only works if its your code.
When it comes to debugging 3rd party code... yes, this would be useful, especially if you could zap 3rd party code from afar. Anyone who has debugged code that contains lot of shared_ptr dereferencing knows what I'm talking about.

- 57
- 1
If it is a member function of a class, make it virtual.

- 95
- 7
-
13Won't necessarily work; if the method can be devirtualized, it can still be inlined. – Alice Mar 13 '14 at 09:18
Many compilers can perform cross-translation-unit inlining. Visual Studio has had it for five years and I believe that GCC can now do it- especially since the OP tagged as Visual C++, it's a fair bet that his compiler can cope.
The simplest way to do this is to take the function's address, and then do something non-meaningless with it, like call it or pass it to an OS/external library function. The compiler can't inline that kind of function.
Why you would ever want to, IDK.
@comments:
If the OP srsly, srsly needs this, then he could compile it as a lib and statically link to it.

- 39,737
- 6
- 87
- 123

- 144,682
- 38
- 256
- 465
-
9While taking the address of a function requires the compiler to provide a non-inlined version of it, I don't think it requires it to not to inline it in other places. – sbi Jul 25 '10 at 13:18
-
3That doesn't stop it from inlining a function. As sbi correctly pointed out, it'll just produce a non-inlined version. – wheaties Jul 25 '10 at 13:30
-
I'm fairly sure that the VC++ documentation says that it won't inline any function where it's address is taken. However, you certainly can't move it between translation units, and if you can't take it's address, then the only way to be certain is to compile it in an external lib. – Puppy Jul 25 '10 at 13:41
-
1Sorry to nitpick, but "the previous two answers" is kind of unhelpful. Hard to say which answers you're referring to. – jalf Jul 25 '10 at 16:06
-
jalf: You're almost certainly right. Didn't have much time when writing said answer. – Puppy Jul 25 '10 at 17:22
You can divide the class implementation between a header and cpp file. if you put the function outside of the class definition, your little function wont be inline.

- 3,082
- 7
- 29
- 38
-
6On some, but not all compilers, this will work. OTOH I have a beef with whoever downvoted this and didn't even leave a comment explaining why. – Ben Voigt Jul 25 '10 at 18:20
Is it possible to force a function not to be inlined?
I won't even attempt to answer that question, because it's irrelevant to be concerned with this except for the two reasons outlined below.
Inlining basically is
- an optimization that's mostly transparent to you
- a way to allow functions to be defined in headers without getting multpile definition errors
(Some would switch the order of these two, but I stick to the traditional order.)
Unless either A) you absolutely need to define a function in some header or B) you are profiling and optimizing a piece of code and know better than the compiler what should be inlined and what shouldn't, inlining should be of no concern to you.
It certainly shouldn't be a concern because of debugging. Your debugger should (and in the case of VC also does) take care of that for you.
-
23@sbi I am one of the downvoters. A real function has a symbol in the object code, it offers several benefits over an peace of inlined code. You can track them from a debugger or dynamic code analyser. With a profiler you can know the exact amount of time spent in the function, it's hard when the peace of code is merged in another bigger peace of code. You can dynamically rename the function, or even substitute this function by another one at runtime. – Ben Apr 28 '11 at 14:12
-
@Ben: Debugging usually takes place in code built for debugging, and that shouldn't have functions inlined anyway. For release builds, inlining is just one of many wildly disturbing distortions the optimizer does to your code. And not inlining a function for profiling seems _absurd_, to put it mildly. (That's the most common dynamic code analyzers covered, what else is there?) Which leaves renaming a function at runtime, which seems an odd concept, unless you refer to late binding to dynamically loaded libs (.so, .dll). That's the only case I can see for _not_ wanting to inline a function. – sbi Apr 28 '11 at 14:39
-
8@sbi : It's not absurd, you can't evaluate the cost of an inlined function. By dynamic code analyser I mean tools like valgrind purify or insure++. Yes I was thinking of late binding or re-binding, I use it sometime tracing purpose. Anyway I don't know what Thomson wanted but it may not be irrelevant to him that's why I downvoted. – Ben Apr 28 '11 at 16:13
-
2@Ben: I maintain that it is absurd. If a function is inlined, the optimizer has a lot more playground to rollick around, which would change the timings. So if you don't inline, you measure the timing of a piece of code that might be completely changed when inlining is enabled. What good would that be for? – sbi May 08 '11 at 06:36
-
6@sbi: That's the point: inlining will change timings. You compare the inlined and non-inlined versions. This is a step that will not be worthwhile for most projects, of course, but it is one way to micro-optimize code. – Fred Nurk May 08 '11 at 06:48
-
1@sbi: I discovered another case where not inlining is a good idea: MSVC2010 Fatal Error C1002. A template class with a member function that calls itself with different template parameters (which all must be inline (in code, not necessarily ASM) due to being a template class) can cause the MSVC10 compiler to crash, unless you prevent inlining. – Mooing Duck Nov 07 '11 at 20:06
-
21I down-voted this because it negates the OP's question instead of answering it. – nolandda Oct 09 '12 at 21:07
-
2@nolandda: You're very welcome. And make sure you also search for that question asking how to make all data global for convenient access, and downvote everybody who says it's a bad idea to do so, because they are negating the question, rather than helping the one who asked. – sbi Oct 10 '12 at 08:42
-
3@nolandda, it's madness. I guess it's not the place for yourself; were are here not only to give quick fixes, but also to teach how to **avoid the need for those fixes** by doing things in **right** way, as opposed to "wrong, but works with fixes", which is broken (as is your way of thinking here). – Griwes Oct 17 '12 at 09:23
-
1Thank you for taking the time to explain to the OP how inlining a function works and why they should not be concerned about it whilst debugging their code. – thecoshman Oct 17 '12 at 09:31
-
10I came here from a Google search "visual c++ prevent inlining", so, yeah, it **is** useful to have a real answer to the question. In my case I wanted to get variables in predictable order and location on the stack, and couldn't think of any other way besides having them `PUSH`ed as parameters to a function. That way my first variable is always at [EBP+8], my second at [EBP+C] and so forth. – Vladislav Zorov Jan 18 '13 at 11:45
-
13I also downvoted you. It's one thing to answer the question as posed AND offer advice as to why alternative solutions might be preferable, but it's another entirely to say "I won't even attempt to answer that question" and proceed to provide advice based on possibly incorrect assumptions about what the OP is doing. And anyway, there are legitimate reasons to prevent inlining. In addition to those already stated, if you're doing runtime mocking of dependencies for testing, you cannot mock inlined functions, and you must ensure your mocks themselves are not inlined or they will not work. – Mitch Lindgren Jun 24 '14 at 05:40
-
2if the compiler intrinsic _ReturnAddress() is used, with vs without inline makes a big difference. – JavaMan Dec 07 '15 at 08:22