Skip to content

Instantly share code, notes, and snippets.

@engelmarkus
Last active December 21, 2016 18:58
Show Gist options
  • Save engelmarkus/a124afbf75e4a489323e6e913f6cf77b to your computer and use it in GitHub Desktop.
Save engelmarkus/a124afbf75e4a489323e6e913f6cf77b to your computer and use it in GitHub Desktop.
Script for determining the correct order of static libraries for GNU ld
#!/usr/bin/env ruby
# When passing static libraries to GNU ld, one has to make sure the order is correct or undefined references will occur.
# This is due to the linker reading each static library just once.
# With "--start-group" and "--end-group" this behaviour can be overridden but using these options is discouraged.
# call the linker verbosely and print the correct library order to the shell
# ~> gcc prog.c -static -Wl,--verbose -Wl,-\( -llib1 -llib2 -lrest_of_the_libs -Wl,-\) | ./order_deps.rb
libs = Array.new
ARGF.each_line do |line|
if line =~ /^\(.*\/lib(.*)\.a\).*o$/
libs << $~[1]
end
end
# circular dependencies will still cause the linking process to fail.
# in this case remove these three lines.
libs.reverse!()
libs.uniq!()
libs.reverse!()
libs.each do |lib|
puts lib
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment