Jawap
Jawap

Reputation: 2543

Very long linking time with -g and without -DNDEBUG options

I'm building a third-party CMake-based C++ project.

Building the project in release mode only takes a couple of seconds. The linking stage takes less than a second (when make prints Linking CXX executable myproject.exe).

Building in debug mode takes more than a minute. Most of this time is spent linking the executable. This happens even during subsequent builds after minimal changes to the code.

The difference to the release mode seems to be the -g option vs. the -O3 -DNDEBUG options. Is this behavior expected? I've worked on similar sized projects before (~18k LOC), but I've never experienced such a big difference in compile time between debug and release modes.

This takes 50 seconds:

C:\msys64\mingw64\bin\g++.exe -std=c++0x -Wall -Wextra -Wpointer-arith -Wcast-align -fstrict-aliasing -Wno-unused-local-typedefs -fvisibility- inlines-hidden -march=native -g -Wl,--whole-archive CMakeFiles\myproject.dir/objects.a -Wl,--no-whole-archive -o myproject.exe -Wl,--major-image-version,0,--minor-image-version,0 @CMakeFiles\myproject.dir\linklibs.rsp

Upvotes: 10

Views: 4459

Answers (1)

yugr
yugr

Reputation: 21999

Unfortunately, link times for large projects can be quite slow, especially in debug mode. Things that usually help are

  • splitting a project to shared libraries (that's done, e.g., by LLVM developers)
  • linking on SSD (or tmpfs)
  • using the Gold linker instead of the default BFD linker (or maybe even LLD as it's getting more stable)

Upvotes: 6

Related Questions