summaryrefslogtreecommitdiffstats
path: root/doc/html/ksquirrel-libs-olibs1.html
blob: 1965e7d25e804de2aaf49cc6f15718a4f322c0a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
    <title>KSquirrel: development</title>

    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <meta name='Author' content='Baryshev Dmitry/Krasu'>
		    
    <link rel="stylesheet" href="styles.css" type="text/css">
</head>
<body>

<ul>
<li>First, write regular C program (without main()):
<br><br>
<table cellpadding="2" cellspacing="2" width="70%" align="center">
  <tbody>
  <tr>
  <td valign="top" bgcolor="#CCCCCC">
									   
  <pre>
  const char* fmt_info()
  {
       return "It is really cool format!";
  }
  </pre>
  </td>
  </tr>
  </tbody>
</table>
<br><br>
<li>Compile it
<br><br>
<table cellpadding="2" cellspacing="2" width="70%" align="center">
  <tbody>
  <tr>
  <td valign="top" bgcolor="#CCCCCC">

  <pre>
       # gcc -O2 -fPIC -c module.c
       # gcc -shared -o module.so module.o
  </pre>
  </td>
  </tr>
  </tbody>
</table>
<br><br>
<li>Let's write a simple test
<br><br>
<table cellpadding="2" cellspacing="2" width="70%" align="center">
  <tbody>
  <tr>
  <td valign="top" bgcolor="#CCCCCC">

  <pre>
  #include &#60;unistd.h&#62;
  #include &#60;string.h&#62;
  #include &#60;errno.h&#62;
  #include &#60;dlfcn.h&#62;

  #define PATH_LENGTH 256
      
  int main(int argc, char * argv[])
  {
      char path[PATH_LENGTH], *msg = NULL;
      const char* (*fmt)();
      void *module;
      
      getcwd(path, PATH_LENGTH);
      strcat(path, "/");
      strcat(path, "module.so");

      /* Load module */
      module = dlopen(path, RTLD_NOW);
      
      /* Error ! */
      if(!module)
      {
          msg = dlerror();

          if(msg != NULL)
	  {
              dlclose(module);
              exit(1);
          }
      }

      /* Try to resolve function "fmt_info()" */
      fmt = dlsym(module, "fmt_info");

      msg = dlerror();

      if(msg != NULL)
      {
          perror(msg);
          dlclose(module);
          exit(1);
      }
      
      /* call fmt_info() through a pointer*/
      printf("%s\n", fmt());

      /* close module */
      if(dlclose(module))
      {
          perror("error");
          exit(1);
      }
      
      return 0;
  }
  
  # gcc -o test main.c -ldl
  # ./test
  It is really cool format!
  #

  </pre>
  </td>
  </tr>
  </tbody>
</table>
<br><br>
<li>That's all! :) Our test program has just loaded <u>module.so</u> and called <u>fmt_info()</u>, located in it. It is very simple, isn't ?
<br><br>
</ul>

</body>
</html>