开发者

Disabling optimizations in an executable and maintaining them in a static library

开发者 https://www.devze.com 2023-04-08 14:23 出处:网络
I\'ve come across an odd situation and my (supposed) knowledge of code linkage is failing me... I have come across a bug that only occurs in a 64-bit build with optimizations turned on (/O2, /O3, or

I've come across an odd situation and my (supposed) knowledge of code linkage is failing me...

I have come across a bug that only occurs in a 64-bit build with optimizations turned on (/O2, /O3, or /Ox). The bug occurs in an executable that is not performance critical and we have to give a demo of a prototype very soon (i.e., Monday). Due to the extreme pressure to get this working for the demo, I had an idea; build the static library (wh开发者_开发技巧ich is performance critical) with optimizations on and turn them off in the executable. This should hide the bug until I fix it while not slowing down the system, or so I thought.

I have now tried this with Link Time Code Generation off, as well as on, and no whole program optimization, but each time, with the VS2005 C++ compiler as well as when using Intel's compiler, the performance critical library is not being linked in with optimizations enabled and things slow down dramatically.

Does anyone know how to accomplish what I am after here? This is an odd situation and I have never had to deal with it, but I did some reading and I couldn't find any documentation saying that what I am trying to accomplish isn't feasible, but apparently it is not or I am missing something. Thanks in advance for any help you guys can offer, I know this is an odd request for a dirty short term "fix", but it is rather important.


Your problem is almost assuredly that critical inlining of functions in your library or the standard library is being disabled in the final application by turning off optimizations.


I would guess that your non-optimized EXE is not actually being linked with the optimized static lib.

If you know what function the optimization is borking, something you can do is use pragmas around that function to turn off optimizations just for it.

#pragma optimize( "", off )
int f(int x )
{
    return x - 1;
}
#pragma optimize( "", on ) 


The statement "is not being linked in with optimizations enabled" is a bit confusing, as this should be impossible; the linker will link whatever the compiler generated. If you set the compiler flags properly when compiling the modules in the static library, then they should all be optimized.

The problem might be that your static library is calling functions from the C++ library that are not optimized. There is nothing you can do about this, since the application and the library must both use the same C++ library or you will have much worse problems than you started with.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号