EQuestionAnswers.com Computer/Electronics Questions and Answers
C, C++, VC++, COM/DCOM, DLL and more
#Login #Sign up  Facebook Twitter TGoogle+
 

#Previous question #Index of DLL Questions #Next question

Linux like Windows also supports all the linking of Windows. We shall discuss all the topics one by one briefly.

Static Linking:
Let us consider math.c file. We want to make it as a static library.
First we compile it with position independent flag on(-fPIC). This is needed for dynamic/static linking.
$cc -fPIC -c math.c

Now make a archive or static lib with the object file.
$ar rc libmath.a math.o

To use this static library in a application we need to do the following steps:

  1. compile the application code (place math.h in include folder)
    $cc -c app.c -Iinclude -o app.o
  2. Link with static library math.a
    $ld app.o libmath.a -o app
  3. Run the application
    $./app

Implicit Dynamic Linking:
Let us consider once again math.c file for dynamic linking. We want to make it as a dynamic library. First we compile it with position independent flag on(-fPIC). This is needed for dynamic/static linking.
$cc -fPIC -c math.c

Now make a shared library with the object file.
$cc -shared libmath.so math.o

To use this shared library in a application we need to do the following steps:

  1. compile the application code (place math.h in include folder)
    $cc -c app.c -Iinclude -o app.o
  2. Link with import library math.lib
    $ld app.o -lmath -o app
  3. Copy the libmath.so in lib path or current path and run the application
    $./app

Explicit Dynamic Linking:
Let us consider once again math.c file for explicit linking. The steps for creating a shared library are same as that of implicit linking.
First we compile it with position independent flag on(-fPIC). This is needed for dynamic/static linking.
$cc -fPIC -c math.c

Now make a shared library with the object file.
$cc -shared libmath.so math.o

To use this shared library in a application we need to load the library then find the function pointer address, invoke the function, and at last unload the library.
Linux provides some dynamic link library APIs to achieve this. Her are some useful frequently use APIs:

  • dlopen() - loads a dynamic link binary
  • dlsym() - returns the function pointer if found the function entry
  • dlclose() - unloads the dynamic link binary
Sample code:
#include<dlfcn.h>
typedef int (add_func) (int a, int b);

void *lib_handle = NULL;
add_func * add;
int main (int argc, char *argv[])
{
  lib_handle = (void *)dlopen("libmath.so", RTLD_LAZY); 
  if(lib_handle) 
  { 
    add = dlsym(lib_handle, "add");
    if(lib_func)
    {
      printf("1 + 2 = %d", add(1, 2));
    }
    else
    {
      printf("Function entry not found in DLL");
    }
    dlclose(lib_handle);
  }
  else
  {
    printf("Unable to open DLL");
  }
}

You have viewed 1 page out of 27. Your DLL learning is 0.00% complete. Login to check your learning progress.

 Vote 0

Similar topics related to this section

# DLL Frequently Asked Questions
# Frequently Asked Questions - DLL-files.com
# Dependency Walker Frequently Asked Questions (FAQ)
# Top 10 DLL Questions !
# DLL COM DCOM Interview Questions and Answers

* #1 webmaster Thu 27 Feb/2014 11:26:10(GMT)  Like 0 Unlike 0

Dear Users,

We are pleased to inform you that a forum/blog has been incorporated with www.mybestnotes.co.in. You are welcomed to add your comments, requests, codes, solutions and feedback to it. Please login(if already a member) or signup(for free) to avail to this facility.

Regards,
Webmaster

Your message goes here:

Name:Guest
Email:anonymous@unknown.com
My Post:*
Secutiry Code: ******  *
Preview this compose before posting this in discussion forum.
 
Note:
  1. Use [Sxx] code to show smiles. Example [S02]
  2. Use [URL ], example [URL http://www.google.com/]
  3. To display code or un formatted text use [CODE] [/CODE]. Example: [CODE] printf("Hello world"); [/CODE]
 [S01]   [S02]   [S03]   [S04]   [S05]   [S06]   [S07]   [S08] 
 [S09]   [S10]   [S11]   [S12]   [S13]   [S14]   [S15]   [S16] 
 [S17]   [S18]   [S19]   [S20]   [S21]   [S22]   [S23]   [S24] 

Note: Only members are allowed, * fields are mandatory.