Article View: gnu.g++.bug
Article #1230g++ and shared libraries
From: Fred Appelman
Date: Sat, 21 Nov 1992 12:11
Date: Sat, 21 Nov 1992 12:11
134 lines
4741 bytes
4741 bytes
This message is cross posted to gnu.g++.bug and comp.unix.sysv386.
I'm having problems with shared libraries build by g++ on a
Unix386 platform (Dell 2.2). I'm not sure if this problem is caused
by g++ or just by not using the proper commands.
Problem description: global variables in the shared library are not initialzed.
If you have a global variable (simple scalar or object) in a library,
that variable needs to be initialized (constructed). If I build a
normal library (with ar) there are no problems. The variables are
initialized as expected. If however I create a shared library (with the
same code) the variable does not get initialized. IMHO either g++ is not
aware of the fact that it should call some initialization routine before
calling main() or the commands I use to construct the shared library
are wrong(incomplete). The following shar archive shows the problem. On
my system, the output is the following:
$ Make
g++ -fpic -c shared_bug.C
g++ -c main.C
g++ -o standalone shared_bug.o main.o
ld -G -o libbug.so shared_bug.o
g++ -o withlib main.o -L. -lbug
./run_test
Running the standalone version, expecting: iBug=20
iBug=20
Running the version with the shared library, expecting: iBug=20
iBug=0
The gcc version is 2.3.1. Any help is appreciated.
Fred
#! /bin/sh
# This is a shell archive. Remove anything before this line, then unpack
# it by saving it into a file and typing "sh file". To overwrite existing
# files, type "sh file -c". You can also feed this as standard input via
# unshar, or by typing "sh <file", e.g.. If this archive is complete, you
# will see the following message at the end:
# "End of shell archive."
# Contents: Makefile main.C run_test shared_bug.C
# Wrapped by fred@cv.ruu.nl on Sat Nov 21 12:53:08 1992
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test -f 'Makefile' -a "${1}" != "-c" ; then
echo shar: Will not clobber existing file \"'Makefile'\"
else
echo shar: Extracting \"'Makefile'\" \(304 characters\)
sed "s/^X//" >'Makefile' <<'END_OF_FILE'
Xall: standalone withlib test
X
Xstandalone: shared_bug.o main.o
X g++ -o $@ shared_bug.o main.o
X
Xwithlib: libbug.so main.o
X g++ -o $@ main.o -L. -lbug
X
Xlibbug.so: shared_bug.o
X ld -G -o $@ shared_bug.o
X
Xshared_bug.o: shared_bug.C
X g++ -fpic -c shared_bug.C
X
Xmain.o: main.C
X g++ -c main.C
X
Xtest:
X ./run_test
END_OF_FILE
if test 304 -ne `wc -c <'Makefile'`; then
echo shar: \"'Makefile'\" unpacked with wrong size!
fi
# end of 'Makefile'
fi
if test -f 'main.C' -a "${1}" != "-c" ; then
echo shar: Will not clobber existing file \"'main.C'\"
else
echo shar: Extracting \"'main.C'\" \(97 characters\)
sed "s/^X//" >'main.C' <<'END_OF_FILE'
X#include <iostream.h>
X
Xextern int iBug;
X
Xmain()
X{
X cerr << "iBug=" << iBug << "\n";
X return 0;
X}
END_OF_FILE
if test 97 -ne `wc -c <'main.C'`; then
echo shar: \"'main.C'\" unpacked with wrong size!
fi
# end of 'main.C'
fi
if test -f 'run_test' -a "${1}" != "-c" ; then
echo shar: Will not clobber existing file \"'run_test'\"
else
echo shar: Extracting \"'run_test'\" \(281 characters\)
sed "s/^X//" >'run_test' <<'END_OF_FILE'
X#! /bin/sh
X#
X
X#
X# Let the system know where to find the shared libary.
X#
XLD_LIBRARY_PATH=.
Xexport LD_LIBRARY_PATH
X
X#
X# Run the tests
Xecho Running the standalone version, expecting: iBug=20
X./standalone
Xecho Running the version with the shared library, expecting: iBug=20
X./withlib
END_OF_FILE
if test 281 -ne `wc -c <'run_test'`; then
echo shar: \"'run_test'\" unpacked with wrong size!
fi
chmod +x 'run_test'
# end of 'run_test'
fi
if test -f 'shared_bug.C' -a "${1}" != "-c" ; then
echo shar: Will not clobber existing file \"'shared_bug.C'\"
else
echo shar: Extracting \"'shared_bug.C'\" \(13 characters\)
sed "s/^X//" >'shared_bug.C' <<'END_OF_FILE'
Xint iBug=20;
END_OF_FILE
if test 13 -ne `wc -c <'shared_bug.C'`; then
echo shar: \"'shared_bug.C'\" unpacked with wrong size!
fi
# end of 'shared_bug.C'
fi
echo shar: End of shell archive.
exit 0
Technical Details
Message-ID:
<1992Nov21.121134.2511@cv.ruu.nl>Path:
arc1-ca.pugleaf.net!sparky!uunet!usc!snorkelwacker.mit.edu!ai-lab!prep.ai.mit.edu!gnulistsShow Full Headers
Complete Article Headers
Newsgroups: gnu.g++.bug,comp.unix.sysv386 From: fred@cv.ruu.nl (Fred Appelman) Subject: g++ and shared libraries Summary: Problems with shared libaries, gcc and global variables. Message-ID: <1992Nov21.121134.2511@cv.ruu.nl> Date: Sat, 21 Nov 1992 12:11:34 GMT Organization: University of Utrecht, 3D Computer Vision Research Group Keywords: shared, global Apparently-To: gnu-g++-bug@nluug.nl Distribution: world Approved: info-gnu@prep.ai.mit.edu Path: arc1-ca.pugleaf.net!sparky!uunet!usc!snorkelwacker.mit.edu!ai-lab!prep.ai.mit.edu!gnulists Followup-To: gnu.g++.bug,comp.unix.sysv386 Lines: 136