Bug 2478 - undefined reference vtable if symbol is in archive
Summary: undefined reference vtable if symbol is in archive
Alias: None
Product: binutils
Classification: Unclassified
Component: ld (show other bugs)
Version: 2.16
: P2 normal
Target Milestone: ---
Assignee: unassigned
Depends on:
Reported: 2006-03-23 15:07 UTC by Sebastian Tusk
Modified: 2006-03-24 13:26 UTC (History)
1 user (show)

See Also:
Host: i686-pc-cygwin
Target: i686-pc-cygwin
Build: i686-pc-cygwin
Last reconfirmed:


Note You need to log in before you can comment on or make changes to this bug.
Description Sebastian Tusk 2006-03-23 15:07:16 UTC
Linking fails with
b.o:b.cpp:(.text$_ZN4TestC1Ev[Test::Test()]+0x8): undefined reference to `vtable
for Test'
if object file is stored in an archive but succeeds if not.

Reproducible with this testcase:
### a.h ###
class Test
	virtual void do_something();
### a.cpp ###
#include "a.h"

void Test::do_something()
### b.cpp ###
#include "a.h"
int main( int argc, char** argv )
	Test test;
	return 0;
### build object files "a.o" "b.o" and put "a.o" in archive "a.a"
g++ -c -o a.o a.cpp && g++.exe -c -o b.o b.cpp && ar -q a.a a.o
### linking with object files succeeds
g++ -o c a.o b.o
### linking with archive file fails
g++ -o c a.a b.o

nm shows for the archive a.a and for the object a.o the same symbols. So it
should succeed.
Comment 1 Sebastian Tusk 2006-03-23 15:10:47 UTC
I get the same results with the latest mingw tools and on a debian box.

GNU ld version 2.16.91 20060118 Debian GNU/Linux
gcc version 4.0.3 20060212 (prerelease) (Debian 4.0.2-9)
host, target, build: i486-linux-gnu
Comment 2 Alan Modra 2006-03-23 21:37:58 UTC
See ld info, node "Options" for info on when ld extracts objects from archives.
Comment 3 Sebastian Tusk 2006-03-24 09:25:39 UTC
You are aware of the fact that I didn't use the -l option? From the
documentation you pointed to I would expect that archives specified as input
files without -l are either handled like objects or rejected as invalid input.
Comment 4 Alan Modra 2006-03-24 13:26:33 UTC
It doesn't make any difference whether an archive is specified as -lfoo or
libfoo.a on the command line.  You can force inclusion of the archive using