Sorry to re-post, but does anyone have suggestions on how to force g++ to link against libc for 'stat' symbol instead of using the weak symbol from my shared library? Any help would be greatly appreciated. -Peter On 9/24/07, Peter McAlpine <petermca@xxxxxxxxx> wrote: > Hello, > > I have written a program and shared library, both of which use the > libc stat function. I want to build optimized and non-optimized > versions of each, and then run the optimized program against the > non-optimized library. > > The stat function is inline, and thus the optimized version of my > shared library does not actually have the stat symbol. > > My non-optimized program was compiled and linked against the > non-optimized library, and for some reason when g++ links my program > it thinks stat symbol should be found in my shared library. In fact it > should be found in libc. > > At runtime, my non-optimized program looks for the stat symbol in my > (optimized) shared library, but because the shared library has had the > (inline) stat symbol optimized out I get a runtime error: > undefined symbol: stat > > If anyone can give me some direction on how to get g++ to use the libc > stat symbol instead of my shared library's stat symbol I'd greatly > appreciate it. > > Thanks, > -Peter > > PS - Below is the code/script I'm using to show this undesired behaviour. > ################# t.sh (compile/run) ###################### > #! /bin/bash -v > # build non-O libfoo > g++ -O0 -c -o foo.o foo.cpp > g++ -O0 -shared -o libfoo_no.so foo.o > > # build O libfoo > g++ -O -c -o foo.o foo.cpp > g++ -O -shared -o libfoo_o.so foo.o > > # build t against no > rm -f libfoo.so > ln -s libfoo_no.so libfoo.so > gcc -O0 -c -o t.o t.c > > # G++ > g++ -O0 -o t_no -L. t.o -lfoo > > echo > # run t_no with o > rm -f libfoo.so > ln -s libfoo_o.so libfoo.so > ./t_no > > ############## foo.cpp ################## > #include <sys/types.h> > #include <sys/stat.h> > #include <unistd.h> > #include <stdio.h> > #include "foo.h" > > int thefile(const char *path) { > struct stat statbuf; > int rc = 0; > printf("in thefile1: %s\n", path); > rc = stat(path, &statbuf); > printf("in thefile2: %s\n", path); > return rc; > } > > ############# foo.h ############ > int thefile(const char *path); > > ############# t.c ############# > #include <stdio.h> > #include <sys/stat.h> > #include <sys/types.h> > #include <unistd.h> > #include "foo.h" > > ############### int main(int argc, char *argv) ############## > { > struct stat attr; > printf("in main\n"); > stat("/tmp/aoeu", &attr); > printf("in main2\n"); > > return 0; > } > > ############ Output from compile/run ################## > ./t.sh > #! /bin/bash -v > # build non-O libfoo > g++ -O0 -c -o foo.o foo.cpp > g++ -O0 -shared -o libfoo_no.so foo.o > > # build O libfoo > g++ -O -c -o foo.o foo.cpp > g++ -O -shared -o libfoo_o.so foo.o > > # build t against no > rm -f libfoo.so > ln -s libfoo_no.so libfoo.so > gcc -O0 -c -o t.o t.c > > # G++ > g++ -O0 -o t_no -L. t.o -lfoo > > echo > > # run t_no with o > rm -f libfoo.so > ln -s libfoo_o.so libfoo.so > ./t_no > in main > ./t_no: symbol lookup error: ./t_no: undefined symbol: stat >