0

I have a huge bundle of multi-threaded source codes which includes five statically linked libraries (which is also an project of the same solution). I use CMake tool(Version 3.7.1) for generating solution for MSVC(VS2015) and Makefile for Linux (Unix Makefiles). Since it has to run on multiple platform, all the platform specific header files are included separately.

I have worked more on Windows build most of the time with optimization value as

set(CMAKE_CXX_FLAGS_RELEASE "/MT /O2")
set(CMAKE_CXX_FLAGS_DEBUG "/MTd /O0")

and for Linux

set(CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS} -fexceptions -fpermissive -Os")

But the size of the binaries vary in huge size. On Windows it is 1,698KB and on Linux it is 17121988 Bytes, approx 16MB.

As there are two different build options (Release and Debug) do we have similar to Linux. If so how should differentiate those build options.

Also I use add_definition("-W -Wall -Wextra -Wno-unused-parameter -Wno-overloaded-virtual -Wconversion -ggdb")

How the minimize the Linux binary size. I have seen similar post like these but the question is vice versa for me.

usr1234567
  • 21,601
  • 16
  • 108
  • 128
yuvi
  • 25
  • 8
  • `CMAKE_CXX_FLAGS` in its three forms is a bit opaque. You might want to expand on what flags are being hidden behind them. In fact, adding the complete command lines generated would remove all of the ambiguity. – user4581301 Jan 10 '17 at 18:58
  • @user4581301: -fexceptions -fpermissive -Os. other than these three I didnt see anything when building on Linux and on Windows "/DWIN32 /D_WINDOWS /W3 /GR /EHsc /MP" – yuvi Jan 10 '17 at 19:05
  • The target processor (x86, ARM, ...) and the address size (32-bit or 64-bit) also affect the size of binary. – Josh Sanford Jan 10 '17 at 20:49

1 Answers1

1

The -ggdb flag you're using includes debugging symbols in your build. With GCC/Linux, these are embedded directly into the executable, rather than being compiled into a separate database (*.PDB) like they are on Windows. That's likely one reason why your Linux build is significantly larger.

If you remove the -g flag, you should see the binary size reduce significantly. You can further reduce the size by adding the -Os flag, which will optimize the built binary to reduce size. You can even further reduce the size by adding -s, which strips the symbol table and relocation information from the final binary.

Note that you'll probably only want to do the above for your "Release" build - you'll want to keep all of that debugging information for your "Debug" build so you can debug with GDB and other tools.

Community
  • 1
  • 1
Chris Vig
  • 8,552
  • 2
  • 27
  • 35