Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tips on reducing c++ linking time

Tags:

I have a project that takes about 8 seconds to link with g++ and ld.

It uses a bunch of static libraries, most of the code is c++.

I'm interested in a general list of tips on how to reduce link time. Anything from "dont include debug symbols" to "make your code less spagetti"

like image 869
Lucas Meijer Avatar asked Dec 29 '10 19:12

Lucas Meijer


People also ask

How do I reduce linking time?

create a ramdisk ,compile to that and link to harddisk. since you're using a lot of static libraries ,you can create a giant library containing all thos libraries so you end up with one libray.

Why does linking take so long?

Linkers have to copy large amount of data from object files to an output file, and that is inevitably slow.


4 Answers

I dealt with this for years at a previous job. The GNU linker simply has serious performance problems when linking large numbers of static libraries. At one point, link time was on par with compile time, which we found so strange we actually investigated this and figured it out.

You can try to merge your static libraries into a "super-object" before linking. Instead of linking like this:

$ g++ -o program program.o $STATIC_LIBS 

You could try this:

$ ld -r -o libraries.o --whole-archive $STATIC_LIBS $ g++ -o program program.o libraries.o 

Note that this method gives the linker less opportunity to exclude unused object code, so your binaries may increase in size somewhat.

like image 111
brewbuck Avatar answered Sep 29 '22 01:09

brewbuck


create a ramdisk ,compile to that and link to harddisk.

since you're using a lot of static libraries ,you can create a giant library containing all thos libraries so you end up with one libray. remove all libraries from your lib-list and add th giant one. This reduces openings of file to 1 for the libraries and may speed up reading actions.

like image 27
engf-010 Avatar answered Sep 29 '22 01:09

engf-010


Turn off whole program optimization (at least during development). Use p-impl to reduce dependencies.

like image 44
Ben Voigt Avatar answered Sep 29 '22 03:09

Ben Voigt


8 seconds is pretty fast, unless you're really sure that it shouldn't take that long. I've got projects that take 5-8 minutes for a full relink since we don't do incremental linking on our release builds. Have you tried using incremental linking (if you're not using -shared, you can use -i or -r)?

like image 25
Wyatt Anderson Avatar answered Sep 29 '22 03:09

Wyatt Anderson