Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looking for a Static Link Order tool on Linux [closed]

Tags:

c++

g++

linker

Are there any decent tools to determine the optimal static link order with g++ under Linux? I'm familiar with the general issues, including (if necessary) the use of repeated references to a single library or --start-group and --end-group to resolve circular dependencies, but what I'd like, if possible, is a tool that will take a bunch of .a files and spit out a good static link order for them, repeating libraries if necessary, while keeping the repetition to a minimum.

Background: I'm working on a project with around 800K lines of inherited c++ code, and trying to refactor it into smaller, more manageable chunks. Some of the existing files are huge monoliths - today I've been wrestling with a single 34K line .h file that defined 113 classes and structs. Many of the classes were defined almost entirely inline in the .h file. As I split this up into smaller chunks, and migrate some of the implementation code into .cpp files, the required link order on Linux keeps changing. That's probably because every library that included the .h file used to compile its own implementation of whatever classes it needed, and now they are having to link to a common implementation in a single library file. Long-term we'll probably reorganize some of the classes into different libraries, and break some of the dependency chains, but right now the dependencies are pretty tangled, and I'm trying to minimize the perturbations to the code that could change behavior. I'd prefer not to have to keep manually figuring out the right link order every time it changes. Suggestions?

like image 443
dewtell Avatar asked Jun 22 '11 02:06

dewtell


2 Answers

I'm not aware of one offhand, but you could implement one yourself. Just use nm to get a list of symbols from each static library, use this to build a dependency graph, then perform a topological sort over the graph for the proper link order.

Alternately, use partial links (ld -r) instead of static libraries. Since this outputs a merged .o file, your final link can declare the libraries in any order, and they'll all be linked properly. The downside is that the linker won't be able to discard unused source files, since they're already linked into monolithic files before usage data is available (you can workaround this by passing -ffunction-sections -fdata-sections during compilation and -Wl,--gc-sections on final link, although this may impact how long it takes to compile)

like image 200
bdonlan Avatar answered Sep 30 '22 17:09

bdonlan


You could use shared libraries instead of static and compile with -fPIC option.

  • Linux static linking is dead?
  • http://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html
like image 30
Andrew Selivanov Avatar answered Sep 30 '22 18:09

Andrew Selivanov