public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* global objects not initializing -- GCC 3.2.3
@ 2005-01-25 17:11 Nick Steckler
  2005-01-25 17:49 ` Eljay Love-Jensen
  0 siblings, 1 reply; 2+ messages in thread
From: Nick Steckler @ 2005-01-25 17:11 UTC (permalink / raw)
  To: gcc-help

I'm looking for the reason why global objects defined in
statically-linked libraries are not initialized at run time. Example:

-------------------------------------------------------------------------------

bash@puma$ cat lib.cpp
#include <iostream>

class X
{
public:
  X()
  {
    std::cout << "Inside X constructor\n";
  }
};

// instantiate global object
X x;

bash@puma$ cat make_static 
g++ -g -c -o lib.o lib.cpp
ar -r libTest.a lib.o
stecklen(bash)@puma[link_test]$ ./make_static 
stecklen(bash)@puma[link_test]$ cat main.cpp
#include <iostream>

int main()
{
  std::cout << "Inside main.\n";

  return 0;
}
bash@puma$ cat make_bin 
g++ -g -I. main.cpp -L. -lTest -o main.out
bash@puma$ ./make_bin 
bash@puma$ ./main.out 
Inside main.
bash@puma$ 

-------------------------------------------------------------------------------

If I change the line that makes the lib to "g++ -shared -o libTest.so
lib.o" and recompile main, the object is initialized.

Why is this?


Thanks for any information.

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: global objects not initializing -- GCC 3.2.3
  2005-01-25 17:11 global objects not initializing -- GCC 3.2.3 Nick Steckler
@ 2005-01-25 17:49 ` Eljay Love-Jensen
  0 siblings, 0 replies; 2+ messages in thread
From: Eljay Love-Jensen @ 2005-01-25 17:49 UTC (permalink / raw)
  To: Nick Steckler, gcc-help

Hi Nick,

 >I'm looking for the reason why global objects defined in 
statically-linked libraries are not initialized at run time.

Your global object named x is not used in main.cpp, so it is not being 
included (sort of like the opposite "dead code stripping" -- "live code 
inclusion").  And hence, is not being constructed.

To force all the symbols (and their corresponding objects) to be 
incorporated into your main.out from your archive regardless of use, change 
your compilation like this:

g++ -g -I. main.cpp -L. -Wl,--whole-archive -lTest -Wl,--no-whole-archive 
-o main.out

HTH,
--Eljay

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2005-01-25 17:49 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-01-25 17:11 global objects not initializing -- GCC 3.2.3 Nick Steckler
2005-01-25 17:49 ` Eljay Love-Jensen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).