How to link in static objects from a static library? - Linux

This is a discussion on How to link in static objects from a static library? - Linux ; Hi, I am creating a static library (using "ar -cq ...") from a bunch of .o files that all contain static "registrar" objects in certain classes. In particular, I have a templated Registrar class, and each of the classes being ...

+ Reply to Thread
Results 1 to 2 of 2

Thread: How to link in static objects from a static library?

  1. How to link in static objects from a static library?

    Hi,

    I am creating a static library (using "ar -cq ...") from a bunch of .o
    files that all contain static "registrar" objects in certain classes.
    In particular, I have a templated Registrar class, and each of the
    classes being archived has a static member of type
    Registrar that is supposed to "register" a constructor
    object for that class. When I explicitly list for the linker all of the
    ..o files that I am archiving, these Registrar objects are all
    automatically linked into the executable such that they are all
    constructed before main is called when I run the program. However, if I
    instead try to link against an archive/static library of these very
    same .o files, these Registrar objects are NOT being linked into the
    executable.

    I am wondering if there is a flag or something that I need to use in
    order to force the linker to link in all static objects from the static
    library in question? Or is this not possible to do? Any help would be
    greatly appreciated. Thanks!

    Matt

    Here are trimmed down versions of the commands I am running (all of
    these commands run w/o errors, save the static object issue mentioned
    above):

    Archive command:
    ar -cq libs/libMyArchive.a
    obj/rel/Uberserver/Credit/DO/TestFramework/DOThroughputTest.o
    obj/rel/Uberserver/Credit/DO/TestFramework/DOCircularityTest.o
    obj/rel/Uberserver/Credit/DO/TestFramework/DOLatencyTest.o
    obj/rel/Uberserver/Credit/DO/TestFramework/DOIONodeFiller.o [more .o
    files]

    Link command that explicitly lists .o files (and WORKS):
    g++ -g -L/creditdev/packages/pgsql/lib
    -L/creditdev/packages/emgtools/lib-g++-3.4.3 -rdynamic
    obj/rel/Base/StringUtilities.o obj/rel/Base/DebugUtils.o
    obj/rel/Base/ConsoleUtilities.o obj/rel/Base/Time.o
    obj/rel/Uberserver/Credit/DO/TestFramework/DOThroughputTest.o
    obj/rel/Uberserver/Credit/DO/TestFramework/DOCircularityTest.o
    obj/rel/Uberserver/Credit/DO/TestFramework/DOLatencyTest.o
    obj/rel/Uberserver/Credit/DO/TestFramework/DOIONodeFiller.o [more .o
    files] --start-group /creditdev/shared_libraries/libstdc++.so.6
    /usr/local/ssl/lib/libcrypto.a -lEmgToolsSystemUtils -ltds -lpthread
    -lncurses -ldl -lpq --end-group -o rel/slave

    Link command that links against .a file (and does NOT work):
    g++ -g -L/creditdev/packages/pgsql/lib
    -L/creditdev/packages/emgtools/lib-g++-3.4.3 -rdynamic
    obj/rel/Base/StringUtilities.o obj/rel/Base/DebugUtils.o
    obj/rel/Base/ConsoleUtilities.o obj/rel/Base/Time.o [more .o files]
    --start-group libs/libMyArchive.a
    /creditdev/shared_libraries/libstdc++.so.6
    /usr/local/ssl/lib/libcrypto.a -lEmgToolsSystemUtils -ltds -lpthread
    -lncurses -ldl -lpq --end-group -o rel/slave


  2. Re: How to link in static objects from a static library?

    golfmat@yahoo.com writes:

    > When I explicitly list for the linker all of the
    > .o files that I am archiving, these Registrar objects are all
    > automatically linked into the executable such that they are all
    > constructed before main is called when I run the program. However, if I
    > instead try to link against an archive/static library of these very
    > same .o files, these Registrar objects are NOT being linked into the
    > executable.


    That is *exactly* how it's supposed to work. More here:
    http://webpages.charter.net/ppluzhnikov/linker.html

    > I am wondering if there is a flag or something that I need to use in
    > order to force the linker to link in all static objects from the static
    > library in question?


    Sure:

    g++ ... -Wl,--whole-archive -lMyArchive -Wl,--no-whole-archive ...

    > Link command that links against .a file (and does NOT work):
    > g++ -g -L/creditdev/packages/pgsql/lib
    > -L/creditdev/packages/emgtools/lib-g++-3.4.3 -rdynamic
    > obj/rel/Base/StringUtilities.o obj/rel/Base/DebugUtils.o
    > obj/rel/Base/ConsoleUtilities.o obj/rel/Base/Time.o [more .o files]
    > --start-group libs/libMyArchive.a
    > /creditdev/shared_libraries/libstdc++.so.6
    > /usr/local/ssl/lib/libcrypto.a -lEmgToolsSystemUtils -ltds -lpthread
    > -lncurses -ldl -lpq --end-group -o rel/slave


    Your link line is all screwed up:
    - you should never explicitly link libstdc++ -- g++ will add it
    for you (and in correct place).
    - the '--start-group' and '--end-group' should be prefixed with
    '-Wl,' so they are correctly passed to the linker.
    - you should never link with '-lpthread' either; use '-pthread'
    at compile *and* link time instead.

    Cheers,
    --
    In order to understand recursion you must first understand recursion.
    Remove /-nsp/ for email.

+ Reply to Thread