2

We have an library static_library.a build by clang++, and there is a file bar.cpp include an global object Foo.

But when we use the library in App layer Xcode project, but the global object Foo constructor doesn't been called. (The global object constructor will do some registration job, and impact the app behavior.)

We think the translation unit are not linked into the executable file.

//bar.cpp in static_library.a
class Foo
{
public:
   Foo()
   {
       std::cout << " constructor called" << std::endl;
   }
};

Foo a;
// <------If this function is called in the App layer project, the
// global constructor object will be called. 
Foo* getInstance()  
{
   return &a;
}

So does there any flag, which can control this behavior?

4
  • Can you try -all_load? Commented Apr 23, 2015 at 4:07
  • 1
    a better way to control would be to have static variable in getInstance function, whenever the function is called your object will be instantiated Commented Apr 23, 2015 at 4:08
  • But there are a lot of global variable in a lot of translation unit, so we cannot list all of them. Commented Apr 23, 2015 at 6:26
  • @jtbandes, -all_load works. If you can make it an answer, I will make it as the correct one. Thanks. Commented Apr 23, 2015 at 6:27

1 Answer 1

1

You most likely need the -all_load linker flag.

This question has more details. You may also be interested in -ObjC or -force_load.

Sign up to request clarification or add additional context in comments.

2 Comments

I didn't find much information about -all_load in the google. Without the -all_load, seams the behaviour voliate the C++ standard. So why clang++ have that and make it default?
@ZijingWu: It's the linker's fault, not clang's, it's because that module isn't referenced from other (needed) modules.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.