[Cmake] command to build dynamic lib changed causing my VXL b uild to brea k

Wheeler, Fred (CRD) wheeler at crd . ge . com
Sun, 13 Jan 2002 10:01:07 -0500

Brad, Peter,

Thanks for your replies.  Brad's idea that I might have a mixed batch of .o files is a good one, but
I guarded against that.  I can demo the problem I am seeing with the commands below.  I'm finding
some more strange behavior described in the comments that start with "!!!". Note that optimization
(whether -O2 is used or not) affects this, and that the problem only happens when I use both objects
in the demo below.

Brad noted that the compiler should know whether the object file it is creating will go in a shared
or non-shared library.  Makes sense to me, but it looks to me like cmake started using "-shared
-nostdlib" for creating the shared lib, but did not change the command line for creating object
files.  Right?

# demo
cd /tmp

# !!! the problem does not occur if either of these object files is removed from the library

# build vcl_ostream_iterator+int-.o
c++ -g -O2 -Wall -Dvcl_EXPORTS -fPIC -I$VXLSRC/vcl -I/no_backup/wheeler/prj/sr/vxl_bld/vcl
-I/no_backup/wheeler/prj/sr/vxl_bld/vxl -I$VXLSRC/vcl -DVXL_WARN_DEPRECATED
-DVXL_WARN_DEPRECATED_ONCE -c $VXLSRC/vcl/Templates/vcl_ostream_iterator+int-.cxx -o

# build vcl_vector+bool-.o
# !!! if I remove the -O2 option - no bus error
c++ -g -O2 -Wall -Dvcl_EXPORTS -fPIC -I$VXLSRC/vcl -I/no_backup/wheeler/prj/sr/vxl_bld/vcl
-I/no_backup/wheeler/prj/sr/vxl_bld/vxl -I$VXLSRC/vcl -DVXL_WARN_DEPRECATED
-DVXL_WARN_DEPRECATED_ONCE -c $VXLSRC/vcl/Templates/vcl_vector+bool-.cxx -o vcl_vector+bool-.o

# a minimal main
cat<<EOF >| foo.cxx
int main() { return 0; }

c++ -g -O2 -c foo.cxx -o foo.o

# with this option there is no bus error
# with this option the exec will produce a bus error
OPT="-shared -nostdlib"

# build vcl library with only two objects
c++ -v $OPT -g -O2 -o /tmp/libvcl.so \
  vcl_ostream_iterator+int-.o \

# link
c++ foo.o -L/tmp -lvcl -Wl,-R,/tmp: -o foo

# run, bus error will occur here is "-shared -nostdlib" was used

# if there was a bus error, gdb yeilds garbage (otherwise it works)
138% gdb foo
GNU gdb 5.1
(gdb) break main
Breakpoint 1 at 0x10700: file foo.cxx, line 1.
(gdb) run
Starting program: /tmp/foo 

Program received signal SIGSEGV, Segmentation fault.
0xff3c9e84 in ?? ()
(gdb) where
#0  0xff3c9e84 in ?? ()
#1  0xff3c3bdc in ?? ()
#2  0xff3b2ea0 in ?? ()
#3  0xff3ba8cc in ?? ()
#4  0xff3c4da0 in ?? ()
#5  0xff3b2958 in ?? ()

Fred Wheeler

Fred Wheeler, GE CRD, KWC-303
Phone: 518-387-7225 (GE Internal Dialcom: 8*833-7225)
Fax:   518-387-4042 (GE Internal Dialcom: 8*833-4042)

-----Original Message-----
From: Brad King [mailto:brad . king at kitware . com]
Sent: Saturday, January 12, 2002 8:57 PM
To: Wheeler, Fred (CRD)
Cc: 'vxl-maintainers at lists . sourceforge . net'; 'cmake at public . kitware . com'
Subject: Re: [Cmake] command to build dynamic lib changed causing my VXL
build to brea k


> Sometime over the past week or two CMake started producing makefiles
> that use the "-shared -nostdlib" options instead of "-Wl,-G" to link
> objects into a shared library (*.so).  With this change all of VXL
> still builds for me, but just about all executables produced in the
> VXL tree stop with a Bus Error immediately after starting.  
> Everything build fine, but nothing runs.  Strange.
We recently switched the flags from -Wl,-G to -shared -nostdlib to solve
other problems.  The -shared flag is the correct choice..."-Wl,-G" tells
the C++ compiler to pass the "-G" option to the linker, which tells it to
create a shared library.  However, the C++ compiler then does not know
that the code it is generating is going into a shared library, so it
doesn't put in the correct static data initialization code (to call
constructors of static data, etc).  The -shared option tells the compiler
to put in the proper init code, and automatically causes it to pass the -G
option on to the linker.

Have you done a complete re-build of VXL in a clean directory?  Your
problems may be caused by some .o files or libraries being built with the
old flags, and some with the new.