problems with startup code in static libraries.

B

Bramz

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
 
V

Victor Bazarov

Bramz said:
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.
 
M

Martijn Lievaart

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
 
B

Bramz

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,162
Latest member
GertrudeMa
Top