CMake, static library and link time optimization <p>I'm trying to create static library with link time optimization using cmake and g++.</p> <pre><code>set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -Wextra -pedantic -std=c++11" ) if (CMAKE_COMPILER_IS_GNUCXX) set( CMAKE_STATIC_LINKER_FLAGS_RELEASE "${CMAKE_STATIC_LINKER_FLAGS_RELEASE} -flto -fwhole-program" ) endif() add_library( mylib STATIC mylib.cpp ) </code></pre> <p>But when running typical</p> <pre><code>cmake -DCMAKE_BUILD_TYPE=Release .. make </code></pre> <p>I'm getting following error:</p> <pre><code>/usr/bin/ar: two different operation options specified </code></pre> <p>link.txt file contains following commands:</p> <pre><code>/usr/bin/ar cq libmylib.a -flto -fwhole-program CMakeFiles/mylib.cpp.o /usr/bin/ranlib libmylib.a </code></pre> <p>From what I understand from running <code>ar --help</code> the <code>-flto -fwhole-program</code> should be before <code>libmylib.a</code> in the first line. But I have no idea how to force CMake to put it there.</p> <p>Is my assumption correct? And how can I resolve this?</p> <p>EDIT: I'd like to add that I'm completely new to using LTO so if it doesn't make sense to use it for static libraries, please do tell me so.</p>