Execution of a function on startup

D

Dave

Hello NG,

If a particular dynamic library gets loaded, I need to ensure that a
function in that library gets executed on startup (and I want the mechanism
to use only Standard C++). Here's what I've got:

void func()
{
// This is the function that must be executed
}

struct foo
{
foo() {func();}
} global_var;

I would like to solicit suggestions on more elegant alternatives. (I
realize that elegance is subjective!)

Thank you!

Dave
 
V

Victor Bazarov

Dave said:
If a particular dynamic library gets loaded, I need to ensure that a
function in that library gets executed on startup (and I want the mechanism
to use only Standard C++). Here's what I've got:

void func()
{
// This is the function that must be executed
}

struct foo
{
foo() {func();}
} global_var;

I would like to solicit suggestions on more elegant alternatives. (I
realize that elegance is subjective!)

The more _sure_ alternative to call that function _upon_loading_ of that
dynamic library would be to call it from the dynamic library _loading_
routine, which is OS-specific.

As for elegance, your method is fine. Of course, if you could make your
function to return an int, then you could make it a bit simpler:

int func() { /* blah */ return 0; }
int global_var = func();

V
 
W

White Wolf

Dave said:
Hello NG,

If a particular dynamic library gets loaded, I need to ensure that a
function in that library gets executed on startup (and I want the
mechanism to use only Standard C++). Here's what I've got:

void func()
{
// This is the function that must be executed
}

struct foo
{
foo() {func();}
} global_var;

I would like to solicit suggestions on more elegant alternatives. (I
realize that elegance is subjective!)

It is worse than that (being subjective). As the standard C++ language (the
one discussed here) has nothing to say about dynamic load libraries (yet),
this is not the best place for asking this question. Or running the risk of
being politically incorrect I may say, this is not the place to ask this
question. ;-) If you need another way to do what you have asked, you will
need to ask it in a newsgroup or other forum dedicated to your
platform/compiler. Most probably there is a way to do that.
 
D

Dave

White Wolf said:
It is worse than that (being subjective). As the standard C++ language (the
one discussed here) has nothing to say about dynamic load libraries (yet),
this is not the best place for asking this question. Or running the risk of
being politically incorrect I may say, this is not the place to ask this
question. ;-) If you need another way to do what you have asked, you will
need to ask it in a newsgroup or other forum dedicated to your
platform/compiler. Most probably there is a way to do that.

--
WW aka Attila
:::
Cole's Axiom: The sum of the intelligence on the planet is a constant. The
population is growing.


I specifically want to stay within the realm of Standard C++. I don't want
to do anything compiler-specific. I probably should not have mentioned the
words "dynamic library" since their presence naturally tends to cause people
to think I'm looking for something platform-specific even though my stated
intent was the opposite. So, let's pose the problem this way:

I need to ensure that a given function is called upon program startup before
control is transferred to main(). I would like to solicit suggestions on
ways I may accomplish this that fall strictly within the standard language.

I'm looking for creative / varied ways to accomplish this as a means of 1)
Possibly improving the code I'm maintaining; 2) Expanding my knowledge of
C++.

Thanks again,
Dave
 
V

Victor Bazarov

Dave said:
[...]
I specifically want to stay within the realm of Standard C++. I don't want
to do anything compiler-specific. I probably should not have mentioned the
words "dynamic library" since their presence naturally tends to cause people
to think I'm looking for something platform-specific even though my stated
intent was the opposite.

But you'd not be getting the best advice on a platform-specific feature,
if you try to look for non-platform-specific mechanism to do something
with a platform-specific element (dynamic libraries in this case). No,
I am not encouraging you to seek platform-specific advice here. Just
trying to indicate the issues with your approach.

Don't get us wrong, please.
So, let's pose the problem this way:

I need to ensure that a given function is called upon program startup before
control is transferred to main().

Just to be totally generic, loading of a dynamic library doesn't
necessarily happen before 'main' is called. See 'dlopen' or 'LoadLibrary'
or whatever it is on your platform.

And, yes, as a very beginner-level C++ test question, "how to call
a function before 'main' is given control" is something we can answer,
and, besides, you had answered it in your original post.
I would like to solicit suggestions on
ways I may accomplish this that fall strictly within the standard language.

That's the problem WW was trying to indicate: there are no dlls in the
standard language. So, now you're switching from a particular problem to
a generic problem, and a solution to the latter is not necessarily
an acceptable solution to the former.
I'm looking for creative / varied ways to accomplish this as a means of 1)
Possibly improving the code I'm maintaining;

Staying within confines of the standard language and library may not be
an improvement in that case. Using proper OS-specific mechanisms might.
Consider yourself warned.
2) Expanding my knowledge of
C++.

That never hurts, of course.

V
 
D

Dave

Victor Bazarov said:
Dave said:
[...]
I specifically want to stay within the realm of Standard C++. I don't want
to do anything compiler-specific. I probably should not have mentioned the
words "dynamic library" since their presence naturally tends to cause people
to think I'm looking for something platform-specific even though my stated
intent was the opposite.

But you'd not be getting the best advice on a platform-specific feature,
if you try to look for non-platform-specific mechanism to do something
with a platform-specific element (dynamic libraries in this case). No,
I am not encouraging you to seek platform-specific advice here. Just
trying to indicate the issues with your approach.

Don't get us wrong, please.
So, let's pose the problem this way:

I need to ensure that a given function is called upon program startup before
control is transferred to main().

Just to be totally generic, loading of a dynamic library doesn't
necessarily happen before 'main' is called. See 'dlopen' or 'LoadLibrary'
or whatever it is on your platform.

And, yes, as a very beginner-level C++ test question, "how to call
a function before 'main' is given control" is something we can answer,
and, besides, you had answered it in your original post.
I would like to solicit suggestions on
ways I may accomplish this that fall strictly within the standard
language.

That's the problem WW was trying to indicate: there are no dlls in the
standard language. So, now you're switching from a particular problem to
a generic problem, and a solution to the latter is not necessarily
an acceptable solution to the former.
I'm looking for creative / varied ways to accomplish this as a means of 1)
Possibly improving the code I'm maintaining;

Staying within confines of the standard language and library may not be
an improvement in that case. Using proper OS-specific mechanisms might.
Consider yourself warned.
2) Expanding my knowledge of
C++.

That never hurts, of course.

V

Please folks, forget the dynamic libs. They don't matter. They might as
well have never existed. The program I'm maintaining already works as it
is. I don't *need* another technique. I just *want* to identify creative
ways to invoke a function upon program startup, before main() is called,
because this is 99% an academic exercise to expand knowledge. If I happen
to find a technique that I like better than what's been done, I will use it,
but it is completely beside the point. To those so inclined towards
thinking of creative ways to use the language, I am giving you a friendly
challenge to exercise your C++ muscles and dazzle the NG with your
creativity / elegance!
 
D

Dave

Victor Bazarov said:
Dave said:
[...]
I specifically want to stay within the realm of Standard C++. I don't want
to do anything compiler-specific. I probably should not have mentioned the
words "dynamic library" since their presence naturally tends to cause people
to think I'm looking for something platform-specific even though my stated
intent was the opposite.

But you'd not be getting the best advice on a platform-specific feature,
if you try to look for non-platform-specific mechanism to do something
with a platform-specific element (dynamic libraries in this case). No,
I am not encouraging you to seek platform-specific advice here. Just
trying to indicate the issues with your approach.

Don't get us wrong, please.
So, let's pose the problem this way:

I need to ensure that a given function is called upon program startup before
control is transferred to main().

Just to be totally generic, loading of a dynamic library doesn't
necessarily happen before 'main' is called. See 'dlopen' or 'LoadLibrary'
or whatever it is on your platform.

And, yes, as a very beginner-level C++ test question, "how to call
a function before 'main' is given control" is something we can answer,
and, besides, you had answered it in your original post.
I would like to solicit suggestions on
ways I may accomplish this that fall strictly within the standard
language.

That's the problem WW was trying to indicate: there are no dlls in the
standard language. So, now you're switching from a particular problem to
a generic problem, and a solution to the latter is not necessarily
an acceptable solution to the former.
I'm looking for creative / varied ways to accomplish this as a means of 1)
Possibly improving the code I'm maintaining;

Staying within confines of the standard language and library may not be
an improvement in that case. Using proper OS-specific mechanisms might.
Consider yourself warned.
2) Expanding my knowledge of
C++.

That never hurts, of course.

V

And by the way, thank you for the alternative you have already offered!
 
V

Victor Bazarov

Dave said:
[..] To those so inclined towards
thinking of creative ways to use the language, I am giving you a friendly
challenge to exercise your C++ muscles and dazzle the NG with your
creativity / elegance!

Thanks, Dave. It's just what we need. I can really see now how we've
stagnated here over the years. How many readers here have written a C++
book? How many have patented a template technique? Embarrassing...
 
D

Dave

Victor Bazarov said:
Dave said:
[..] To those so inclined towards
thinking of creative ways to use the language, I am giving you a friendly
challenge to exercise your C++ muscles and dazzle the NG with your
creativity / elegance!

Thanks, Dave. It's just what we need. I can really see now how we've
stagnated here over the years. How many readers here have written a C++
book? How many have patented a template technique? Embarrassing...

Oh for God's sake, I give up. This is clearly not the place to get an
answer to a Standard C++ question. I obviously won't be missed (nor will I
miss the sarcasm).
 
V

Victor Bazarov

Dave said:
[...]
Oh for God's sake, I give up.

Come on, what is this, your first day on Usenet? Don't give up
simply because you've received a sarcastic remark in response to
your own. OTOH, who cares?... Bye!
 
W

White Wolf

Dave wrote:
[SNIP]
I specifically want to stay within the realm of Standard C++. I don't
want to do anything compiler-specific. I probably should not have
mentioned the words "dynamic library" since their presence naturally
tends to cause people to think I'm looking for something
platform-specific even though my stated intent was the opposite. So,
let's pose the problem this way:

I need to ensure that a given function is called upon program startup
before control is transferred to main(). I would like to solicit
suggestions on ways I may accomplish this that fall strictly within the
standard language.

I'm looking for creative / varied ways to accomplish this as a means of
1) Possibly improving the code I'm maintaining; 2) Expanding my
knowledge of C++.


The reason why I have told you that you are asking the question in the wrong
place is because *nothing* whatsoever guarantees that *any* advice I give
you here based on my knowledge of Standard C++ will work once you try to use
in in your dynamic load libraries. Got it? When I say Standard C++ has
nothing to say (as of today, and I would say unfortunately) about dynamic
load libraries it also means, that any Standard solution I (or anyone else)
gives you here has about the same chance to work in the presence of dynamic
loading as any non-standard one. I know it sounds like I am unreasonable,
but this is really how it is. Once you say your platform, there is more
chance to give you useful ideas. But once you did that, we are off-topic
here. :)
 
S

SunDog

Dave said:
Victor Bazarov said:
Dave said:
[..] To those so inclined towards
thinking of creative ways to use the language, I am giving you a friendly
challenge to exercise your C++ muscles and dazzle the NG with your
creativity / elegance!

Thanks, Dave. It's just what we need. I can really see now how we've
stagnated here over the years. How many readers here have written a C++
book? How many have patented a template technique? Embarrassing...

Oh for God's sake, I give up. This is clearly not the place to get an
answer to a Standard C++ question. I obviously won't be missed (nor will I
miss the sarcasm).

dave:
please dont let this stuff bother you - just ignore it. its somewhat common
on the newsgroups (not that that excuses it) and victor has a long history
here of the sarcasm you mention as well as being a general ass with an
arrogant know it all attitude. the c++ newsgroups are apparently his life;
what more can I say? its really kind of sad.

keep posting and learning!

best,
dan
 

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

Forum statistics

Threads
474,197
Messages
2,571,041
Members
47,643
Latest member
ashutoshjha_1101

Latest Threads

Top