0

Let's say a function A in libA.a calls a function B in libB.a, and a separate function C in libB.a relies on function D in libA.a. If you try to link them with gcc main.c libA.a libB.a, you will get undefined reference to D. If you try gcc main.c libB.a libA.a, you will get undefined reference to B.

DrownedSuccess
  • 123
  • 1
  • 8
  • Hmmm.. that should work without problem. This issue should happen in the opposite case. If you compile with `libA.a` before `libB.a` then libA can call any of libB's functions. Are you just calling exactly `gcc libA.a libB.a`? That of course won't work, you are missing a main. But `gcc main.c libA.a libB.a` should work. Also: define "it won't work" because currently it is unclear what you mean. Add error messages and a [mre]. – Marco Bonelli Aug 24 '23 at 01:03
  • See my answer: https://stackoverflow.com/a/34168951/5382650 – Craig Estey Aug 24 '23 at 01:22
  • @CraigEstey Isn't that only for ld? I'm using gcc (`gcc -c` then `ld` doesn't work for me). – DrownedSuccess Aug 24 '23 at 01:25
  • No, it works just fine for `gcc` because it invokes `ld` under the hood. You just have to add `-Wl,*` to escape the linker options. See `man gcc` for documentation on `-Wl` – Craig Estey Aug 24 '23 at 01:27
  • @CraigEstey Weird... it says that `--start-group` and `--end-group` is an invalid option. – DrownedSuccess Aug 24 '23 at 01:28
  • @CraigEstey `-Wl,--start-group` and `-Wl,--end-group` worked perfectly! Can you put this in an answer? – DrownedSuccess Aug 24 '23 at 01:39
  • 1
    Since `libA` and `libB` cannot be used without each other, then perhaps they shouldn't be two separate libraries. – n. m. could be an AI Aug 24 '23 at 03:52

0 Answers0