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?

7
  • 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. Commented Aug 14, 2012 at 6:57
  • Also, have you tried changing the order of the two libraries? Commented Aug 14, 2012 at 6: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. Commented Aug 14, 2012 at 7:02
  • And the error is about an "undefined reference" that should be in one of your libraries? Or is it about some other function? Commented Aug 14, 2012 at 7: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. Commented Aug 14, 2012 at 7:32

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.