Boost logo

Boost :

From: Vladimir Prus (ghost_at_[hidden])
Date: 2007-05-15 02:32:02


Douglas Gregor wrote:

> I'm having trouble getting programs using boost_unit_test_framework
> to link. The fundamental issue is that trying to link something like
> this:
>
> g++ -fPIC -o algorithms_test algorithms_test.o -
> lboost_unit_test_framework
>
> dies with a linker error:
>
> /usr/bin/ld: Undefined symbols:
> _main
> collect2: ld returned 1 exit status
>
> The issue, of course, is that "main" lives within the library
> boost_unit_test_framework, but linkers don't look into libraries
> (either static or dynamic) to find main.

I think this is false, see below.

> Our tests pass because BBv2 decides to put the archive file on the
> link line:
>
> g++ -fPIC -o algorithms_test algorithms_test.o
> libboost_unit_test_framework.a
>
> For some reason, this non-standard practice seems to work... but I
> can't imagine it's truly portable,

It's absolutely portable.

> and it forces users of
> boost_unit_test_framework into statically linking in a non-
> traditional way.

The above statements all seem to be false. There's no difference, in my testing,
between

        libfoo.a

and

        -L . -lfoo

provided the latter finds the same libfoo.a.

Further, in my testing putting 'main' in static library works. I believe that
even works on Windows. Furthermore, previous testing reveals that on Linux,
putting 'main' in shared library works as well.

You can easily test it for yourself.

> Why must main() be in a library? It seems a trivial matter to fix.

The real issue is that there's no 'main()' in library, as shown below:

ghost_at_wind:~/Work/Boost/boost-rc/stage/lib$ readelf --symbols libboost_unit_test_framework-gcc40-d.so | grep main
   871: 00074a43 17 OBJECT WEAK DEFAULT 12 _ZTSSt12domain_error
  1253: 0007ed68 12 OBJECT WEAK DEFAULT 22 _ZTISt12domain_error
   165: 00000000 0 FILE LOCAL DEFAULT ABS unit_test_main.cpp
   259: 0007ed98 4 OBJECT LOCAL HIDDEN 22 DW.ref._ZTISt12domain_err
  1128: 00074a43 17 OBJECT WEAK DEFAULT 12 _ZTSSt12domain_error
  1510: 0007ed68 12 OBJECT WEAK DEFAULT 22 _ZTISt12domain_error

When both static and shared libraries are present, gcc picks shared
on most platforms of note. Since shared library lacks 'main', you get
linker error.

I believe this is intentional change made in Boost.Test for 1.34, in the
interest of making Linux (where 'main' can be in shared library) and
Windows (where 'main' cannot be in shared library), uniform.

However, you'd need to ask Gennadiy how to your tests work now.

- Volodya


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk