简体   繁体   中英

Solaris shared libraries and global variables

I have an issue with global variables in shared library on Solaris. Consider following sample:

class Foo
{
public:
 Foo() { Init(); }

private:
  void Init() { // do something }
};

I have some code in shared library:

Foo g_Foo;

I've noticed that Foo constructor is never called on Solaris while the same code works Linux.

I'm using gcc 3.4.3 and Sun linker.

Are you creating the shared object with the -G flag? eg CC -G -o mylib.so myfile.cpp

If you don't specify -G, then the compiler may not initialise global variables correctly. See compiler documentation here .

Note, the docs also say you can't use ld, but need to use CC to do the linking.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM