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?

ZijingWu
  • 3,350
  • 3
  • 25
  • 40

1 Answers1

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.

Community
  • 1
  • 1
jtbandes
  • 115,675
  • 35
  • 233
  • 266
  • 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 Apr 26 '15 at 03:00
  • 1
    @ZijingWu: It's the linker's fault, not clang's, it's because that module isn't referenced from other (needed) modules. – Thomas Apr 26 '15 at 10:55