Use nm to view symbols in the file.
Example code:
Cat sub1.c
-----------------------------------------------
# Include <stdlib. h>
IntPrn1(Char * Str)
{
Printf ("this is in sub1.d !! ");
Printf ("% s \ n", STR );
}
-----------------------------------------------
Compile command:
$ CC-C sub1.c (generate sub1.o)
$ Nm sub1.o
00000000 t gcc2_compiled.
U printf
00000000 TBPrn1
$ LD-g-o sub1.d sub1.o
$ Nm sub1.d
00001210 A _ dynamic
00001204 A _ global_offset_table _
00001260 A _ bss_start
00001260 A _ edata
00001260 A _ end
000001eb A _ etext
000001c8 t gcc2_compiled.
U printf
000001c8 tPrn1
Caller code:
-----------------------------------------------
// # Include <stdlib. h>
# Include <stdio. h>
# Include <dlfcn. h>
# Include <unistd. h>
# DEFINE _ main __
# Include "Debug. H"
# UNDEF _ main __
# Define def_dl_file "sub1.d"
# Define def_dl_fun "prn1"
Main (INT argc, char * argv [])
{
Void * handle;
INT (* PRN) (char * Str );
Char * error;
Char dlfilename [100], dlfiledir [100], dlfunname [50];
Memset (dlfunname, '\ 0', 50 );
Memset (dlfiledir, '\ 0', 100 );
Memset (dlfilename, '\ 0', 100 );
Getcwd (dlfiledir, 100 );
If (argc> = 2)
Sprintf (dlfilename, "% S/% s", dlfiledir, argv [1]);
Else
Sprintf (dlfilename, "% S/% s", dlfiledir, def_dl_file );
Msgstr (dlfilename );
If (argc = 3)
Strcpy (dlfunname, argv [2]);
Else
Strcpy (dlfunname, def_dl_fun );
Msgstr (dlfunname );
Handle = dlopen (dlfilename, rtld_lazy );
If (! Handle)
{
Fputs (dlerror (), stderr );
Exit (1 );
}
MSG ("after dlopen ()!! ");
PRN = dlsym (handle, dlfunname );
If (error = dlerror ())! = NULL)
{
Fputs (error, stderr );
Exit (1 );
}
MSG ("after dlsym ()!! ");
PRN ("good !! ");
MSG ("after PRN ()!! ");
Dlclose (handle );
MSG ("after dlclose ()!! ");
}
-----------------------------------------------
Compile command:
Gcc-C main. c
Gcc-O main. x main. O-LDL
-Enable dynamic link library for LDL.
Execution result:
-----------------------------------------------
: Dlfilename is string:/home/mento/coding/C/dltst/sub1.d
: Dlfunname is string: prn1
: Message: After dlopen ()!!
: Message: After dlsym ()!!
This is in sub1.d !! Good !!
: Message: After PRN ()!!
: Message: After dlclose ()!!
-----------------------------------------------