Why we need the presence of the shared library during the compile time of my executable? My reasoning is that since shared library is not included into my executable and is loaded during the runtime, it is not supposed to be needed during compile time. Or Am I missing something?
#include<stdio.h>
int addNumbers(int, int); //prototype should be enough, no?
int main(int argc, char* argv[]){
int sum = addNumbers(1,2);
printf("sum is %d\n", sum);
return 0;
}
I had the libfoo.so
in my current dir but I changed its name to libfar.so
to find that shared lib is needed at compile or it doesn't compile.
gcc -o main main.c -L. -lfoo
gives main.c:(.text+0x28): undefiend reference to 'addNumber'
I think it should be enough to only have the name of the shared library. The shared library itself is not needed since it is found in the LD_LIBRARY_PATH and loaded dynamically at runtime. Is there something else needed other than the name of the shared lib?
gcc
line is not only compiling but also linking. – Overbearinggcc -c -o main.o main.c
. – Explain-undefined dynamic_lookup
will tell the compiler to assume any undefined symbols will be resolved when dynamic linking happens – Fettergcc
uses-l
options. – Insultdlopen
,dlsym
, etc, then you don't need the library until you run it. But when you use the linker, it looks up things like symbol versions and the real library name. I.E. you link tolibfoo
but then the linker really links you tolibfoo.so.4
– Mcgeeld
. It is supported byld
on macOS. See manpagez.com/man/1/ld – Selena