So I have written some code that uses C++17 filesystem, and am dismayed at the size of the executable. I use the compile option -Os (Min Size Release)and link with -lstdc++fs and the executable size is 2.3MB. Is there some compile or linker or cmake option I need to set to improve this? KDevelop IDE has this as the verbose cmake output:
/home/TheIdeasMan/projects/kdev/c17fs/build> make -j8
/usr/bin/cmake -H/home/TheIdeasMan/projects/kdev/c17fs -B/home/TheIdeasMan/projects/kdev/c17fs/build --check-build-system CMakeFiles/Makefile.cmake 0
/usr/bin/cmake -E cmake_progress_start /home/TheIdeasMan/projects/kdev/c17fs/build/CMakeFiles /home/TheIdeasMan/projects/kdev/c17fs/build/CMakeFiles/progress.marks
/usr/bin/make -f CMakeFiles/Makefile2 all
make[1]: Entering directory '/home/TheIdeasMan/projects/kdev/c17fs/build'
/usr/bin/make -f CMakeFiles/c17fs.dir/build.make CMakeFiles/c17fs.dir/depend
make[2]: Entering directory '/home/TheIdeasMan/projects/kdev/c17fs/build'
cd /home/TheIdeasMan/projects/kdev/c17fs/build && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/TheIdeasMan/projects/kdev/c17fs /home/TheIdeasMan/projects/kdev/c17fs /home/TheIdeasMan/projects/kdev/c17fs/build /home/TheIdeasMan/projects/kdev/c17fs/build /home/TheIdeasMan/projects/kdev/c17fs/build/CMakeFiles/c17fs.dir/DependInfo.cmake --color=
make[2]: Leaving directory '/home/TheIdeasMan/projects/kdev/c17fs/build'
/usr/bin/make -f CMakeFiles/c17fs.dir/build.make CMakeFiles/c17fs.dir/build
make[2]: Entering directory '/home/TheIdeasMan/projects/kdev/c17fs/build'
[ 50%] Building CXX object CMakeFiles/c17fs.dir/main.cpp.o
/usr/local/bin/g++-7.0 -std=c++1z -Wall -Wextra -pedantic-errors -Os -DNDEBUG -o CMakeFiles/c17fs.dir/main.cpp.o -c /home/TheIdeasMan/projects/kdev/c17fs/main.cpp
[100%] Linking CXX executable c17fs
/usr/bin/cmake -E cmake_link_script CMakeFiles/c17fs.dir/link.txt --verbose=1
/usr/local/bin/g++-7.0 -std=c++1z -Wall -Wextra -pedantic-errors -Os -DNDEBUG CMakeFiles/c17fs.dir/main.cpp.o -o c17fs -rdynamic -lstdc++fs
make[2]: Leaving directory '/home/TheIdeasMan/projects/kdev/c17fs/build'
[100%] Built target c17fs
make[1]: Leaving directory '/home/TheIdeasMan/projects/kdev/c17fs/build'
/usr/bin/cmake -E cmake_progress_start /home/TheIdeasMan/projects/kdev/c17fs/build/CMakeFiles 0
*** Finished ***
I use the compile option -Os (Min Size Release)and link with -lstdc++fs and the executable size is 2.3MB
.
There is the -DNDEBUG option, which I am guessing is a cmake thing, does it mean "No Debug" ? I am trying to look for documentation for that now. It is a default setting in KDevelop for the release builds.
% g++ -std=c++17 -Wall -Wextra -pedantic-errors -Os -fdata-sections -ffunction-sections -fwhole-program -flto fstest.cxx -lstdc++fs -Wl,--gc-sections
% ls -lh a.out
-rwxr-xr-x 1 mbozzi mbozzi 2.1M Apr 26 01:28 a.out
% g++ --version
g++ (GCC) 7.0.1 20170305 (experimental)
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
% uname -srm
Linux 4.10.8-1-ARCH x86_64
On my system at least, libstdc++fs.a is built with debug symbols. Stripping those symbols from an executable built using the above command line leaves a 161K executable.
Edit: You can strip the debug symbols using objcopy -g a.out: % objcopy -g a.out
% ls -lh a.out
-rwxr-xr-x 1 mbozzi mbozzi 161K Apr 26 01:42 a.out
Glad that helped. Maybe once the filesystem library gets integrated into libstdc++ they'll strip the debugging symbols by default.
My system has a shared library libstdc++fs.la, is there any way to link that instead
Not really, because the la file is an artifact of the build process and is ideally transparent to you as a library user. It's not a shared library itself, but a bunch of metadata that helps keep track of different library versions and dependencies of that library. It comes from libtool, which is supposed to help write portable code. If you're curious, open it up -- it's human readable.