6

In GCC, I can use the linker flags -Wl,--start-group and -Wl,--end-group to resolve linking problems with libraries that have circular dependencies. I'd like to do the same with clang, but it seems like this feature was dropped in lld version 3.2. How do I do it?

usr1234567
  • 21,601
  • 16
  • 108
  • 128
Mokosha
  • 2,737
  • 16
  • 33
  • 6
    https://en.wikipedia.org/wiki/Technical_debt – Hans Passant Oct 10 '14 at 22:19
  • list all the libs (in any order) twice: `-la -lb ...-lx -la -lb ... -lx` (best done with a script) – mlvljr Oct 14 '14 at 13:30
  • 1
    @mlvljr This may or may not be enough. – user1225999 Oct 14 '14 at 13:35
  • @user1225999 should be -- this guarantees that for every symbol referenced in the object code, the ~definition *will* appear at least once after the reference / declaration – mlvljr Oct 15 '14 at 20:29
  • 1
    @mlvljr GNU ld will only include objects from an archive that it actually needs at that point. Knowing this one can easily construct a counterexample: http://pastebin.com/YSrNqT4h – user1225999 Oct 16 '14 at 08:09
  • @user1225999 Well, this was my point exactly (on including only the already referenced objects), so your example fails? – mlvljr Oct 16 '14 at 14:28
  • The example fails to link even though the libraries liba.a and libb.a are linked twice as you suggested. For this specific example you would need to add another `-la` in order to make it link. – user1225999 Oct 16 '14 at 14:38
  • According to this [change](https://reviews.llvm.org/D43786) the switches should now be ignored and not needed since circular dependencies should always be resolved automatically. Maybe they are adopting same behavior as MSVC (which searches for symbols all libraries at any time)? Can someone confirm? – ceztko Aug 03 '18 at 19:35

3 Answers3

4

The release notes of LLVM 3.2 state that

llvm-ld and llvm-stub have been removed, llvm-ld functionality can be partially replaced by llvm-link | opt | {llc | as, llc -filetype=obj} | ld, or fully replaced by Clang.

By default clang seems to use the system linker. That is on Linux for example it uses the GNU ld:

$ clang --version
clang version 3.2 (branches/release_32 170558)
...
$ clang -Wl,--verbose
GNU ld (GNU Binutils; devel:gcc / openSUSE_12.3) 2.24.0.20140403-196
...

This suggests that you can use -Wl,--start-group and -Wl,--end-group as with GCC.

user1225999
  • 912
  • 8
  • 14
  • This is the case. Apple's ld tool doesn't support the --start-group flags, but it doesn't care about circular dependencies either. – Mokosha Oct 16 '14 at 02:15
3

I'm not a fan of circular dependencies :) but here some say they handle this cases by linking some libraries twice. I haven't tried it but this might increase your code's footprint.

$(CC) -o myApp -lfoo -lbar -lfoo

I don't know if this works with clang but it might worth a shot.

Best solution would be removing the circular dependencies as it will generate you more problems in the future.

Community
  • 1
  • 1
VAndrei
  • 5,420
  • 18
  • 43
0

I had a similar problem linking with a custom clang compiler using a QT c++ project.

The problem turned out to be that QT was using clang (c compiler) rather than clang++ (c++ compiler). By defining the compiler as clang++ instead of clang in the QT project (setting QMAKE_CXX=clang++) the project linked successfully.

As I understand it, the same holds true for gcc and g++.