Path: utzoo!mnetor!uunet!lll-winken!lll-lcc!ames!hao!husc6!mit-eddie!uw-beaver!uw-june!brian From: brian@uw-june.UUCP (Brian Bershad) Newsgroups: comp.lang.c++ Subject: problems with ld and static constructors Message-ID: <3901@uw-june.UUCP> Date: 31 Dec 87 17:19:50 GMT Reply-To: brian@uw-june.UUCP (Brian Bershad) Organization: U of Washington, Computer Science, Seattle Lines: 50 Keywords: bug, munch, static constructors If definitions for static vars having constructors are in a file that has nothing else (or nothing else called by the rest of the program), and that file is placed in an 'ar' archive, the pre-munch pass of ld does not pull that file in. So, nm does not see the STI defs, and munch can't do the right thing. Try: mystream.c: ---------------- #include char pcout_buf[BUFSIZE]; filebuf pcout_file(stdout); // UNIX output stream 1 ostream pcout(&pcout_file); ---------------- main.c: --------------- #include extern ostream pcout; main() { pcout << "Hello World\n"; } With: CC -c mystream.c main.c CC mystream.o main.o all is well, but with CC -c mystream.c main.c ar r foo.a mystream.o CC main.o foo.a a seg fault occurs on the first call to pcout since it isn't getting properly constructed. A kludge is to put a function in mystream.c that gets called from main.c so as to pull the whole file in. Anyone have a better one? -- brian@june.cs.washington.edu Brian Bershad {ihnp4,decvax,ucbvax}!uw-beaver!uw-june!brian Dept. of Computer Science, FR-35 University of Washington Seattle, WA 98195