problems with startup code in static libraries.

Discussion in 'C++' started by Bramz, Jan 28, 2004.

  1. Bramz

    Bramz Guest

    Hi all,

    I have a rather strange linker (?) problem. At least, I think it's a linker
    problem, but I'm not so sure. It's a bit lengthy to describe ...

    I've implemented a generic factory similar to the one in 'modern c++ design'
    (page 208). To initialize this factory with the different products, I'm
    using the same technique on page 204-205 that bassicly looks like:

    namespace
    {
    bool foo() { stuff that needs to be done; }
    const bool bar = foo();
    }

    the real thing looks like the following, which is expanded from the macro
    LASS_EXECUTE_BEFORE_MAIN(fdtd::rec::deviceGraphicsFactory()->subscribe("avi"
    , fdtd::rec::DeviceGraphicsAvi::make);):

    namespace
    {
    bool lassDummyName12func ()
    {
    fdtd::rec::deviceGraphicsFactory()->subscribe("avi",
    fdtd::rec::DeviceGraphicsAvi::make);
    return true;
    };
    const bool lassDummyName12var = lassDummyName12func ();
    }

    the problem is that i'm doing this inside a project (MSVC7) that creates a
    static library "rec", and this static library is inserted in another project
    that builds the executable "app".

    So far, no problem i thought, but the funny thing is, it doesn't work in the
    file i do want it to be in: "device_graphics_avi.cpp".
    LASS_EXECUTE_BEFORE_MAIN(...) never does execute its argument. But if I
    move the exact same thing to a file "fdtd.cpp" in the main project for the
    executable ("app"), it suddenly _does_ work: the product gets subscribed to
    the factory, and all works well.

    I even managed to move LASS_EXECUTE_BEFORE_MAIN(...) to another file
    "recorder_2d_graphical.cpp" inside the static library project ("rec") where
    it _does_ gets executed.

    I've tried various things. To replace the code to be executed by simple
    dummy code, to expand the macro myself and try various variants on it. But
    in the file I want it to be (device_graphics_avi.cpp) the start up code
    never gets executed. Whatever I try to execute by
    LASS_EXECUTE_BEFORE_MAIN(...) will have the following effect depending on
    the file it is in:
    static library project:
    - "device_graphics_avi.cpp" in "rec": not executed
    - "recorder_2d_graphical.cpp" in "rec": _is_ executed
    executable project:
    - "fdtd.cpp" in "app": _is_executed

    I have absolutely _no_ idea what's going on. It's about the exact same code
    (just placed in different source files), all source files get compiled
    without errors, all projects are build without linker errors. "rec" is
    linked in "app". And yet, the start up code is not executed if I put it in
    device_graphics_avi.cpp.

    I _really_ do want it to put in device_graphics_avi.cpp, since its a single
    module, a single product in a single source file that register itself to the
    object factory.

    The question: Does anyone know what might be going on here? It would be
    really helpfull.

    Thanks a lot in advance,
    Bram de Greve
     
    Bramz, Jan 28, 2004
    #1
    1. Advertising

  2. "Bramz" <> wrote...
    > I have a rather strange linker (?) problem. At least, I think it's a

    linker
    > problem, but I'm not so sure. It's a bit lengthy to describe ...
    > [...]
    > The question: Does anyone know what might be going on here? It would be
    > really helpfull.


    Yes, somebody in a newsgroup dedicated to your compiler is bound to know.
    Linking is compiler- and platform-specific and as such OT here.
     
    Victor Bazarov, Jan 28, 2004
    #2
    1. Advertising

  3. On Wed, 28 Jan 2004 16:10:37 +0100, Bramz wrote:

    > I _really_ do want it to put in device_graphics_avi.cpp, since its a single
    > module, a single product in a single source file that register itself to the
    > object factory.
    >
    > The question: Does anyone know what might be going on here? It would be
    > really helpfull.


    I guess the file does not get linked in because you don't use anything of
    it. Make sure you actually use something from that sourcefile, this can
    ofcourse be from another file in the same library (that is used).

    Or link in the object directly, not from a library. Mind you, istr MSVC 5
    had some problems linking in stuff like this, we had to jump through
    some strange hoops to make MSVC include objects that where not otherwise
    used. Don't know about the newer versions.

    HTH,
    M4
     
    Martijn Lievaart, Jan 28, 2004
    #3
  4. Bramz

    Bramz Guest

    Victor: my apologies, I'm not a frequent user of newsgroups, and I wasn't
    really aware I should ask this question in here. Next time, I'll try to
    find a more proper newsgroup :)

    Martijn: thank you for your answer. It confirms what I already suspected.

    Thank you for your time,
    Bramz
     
    Bramz, Jan 29, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Barry

    WebApplication Startup Problems

    Barry, Jun 6, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    366
    Barry
    Jun 6, 2005
  2. Replies:
    2
    Views:
    6,512
    Maett
    May 16, 2005
  3. Marcelo Fernandez

    Problems with static libraries

    Marcelo Fernandez, May 26, 2007, in forum: C++
    Replies:
    2
    Views:
    357
    Ron AF Greve
    May 26, 2007
  4. Karsten Wutzke
    Replies:
    21
    Views:
    920
    Roedy Green
    Jun 29, 2007
  5. Sriram Srinivasan
    Replies:
    13
    Views:
    567
    Benjamin Kaplan
    Nov 12, 2009
Loading...

Share This Page