According to gcc documentation for link time optimization:
It is recommended that you compile all the files participating in the same link with the same options and also specify those options at link time
I specify in CML.txt
the following:
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
set(CMAKE_VERBOSE_MAKEFILE on)
to see the actual emitted compile and linking command. My compile commands are like so [snipped for brevity]:
[33%] Building CXX object CMakeFiles/CMakeProject.dir/code/main.cpp.o
/usr/bin/c++ -O3 -flto -fno-fat-lto-objects -fPIE -m64 -fno-common -fPIC -fno-strict-aliasing -fexceptions -fopenmp -DNDEBUG -o CMakeFiles/CMakeProject.dir/code/main.cpp.o -c code/main.cpp
[ 66%] Building CXX object CMakeFiles/CMakeProject.dir/code/impl.cpp.o
/usr/bin/c++ -O3 -flto -fno-fat-lto-objects -fPIE -m64 -fno-common -fPIC -fno-strict-aliasing -fexceptions -fopenmp -DNDEBUG -o CMakeFiles/CMakeProject.dir/code/impl.cpp.o -c code/impl.cpp
The emitted linking command is like so:
[100%] Linking CXX executable CMakeProject
/snap/cmake/1336/bin/cmake -E cmake_link_script CMakeFiles/CMakeProject.dir/link.txt --verbose=1
/usr/bin/c++ -O3 -flto -fno-fat-lto-objects code/main.cpp.o code/impl.cpp.o -o CMakeProject -Wl,-rpath,/usr/local/lib -lm -lpthread -ldl
In particular, unlike as suggested over in the GCC site, I do not see flags such as -fPIC
, etc., which feature in the compile stage appear in the linking stage. Although, I do see that both the linker and the compiler make use of -flto
.
Is this because some of the flags that appear in the compile stage are not relevant to the linking stage? Is there CMake documentation of which flags are applicable to both the linker and the compiler and which are not? Clearly, CMake should be using this information to generate the right linking command for inter procedural/link time optimization.