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 7561295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:07:39+00:00 2026-05-30T13:07:39+00:00

I have created a system that automatically registers function objects (functors) into a map

  • 0

I have created a system that automatically registers function objects (functors) into a map based on the constructor of an global instance.

In each cpp file that defines the functor, there’s a global instance of the registrar class instance to register the functor to a singleton std::map<int, std::function<...> > object.

This is the definition of registrar class:

template
<
    typename map_type,
    typename handler_type
>
struct registrar
{
    registrar
        (
             map_type& map_object,
             boost::uint16_t cmd_code,
             const handler_type& handler
        )
        {
          map_object.insert(std::pair<boost::uint16_t, handler_type>(cmd_code, handler));
        }
};

in each .cpp file. The global instance is defined like this:

namespace one_way
{
    static registrar <in_out_map_type, handler>
        post_receiver(in_out_map_type::instance(), command, handlers());
}

All works fine if I compile all the cpp with the main.cpp together. But If I compile the cpp file into a static library and link it to main.cpp, the registration does not work.

I tested with VC10 and GCC4.61 both on Windows & and Ubuntu 11.10. Both fail.

I found a thread with the same problem but OP did not say whether he solved it or not.

Am I missing anything?


Edit


Thanks for all responses including the comments.

Every response indeed helped me to think more and investigate deep into this method. After all the study and trials, I finally gave up the idea of relying on global/static variable for self-registration across binary boundaries, because there’s no portable way to guarantee it will work.

My final way is to keep the registration within one binary.

  • 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-05-30T13:07:41+00:00Added an answer on May 30, 2026 at 1:07 pm

    I believe your object file from the library is not getting linked. Look how Microsoft handles the acrtused symbol (search case insensitive, I do not remember the case and there’s no MSVC on this machine).

    Once you know how they handle acrtused, do the same thing with your global variable to force it to get linked.

    Will update if I find the answer.

    Here are a couple of possibilities to force things to link and initialize in a somewhat forced order.

    Look here for a GCC answer.

    Look here for MSVC10.

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

Sidebar

Related Questions

I have created an MVC application that dynamically routes actions and controllers based on
I am trying to re-write an existing system with objects. I have created a
I have an InstallShield (InstallScript) installation, created using IS2009, that's built automatically every night,
I have created windows service. I need to run that automatically after 2 minutes.
I have written a system that registers metadata for a class when initiating a
I have a system that creates an order and that order can be billed
I have a system-wide manual reset event that I create by doing the following:
I have a WCF service that uses a System.ServiceModel.Syndication.SyndicationFeed to create an RSS feed.
On the class level, I have created reference: System::Data::Odbc::OdbcConnection Connection; in some method I
I have created a WCF service for uploading images , which accepts System.IO.Stream as

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.