0

I have a couple of libraries that are created using avr-ar. Each contains a few objects.

The objects in library1 need symbols from objects in library2. The problem is that when I try to compile the whole thing I get undefined reference issues.

This is where it's failing, there's nothing fancy going on in $(INCLUDE) $(CFLAGS) $(LIBS)

CFLAGS=-mmcu=atmega328p -DF_CPU=16000000UL -Os -w -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums
LIBS=library1.a library2.a

$(CXX) $(INCLUDE) $^ $(CFLAGS) -o $@ $(LIBS)

I'm running Ubuntu 12.04 and

Using built-in specs.
COLLECT_GCC=avr-g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/avr/4.5.3/lto-wrapper
Target: avr
Configured with: ../src/configure -v --enable-languages=c,c++ --prefix=/usr/lib --infodir=/usr/share/info --mandir=/usr/share/man --bindir=/usr/bin --libexecdir=/usr/lib --libdir=/usr/lib --enable-shared --with-system-zlib --enable-long-long --enable-nls --without-included-gettext --disable-libssp --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=avr
Thread model: single
gcc version 4.5.3 (GCC) 

If I extract the objects from the libraries and put the all in a library, everything goes well.

I would like to keep them separate, is there a way to achieve this?

johndoe
  • 15
  • 1
  • 4
  • It would be helpful if you could edit your question to include the actual errors, as well as the contents of the makefile variables referenced. – Some programmer dude Aug 14 '12 at 06:57
  • Also, have you tried changing the order of the two libraries? – Some programmer dude Aug 14 '12 at 06:57
  • @JoachimPileborg Yes, I have tried changing the order of the libraries but I get more undefined symbol errors. With the order I have I get only one. – johndoe Aug 14 '12 at 07:02
  • And the error is about an "undefined reference" that should be in one of your libraries? Or is it about some other function? – Some programmer dude Aug 14 '12 at 07:23
  • Yes the error is in one of the libraries. As I've said the thing that's "missing" is in one of the other libraries. So what I need is for the linker to look in all the libraries before throwing the undefined reference error. – johndoe Aug 14 '12 at 07:32
  • Please add the complete command-line of the compiler, as well as the complete text of the error you receive. – user1202136 Aug 14 '12 at 07:41
  • It's a "cyclic dependency". I managed to solve it by using `LIBS=library1.a library2.a library1.a` Tough I still wait for a cleaner solution, some compiler flag. – johndoe Aug 14 '12 at 07:58

2 Answers2

1

you could try making the linker do a recursive link by grouping the libraries. I havn't checked the following but maybe change:

LIBS=library1.a library2.a

To

LIBS=-Wl,--start-group library1.a library2.a -Wl,--end-group

This will cause the linker to go back and forth until all symbols are defined, at a linker performance cost. This is useful when two libraries depend on each other because the linker usually only passes each file once.

Hope this helps

0

Since you say library1 is depending on references from library2, your LIBS=library1.a library2.a needs to be changed to LIBS=library2.a library1.a

During compilation, the symbols are parsed from left to right order of your listed libraries, so if you are relying on library2.a in library1.a, you need to supply library2.a first.

Amarnath Revanna
  • 521
  • 1
  • 3
  • 9
  • There's a cyclic dependency between some of the libraries, I managed to solve it by using something like this `LIBS=library1.a library2.a library1.a` – johndoe Aug 14 '12 at 09:29