Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
561 views
in Technique[技术] by (71.8m points)

How do I enable link time optimization (LTO) with CMake?

I have the most recent cmake build and trying all build configurations (Debug, MinSizeRel, RelWithDebugInfo, Release, General) I see nowhere (text search) in generated makefiles the string -lto, so or the functionality is still not present, or it requires manual intervertion (in that case a text search for LTO or Link time optimization) over the documentation gives no result, so I see nowhere in official documentation a way to enable LTO.

Is there any way to enable LTO "globally" (for all compilers) without specifying manually flags that could be at worst supported only by GCC?

question from:https://stackoverflow.com/questions/31355692/how-do-i-enable-link-time-optimization-lto-with-cmake

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Good news! CMake v3.9 finally supports LTO.


Example

Here's an example code to show how it works:

cmake_minimum_required(VERSION 3.9.4)

include(CheckIPOSupported)
check_ipo_supported(RESULT supported OUTPUT error)

add_executable(example Example.cpp)

if( supported )
    message(STATUS "IPO / LTO enabled")
    set_property(TARGET example PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)
else()
    message(STATUS "IPO / LTO not supported: <${error}>")
endif()

For GCC this adds -flto -fno-fat-lto-objects to the targets compile commands.


Checking Compiler Support

The Module CheckIPOSupported provides checking whether interprocedural optimization (IPO/LTO) is supported by the compiler or not:

check_ipo_supported([RESULT <result>] [OUTPUT <output>]
                    [LANGUAGES <lang>...])

If no arguments is passed (= check_ipo_supported()) an error is raised to indicate it's not supported, otherwise the result variable is set to either YES or NO. More details are described in the documentation of the module.


Enabling LTO

LTO is enabled either for a single target or as default for all targets.

LTO for a single target

To enable LTO for a target set INTERPROCEDURAL_OPTIMIZATION to TRUE. This is done by the set_property() command:

set_property(TARGET name-target-here
             PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)

LTO as default

It's possible to enable LTO per default by setting CMAKE_INTERPROCEDURAL_OPTIMIZATION to TRUE:

set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)

This will enable INTERPROCEDURAL_OPTIMIZATION for all targets created after this line. Those created before are not affected.


See also


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...