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

  • Home
  • SEARCH
  • 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 6131605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:59:45+00:00 2026-05-23T16:59:45+00:00

Developing environment GNU GCC (g++) 4.1.2, CMAKE After I read some authoritative book named

  • 0

Developing environment GNU GCC (g++) 4.1.2, CMAKE

After I read some authoritative book named ‘Advanced C++ Programing Styles and Idioms by James Coplien (I must admit that this book is quite old. So some of you may find it a sort of out-dated.), I found the ‘Exemplar idiom’ was quite useful to impair the inter-dependency among inheritance class hierarchy. So tried to create simple project for handling this idiom as follows.

Thank you for your patience in advance while I list the entire source code as follows.

In CMakeLists.txt

cmake_minimum_required(VERSION 2.6)

project(TestProject)

add_library(exemplar_idiom STATIC base.cpp derived1.cpp)

add_executable(exemplar_example main.cpp)
target_link_libraries(exemplar_example exemplar_idiom)
set_target_properties(exemplar_example PROPERTIES OUTPUT_NAME exemplar_example)

In exemplar.h

#ifndef EXEMPLAR_H_
#define EXEMPLAR_H_

class   Exemplar
{
public:
    Exemplar() {};
};

#else
#endif  //  EXEMPLAR_H_

In base.h

#ifndef BASE_H_
#define BASE_H_

#include <string>

class   Exemplar;

class   Base
{
public:
    Base(Exemplar /* no use */);
    Base();

    virtual Base*   make(const std::string& key);

    virtual void    memberMethod1();
    virtual int     memberMethod2();

    virtual ~Base() {};
protected:
    static  Base*   list_;
    Base*   next_;

};

#else
#endif  //  BASE_H_

In base.cpp

#include <string>
#include <iostream>

#include "exemplar.h"
#include "base.h"

Base* Base::list_ = 0;
static Exemplar exemplar;
static Base baseInstance(exemplar);
Base*   baseExemplar = &baseInstance;

Base::Base(Exemplar /* no use */) : next_(list_)
{
    std::cout << "Base object exemplar ctor" << std::endl;
    list_ = this;
}

Base::Base() : next_(0) {}

Base* Base::make(const std::string& key)
{
    Base* retval = 0;

    for (Base* a = list_; a; a = a->next_)
    {
        if (a != baseExemplar)
        {
            if ((retval = a->make(key)))
            {
                break;
            }
        }
        else if (key == "base")
        {
            retval = new Base();
            break;
        }
    }
    return retval;
}

void Base::memberMethod1()
{
    std::cout << "base class memberMethod1() called." << std::endl;
}

int Base::memberMethod2()
{
    std::cout << "base class memberMethod2() called." << std::endl;
    return 0;
}

In derived1.h

#include "base.h"

#ifndef DERIVED1_H_
#define DERIVED1_H_

class   Exemplar;

class   Derived1 : public Base
{
public:
    Derived1(Exemplar /* no use */);

    //  Conventional default constructor which will be used to instantiate normal object
    Derived1();

    virtual Derived1*   make(const std::string& key);
    virtual void    memberMethod1();
    virtual int     memberMethod2();
};

#else
#endif  //  DERIVED1_H_

In derived1.cpp

#include <iostream>

#include "exemplar.h"
#include "derived1.h"

static Exemplar exemplar;
static Derived1 derived1Exemplar(exemplar);

Derived1::Derived1(Exemplar a) : Base(a)
{
    std::cout << "Derived object exemplar ctor" << std::endl;
}

Derived1::Derived1() : Base() {}

Derived1*   Derived1::make(const std::string& key)
{
    Derived1* retval = 0;
    if (key == "derived1")
    {
        retval = new Derived1();
    }
    return retval;
}

void    Derived1::memberMethod1()
{
    std::cout << "Derived1::memberMethod1" << std::endl;
}

int    Derived1::memberMethod2()
{
    /*  dummy   */
    std::cout << "Derived1::memberMethod2" << std::endl;
    return 0;
}

In main.cpp

#include "base.h"

extern Base* baseExemplar;

int main()
{
    Base *ptr = baseExemplar->make("derived1");
    if (ptr) {
        ptr->memberMethod1();
        ptr->memberMethod2();
    }

    Base *ptr2 = baseExemplar->make("base");
    if (ptr2) {
        ptr2->memberMethod1();
        ptr2->memberMethod2();
    }

    return 0;
}

When we change the CMakeLists.txt slightly to create shared object by substituting the keyword of ‘STATIC’ as ‘SHARED’, then I could see the behavior that I expected. I could see that as a result of static object creation of Derived1 class object (with Exemplar arg), the link list chain in the Base class is properly maintained. And after that, when invoking the virtual make() method from the base exemplar object in main(), I could see both Derived1 object and Base object were successfully created by specifying key string of “derived1” and “base”.

However, if I revert back the keyword ‘SHARED’ to ‘STATIC’, then I could see the different result. It seems that such static object (Derived1 exemplar object) is not created at all. So it failed to create the Derived1 object by invoking make(“derived1”) member method.

Is there any way to avoid this issue, even in the case of static library? I would like to expect static object constructor’s side-effect towards Base class’ link list.

I’m not exactly sure of whether what I’m trying to make here is making sense to you. However, it would be greatly appreciated if you could give me the pointer on the above.

  • 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-23T16:59:46+00:00Added an answer on May 23, 2026 at 4:59 pm

    Yes, the GNU linker ld only adds those symbols that it thinks are actually used. ld can only check if the symbol is in use, but not if the side effects of the symbol’s initialization is used.

    Thus, the outcome will be a situation like yours, failures due to non initialization of an unused static object.

    You can use --whole-archive when linking to tell the linker to include all symbols. The drawback is that symbols that you never want to use will also get added and result in increased code size.

    Also another thing to note is:

    The linker drops unused object files only if they are from a library. Object files explicitly passed to the linker will be always linked.

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

Sidebar

Related Questions

Developing environment: GNU GCC (g++) 4.1.2 While I'm trying to investigate how to increase
After developing in VS.NET for many years, I'm starting to do some Xcode development
I'm developing a Sinatra-based application and have seen some apps using an environment.rb file
I'm developing in an environment that is severely constrained, but the developers also have
I'm currently developing in a closed environment a web based slide show of sorts,
I am developing a WebPart (it will be used in a SharePoint environment, although
I'm developing a customized document library in the SharePoint 2007 environment. What I'm doing
I'm currently developing a new language for programming in a continuous environment (compare it
Firstly, I come from Windows-VisualStudio-C++ background. Now I am developing in a Ubuntu environment.
I use a chroot development environment for developing software for devices. The chroot dev

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.