Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8974953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:46:17+00:00 2026-06-15T18:46:17+00:00

Is there any way to make a function get called just by linking its

  • 0

Is there any way to make a function get called just by linking its .o file?

For example:

foo.cpp:

extern int x;

void f() { x = 42; }

struct T { T() { f(); } } t; // we use constructor of global
                             // object to call f during initialization

bar.cpp:

#include <iostream>

int x;

int main()
{
    std::cout << x;
}

To compile/link/run:

$ g++ -c foo.cpp
$ g++ -c bar.cpp
$ g++ foo.o bar.o
$ ./a.out
42

This seems to work with gcc 4.7. It outputs 42 as expected. However I remember on some old compilers I had a problem with this pattern that because nothing was really “using” foo.o it was optimized out at link time. (perhaps this particular example is not representative of the problem for some reason)

What does the C++11 standard have to say about this pattern? Is it guaranteed to work?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T18:46:18+00:00Added an answer on June 15, 2026 at 6:46 pm

    I believe you’re not off the hook. The standard doesn’t guarantee that your code works as intended, although many people rely on that behaviour for various “self-registering” constructions.

    Your object t is dynamically initialized, which has the side effect of calling f. The standard has this to say about dynamic initialization of statically-stored objects (3.6.2/4, “Initialization of non-local variables”):

    It is implementation-defined whether the dynamic initialization of a non-local variable with static storage
    duration is done before the first statement of main. If the initialization is deferred to some point in time
    after the first statement of main, it shall occur before the first odr-use (3.2) of any function or variable
    defined in the same translation unit as the variable to be initialized.

    In your code, only x is odr-used, but x is defined in the main translation unit. No variable or function from the other TU is odr-used in your program, so technically there is no guarantee that t will ever be initialized. Technically, something from every TU must be referred to by the static control flow of your program in order for everything to be initialized.

    As I said, there’s lots of real-world code out there with “self-registering” translation units (which for example register a factory function pointer in a string-keyed map), so that by simply adding TUs to the final program you end up with more functionality. I’m told that most compilers will unconditionally initialize all global variables just because not doing so would break a lot of real-world code. But don’t rely on it!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way I can make Firefox call a JavaScript function in my
Is there any way to make a DataTemplate reference itself just from XAML? In
Is there any way to make the text align left and justify as same
Is there any way to make the form semi-transparent while it is being moved
Is there any way to make the program sleep until an event occurs, but
Is there any way to make two controls that are in different containers the
Is there any way to make this work in Java? public static void change(List<?
Is there any way to make the background of the google charts transparent. Google
Is there any way to make a list of classes behave like a set
is there anyway in QTP to make changes take place on all function libraries

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.