Linux/Unix dynamic link library instance

Source: Internet
Author: User

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 ()!!
-----------------------------------------------

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.