You need to force the compiler and linker to fetch the function from the library. In your current attempt, you reference a character called fun1
in your program. Because your fun1
is a char, the compiler has no reason to declare a symbol in your .o file that is a function called fun1
. As a result, function fun1
will not be fetched from the library and there is no function fun1
in your executable.
(Note that in your example, there is no char fun1
declared, so your compiler should complain. Did you turn warnings on???)
To force the compiler and linker to load the function fun1
from the library, you can write:
char *fun1(void); // prototype;
char *(*p)(void) // p is a pointer to a function with same signature as fun1...
= fun1; // and we initialize it with function 'fun1'
Note: should the compiler determine that you never use p
, it still may remove the function pointer variable and the assignment of fun1
to it, so fun1
would still not have been loaded.