c - I cannot run a ELF-format program .The shell tells me no such file or directory -
my environment ubuntu 14 32bits.
write 3 c files called main.c,foo.c,and bar.c respectively.
codes simple.
first source code main.c
#include<stdio.h> extern void foo(); int main(){ foo(); return 0; }
the second source code foo.c
#include<stdio.h> void foo(){ printf("hi,i foo."); bar(); }
the last 1 bar.c
#include<stdio.h> void bar(){ printf("hi,i bar."); }
all files above put same folder called test.
(its absolute path /home/jack/desktop/test)
issue commands :
$ gcc -fpic -shared -wl,-soname,libbar.so.1 -o libbar.so.1.0.0 bar.c $ ln -s libbar.so.1.0.0 libbar.so $ gcc -fpic -shared -wl,-soname,libfoo.so.1 -o libfoo.so.1.0.0 foo.c -lbar -l. $ ln -s libfoo.so.1.0.0 libfoo.so $ gcc -c main.c $ ld -rpath /home/jack/desktop/test -e main -o main main.o -l. -lfoo -lbar
then run executable file called main.
$./main
but shell return string below
bash: ./main: no such file or directory.
but main file exists in current directory.
why?
normally should not invoke ld
directly. instead should use gcc
link. gcc
passes special options linker.
if modify script use correct sonames , use gcc
link, works. did this:
gcc -fpic -shared -wl,-soname,libbar.so.1.0.0 -o libbar.so.1.0.0 bar.c ln -s libbar.so.1.0.0 libbar.so gcc -fpic -shared -wl,-soname,libfoo.so.1.0.0 -o libfoo.so.1.0.0 foo.c -lbar -l. ln -s libfoo.so.1.0.0 libfoo.so gcc -c main.c gcc -wl,-rpath,$(pwd) -o main main.o -l$(pwd) -lfoo -lbar
using ld
directly unusual situations. don't it. said, offhand not know wrong approach. think -e
wrong here -- don't want invoke main directly, _start
other things. looking @ output of ldd
shows important differences between 2 approaches. invoking link using gcc -v
show bit of happening behind scenes, in case want understand more.
Comments
Post a Comment