johndoe
johndoe

Reputation: 15

Undefined reference when linking static libraries

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?

Upvotes: 0

Views: 3257

Answers (2)

William Culver
William Culver

Reputation: 46

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

Upvotes: 1

Amarnath Revanna
Amarnath Revanna

Reputation: 531

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.

Upvotes: 0

Related Questions