summaryrefslogtreecommitdiff
path: root/docs/libraries/LibraryArchives-StaticAndDynamic.html
blob: e458e4f242ee109815f8baaaaf953ceda62163a0 (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Linux Tutorial - Static, Shared Dynamic and Loadable Linux Libraries</title>
  <link rel="stylesheet" href="yolinux.css" type="text/css" />
  <link rel=”canonical” href=”http://www.yolinux.com/TUTORIALS/LibraryArchives-StaticAndDynamic.html" />
  <meta name="keywords" content="static libraries,shared objects,dynamic libraries,loadable libraries,linux,development,ar, ranlib" />
  <meta name="description" content="YoLinux Tutorials: Static, Shared Dynamic and Loadable Linux Libraries. Linux Information Portal includes informative tutorials and links to many Linux sites. The YoLinux portal covers topics from desktop to servers and from developers to users" />
  <meta name="author" content="Greg Ippolito" />
</head>

<body bgcolor="#cccccc">
<p>
</p>
<table cellpadding="4" width="100%">
  <tbody>
    <tr>
      <td valign="top">
      <a href="http://www.yolinux.com/"><img src="images/YoLinux_Tutorial_logo.png" alt="Yolinux.com Tutorial" border="none" /></a>
      </td>
      <td valign="top">
      <h1>Static, Shared Dynamic and Loadable Linux Libraries</h1>
      <p>
This tutorial discusses the philosophy behind libraries and the creation and 
use of C/C++ library "shared components" and "plug-ins". 
The various technologies and methodologies used and insight to their appropriate application, is also discussed.
In this tutorial, all libraries are created using the GNU Linux compiler.
      </p>
      <p>
      </p>
      <div align="right">
<!-- ValueClick Media 468x60 and 728x90 Banner CODE for yo-linux.com -->
<script language="javascript" src="http://media.fastclick.net/w/get.media?sid=51962&m=1&tp=5&d=j&t=s"></script>
<noscript><a href="http://media.fastclick.net/w/click.here?sid=51962&m=1&c=1" target="_top">
<img src="http://media.fastclick.net/w/get.media?sid=51962&m=1&tp=5&d=s&c=1"
width=728 height=90 border=1></a></noscript>
<!-- ValueClick Media 468x60 and 728x90 Banner CODE for yo-linux.com -->

      </div>

      </td>
    </tr>
  </tbody>
</table>

<p>
</p>
<!-- Menu bar -->
<table bgcolor="#000000" width="100%">
<tbody>
<tr bgcolor="#000000">
<td colspan="3">
<form method="post" action="http://www.yolinux.com/cgi-bin/htsearch">
<input type="hidden" name="method" value="all" /> 
<input type="hidden" name="format" value="long" /> 
<input type="hidden" name="sort" value="score" /> 
<input type="hidden" name="config" value="htdig-yolinux" /> 
<input type="hidden" name="restrict" value="" /> 
<input type="hidden" name="exclude" value="" /> 
<input type="text" size="20" maxlength="80" name="words"> 
<input type="submit" value="Search">
</td>
<td align="right">
<font color="#f7bc33" size="-1">
<b>
&nbsp;|&nbsp;
<a href="/" class="menubar" title="YoLinux Home Page">Home Page</a>&nbsp;|&nbsp;
<a href="/TUTORIALS/" class="menubar" title="YoLinux Web Site Tutorials Index">Linux Tutorials</a>&nbsp;|&nbsp;
<a href="/YoLinux-Terms.html" class="menubar" title="YoLinux.com terms of use" rel="nofollow">Terms</a>&nbsp;|&nbsp;
<a href="/privacy.html" class="menubar" title="YoLinux.com privacy policy" rel="nofollow">Privacy Policy</a>&nbsp;|&nbsp;
<a href="/YoLinux-Advertising.html" class="menubar" title="Advertising rates for YoLinux.com">Advertising</a>&nbsp;|&nbsp;
<a href="/YoLinuxEmailForm.html" class="menubar" title="Send us an email" rel="nofollow">Contact</a> &nbsp;|&nbsp;
</b>
</font>
</form>
</td>
</tr>
</tbody>
</table>
<!-- End of menu bar -->

<table bgcolor="#000000" border="0" cellpadding="4" cellspacing="1" width="100%">
  <tbody>
    <tr bgcolor="#cccccc">
      <td bgcolor="#c0c0c0" valign="top" width="160">
      <p>
      <font size="-1">
      <b>Related YoLinux Tutorials:</b>
      </font></p>
      <p>
      <font size="-1">&deg;<a href="LinuxTutorialC++.html">C++ Info, links</a>
      </font></p>
      <p>
      <font size="-1">&deg;<a href="LinuxTutorialC++StringClass.html">C++ String Class</a>
      </font></p>
      <p>
      <font size="-1">&deg;<a href="LinuxTutorialC++STL.html">C++ STL vector, list</a>
      </font></p>
      <p>
      <font size="-1">&deg;<a href="ForkExecProcesses.html">Fork and exec</a>
      </font></p>
      <p>
      <font size="-1">&deg;<a href="Sockets.html">Sockets programming</a>
      </font></p>
      <p>
      <font size="-1">&deg;<a href="LinuxTutorialXemacs.html">Emacs and C/C++</a>
      </font></p>
      <p>
      <font size="-1">&deg;<a href="LinuxTutorialAdvanced_vi.html">Advanced VI</a>
      </font></p>
      <p>
      <font size="-1">&deg;<a href="LinuxTutorialC++CGI.html">CGI in C++</a>
      </font></p>
      <p>
      <font size="-1">&deg;<a href="ClearcaseCommands.html">Clearcase Commands</a>
      </font></p>
      <p>
      <font size="-1">&deg;<a href="MicrosoftVisualC++Tips.html">MS/Visual C++ Practices</a>
      </font></p>
      <p>
      <font size="-1">&deg;<a href="C++MemoryCorruptionAndMemoryLeaks.html">C++ Memory corruption and leaks</a>
      </font></p>
      <p>
      <font size="-1">&deg;<a href="index.html">YoLinux Tutorials Index</a>
      </font>
      </p>

<hr />
<p>
<!-- BEGIN RICH-MEDIA CODE -->
<script type="text/javascript"><!--
google_ad_client = "pub-8567479315106986";
/* 160x600, created 10/19/09 */
google_ad_slot = "2598351238";
google_ad_width = 160;
google_ad_height = 600;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</p>
<hr />
<p>
<!-- Ad links -->
<script type="text/javascript"><!-- 
google_ad_client = "pub-8567479315106986";
google_ad_width = 160;
google_ad_height = 90;
google_ad_format = "160x90_0ads_al_s";
google_ad_channel ="";
google_color_border = "FFFFFF";
google_color_bg = "C0C0C0";
google_color_link = "0000FF";
google_color_text = "000000";
google_color_url = "008000";
//-->
</script>
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</p>
<!-- END CODE -->
<hr />
<p>
<a href="http://yolinux.tradepub.com/">Free Information Technology Magazines and Document Downloads<br /><img src="images/TradePubMagazines.gif" border="0" alt="TradePub link image" /></a>
</p>
<hr />

      <p>
      <a href="http://yolinux.tradepub.com/?pt=cat&amp;page=Infosoft">Free Information Technology <b>Software and Development</b> Magazine Subscriptions and Document Downloads</a>
      </p>
      <p>
      </p>
      <hr />
      <p>
      </p>
      </td>

      <td>
      <table border="0" cellpadding="2" cellspacing="0" width="100%">
        <tbody>
          <tr bgcolor="#ffcc33">
            <td><b><big>Why libraries are used: </big></b></td>
          </tr>
        </tbody>
      </table>

      <p> This methodology, also known as "shared components" or
"archive libraries", groups together multiple compiled object code
files into a single file known as a library. Typically C functions/C++
classes and methods which can be shared by more than one application
are broken out of the application's source code, compiled and bundled
into a library.
The C standard libraries and C++ STL are examples of shared components
which can be linked with your code. The benefit is that each and every
object file need not be stated when linking because the developer can
reference the individual library. This simplifies the
multiple use and sharing of software components between applications.
It also allows application vendors a way to simply release an API to
interface with an application.
Components which are large can be created for dynamic use, thus the
library remain separate from the executable reducing it's size and thus
disk space used.
The library components are then called by various applications for use
when needed. </p>
      <p> 
      </p>
      <hr />
      <table border="0" cellpadding="2" cellspacing="0" width="100%">
        <tbody>
          <tr bgcolor="#ffcc33">
            <td><b><big>Linux Library Types: </big></b></td>
          </tr>
        </tbody>
      </table>

      <p>
There are two Linux C/C++ library types which can be created:
      </p>
      <ol>
        <li>Static libraries (.a): Library of object code which is linked with, and becomes part of the application.
        </li>
        <li>Dynamically linked shared object libraries (.so): There is only one form of this library but it can be used in two ways.
            <ol>
            <li>Dynamically linked at run time but statically aware. The
libraries must be available during compile/link phase. The shared
objects are not included into the executable component but are tied to
the execution. </li>
            <li>Dynamically loaded/unloaded and linked during execution
(i.e. browser plug-in) using the dynamic linking loader system
functions.  </li>
            </ol>
        </li>
      </ol>

      <p> 
      </p>
      <h4>Library naming conventions:</h4>
Libraries are typically names with the prefix "lib". This is true for
all the C standard libraries. When linking, the command line reference
to the library will not contain the library prefix or suffix.
      <p>
Thus the following link command: <tt>gcc src-file.c -lm -lpthread</tt>
      <br />
The libraries referenced in this example for inclusion during linking 
are the math library and the thread library. They are found in 
      <tt>/usr/lib/lib<b>m</b>.a</tt> and <tt>/usr/lib/lib<b>pthread</b>.a</tt>. 
      </p>
      <p> 
      </p>
      <hr />
      <table border="0" cellpadding="2" cellspacing="0" width="100%">

        <tbody>
          <tr bgcolor="#ffcc33">
            <td><b><big>Static Libraries: (.a)</big></b></td>
          </tr>

        </tbody>
      </table>

      <p> 
How to generate a library: 
    </p>
      <ul>

    <li>Compile: <tt>cc -Wall -c ctest1.c ctest2.c</tt>
        <br />

        Compiler options:
        
          <ul>

        <li>-Wall: include warnings. See man page for warnings specified.
        </li>
          </ul>

    </li>
        <li>Create library "libctest.a": <tt>ar -cvq libctest.a ctest1.o ctest2.o</tt>
    </li>
        <li>List files in library: <tt>ar -t libctest.a</tt>
    </li>
        <li>Linking with the library: 
        
          <ul>

        <li><tt>cc -o <i>executable-name</i> prog.c libctest.a</tt>
        </li>
            <li><tt>cc -o <i>executable-name</i> prog.c -L/path/to/library-directory -lctest</tt>
        </li>
          </ul>

    </li>
        <li>Example files: 
        
          <ul>

        <li>ctest1.c
            
              <table bgcolor="#000000" border="0" cellpadding="4" cellspacing="1" width="100%">

            <tbody>
                  <tr bgcolor="#c0c0c0">
                    <td>
            
                    <pre>void ctest1(int *i)<br />{<br />   *i=5;<br />}<br />            </pre>

            </td>
                  </tr>
                </tbody>
              </table>

        </li>
            <li>ctest2.c
            
              <table bgcolor="#000000" border="0" cellpadding="4" cellspacing="1" width="100%">

            <tbody>
                  <tr bgcolor="#c0c0c0">
                    <td>
            
                    <pre>void ctest2(int *i)<br />{<br />   *i=100;<br />}<br />            </pre>

            </td>
                  </tr>
                </tbody>
              </table>

        </li>
            <li>prog.c
            
              <table bgcolor="#000000" border="0" cellpadding="4" cellspacing="1" width="100%">

            <tbody>
                  <tr bgcolor="#c0c0c0">
                    <td>
            
                    <pre>#include &lt;stdio.h&gt;<br />void ctest1(int *);<br />void ctest2(int *);<br /><br />int main()<br />{<br />   int x;<br />   ctest1(&amp;x);<br />   printf("Valx=%d\n",x);<br /><br />   return 0;<br />}<br />            </pre>

            </td>
                  </tr>
                </tbody>
              </table>

        </li>
          </ul>

    </li>
      </ul>

Historical note: 
After creating the library it was once necessary to run the command: <tt>ranlib ctest.a</tt>.
This created a symbol table within the archive. Ranlib is now embedded into the "ar"
command.
      <p>
Note for MS/Windows developers: The Linux/Unix ".a" library is conceptually
the same as the Visual C++ static ".lib" libraries.
      </p>
      <p>
      </p>
      <hr />
      <table border="0" cellpadding="2" cellspacing="0" width="100%">

        <tbody>
          <tr bgcolor="#ffcc33">
            <td><b><big>Dynamically Linked "Shared Object" Libraries: (.so)</big></b></td>
          </tr>

        </tbody>
      </table>

      <p>
How to generate a shared object: (Dynamically linked object library file.)
Note that this is a two step process.
      </p>
      <ol>

        <li>Create object code
        </li>
        <li>Create library
        </li>
        <li>Optional: create default version using a symbolic link.
        </li>
      </ol>

      <b>Library creation example:</b>
    
    <dl>
    <dd>
    
    <pre>    gcc -Wall -fPIC -c *.c
    gcc -shared -Wl,-soname,libctest.so.1 -o libctest.so.1.0   *.o
    mv libctest.so.1.0 /opt/lib
    ln -sf /opt/lib/libctest.so.1.0 /opt/lib/libctest.so
    ln -sf /opt/lib/libctest.so.1.0 /opt/lib/libctest.so.1
          </pre>

    This creates the library <tt>libctest.so.1.0</tt> and symbolic links to it.
    
    <p>
    Compiler options:
    </p>
    <ul>
    <li><tt>-Wall</tt>: include warnings. See man page for warnings specified.
    </li>
            <li><tt>-fPIC</tt>: Compiler directive to output position independent code,  a characteristic required by shared libraries. Also see "-fpic".
    </li>
            <li><tt>-shared</tt>: Produce  a  shared  object which can then be linked with other objects to form an executable.
    </li>
            <li><tt>-W1</tt>: Pass options to linker.
        <br />
In this example the options to be passed on to the linker are:
        "<tt>-soname libctest.so.1</tt>".
        The name passed with the "-o" option is passed to <tt>gcc</tt>.
    </li>
            <li>Option <tt>-o</tt>: Output of operation. In this case the name of the shared object to be output will be "<tt>libctest.so.1.0</tt>"
    </li>
          </ul>

    
          <p>
    Library Links:
    </p>
          <ul>

    <li>The link to <tt>/opt/lib/libctest.so</tt> allows the naming convention for the compile flag <tt>-lctest</tt> to work.
    </li>
            <li>The link to <tt>/opt/lib/libctest.so.1</tt> allows the run time binding to work. See dependency below.
    </li>
          </ul>

    </dd>
      </dl>

    
      <p>
      <b>Compile main program and link with shared object library:</b>
      </p>
      <dl>
        <dd>
Compiling for runtime linking with a dynamically linked <tt>libctest.so.1.0</tt>:
          <pre>    gcc -Wall -I<i>/path/to/include-files</i> -L<i>/path/to/libraries</i> prog.c -lctest -o prog
Use:
    gcc -Wall -L/opt/lib prog.c -lctest -o prog
      </pre>

Where the name of the library is <tt>libctest.so</tt>. (This is why you must create the symbolic links or you will get the error "/usr/bin/ld: cannot find -lctest".)
    <br />

The libraries will NOT be included in the executable but will be dynamically linked during runtime execution.
        </dd>
      </dl>

      <p>
      <b>List Dependencies:</b>
      </p>
      <p>
The shared library dependencies of the executable can be listed with the
command: <tt><a href="http://man.yolinux.com/cgi-bin/man2html?cgi_command=ldd"><b>ldd</b></a> <i>name-of-executable</i></tt>
      </p>
      <p>
    </p>
      <dl>
        <dd>
    Example: <tt>ldd prog</tt>
    
          <pre>        libctest.so.1 =&gt; /opt/lib/libctest.so.1 (0x00002aaaaaaac000)
        libc.so.6 =&gt; /lib64/tls/libc.so.6 (0x0000003aa4e00000)
        /lib64/ld-linux-x86-64.so.2 (0x0000003aa4c00000)
    </pre>

    </dd>
      </dl>

      <p>
      <b>Run Program:</b>
   </p>
      <ul>

   <li>Set path: <tt>export LD_LIBRARY_PATH=/opt/lib:$LD_LIBRARY_PATH</tt>
   </li>
        <li>Run: <tt>prog</tt>
   </li>
      </ul>

      <p>
      <b>Man Pages:</b>
    </p>
      <ul>

    <li><a href="http://man.yolinux.com/cgi-bin/man2html?cgi_command=gcc">gcc</a> - GNU C compiler
    </li>
        <li><a href="http://man.yolinux.com/cgi-bin/man2html?cgi_command=ld">ld</a> - The GNU Linker
    </li>
        <li><a href="http://man.yolinux.com/cgi-bin/man2html?cgi_command=ldd">ldd</a> - List dependencies
    </li>
      </ul>

      <p>
      <b>Links:</b>
      </p>
      <ul>

        <li><a href="http://www.tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html">LDP: Shared libraries</a>
        </li>
      </ul>

      <p>
      </p>
      <hr />
      <table border="0" cellpadding="2" cellspacing="0" width="100%">

        <tbody>
          <tr bgcolor="#c0c0c0">
            <td><b><big>Library Path:</big></b></td>
          </tr>

        </tbody>
      </table>

      <p>In order for an executable to find the required libraries to
link with during run time, one must configure the system so that the
libraries can be found.
Methods available: (Do at least one of the following)
</p>
      <ol>

        <li>Add library directories to be included during dynamic linking to the file
          <tt>/etc/ld.so.conf</tt>
          <p>
Sample: <tt>/etc/ld.so.conf</tt>
          </p>
          <dl>
            <dd>
              <table bgcolor="#000000" border="0" cellpadding="4" cellspacing="1" width="100%">
                <tbody>
                  <tr bgcolor="#c0c0c0">
                    <td>
                    <pre>/usr/X11R6/lib
/usr/lib
...
..
/usr/lib/sane
/usr/lib/mysql
/opt/lib
                    </pre>
                    </td>
                  </tr>
                </tbody>
              </table>

            </dd>
          </dl>
Add the library path to this file and then execute the command (as root)
          <tt>ldconfig</tt> to configure the linker run-time bindings. 
          <br />

You can use the "<b>-f <i>file-name</i></b>" flag to reference another 
configuration file if you are developing for different environments.
          <br />

See man page for command 
          <a href="http://man.yolinux.com/cgi-bin/man2html?cgi_command=ldconfig">ldconfig</a>.
          <p>
          <b>OR</b>
          </p>
          <p>
          </p>
        </li>
        <li>Add specified directory to library cache: (as root)
            <br />
          <tt>ldconfig -n <i>/opt/lib</i></tt>
            <br />
        Where <tt>/opt/lib</tt> is the directory containing your library <tt>libctest.so</tt>
            <br />
(When developing and just adding your current directory: <tt>ldconfig -n .</tt> Link with -L.)
          <p>This will NOT permanently configure the system to include this directory. The information will be lost upon system reboot.
          </p>
          <p>
          <b>OR</b>
          </p>
          <p>
          </p>
        </li>
        <li>Specify the environment variable <tt>LD_LIBRARY_PATH</tt> to point to the directory paths containing the shared object library.
This will specify to the run time loader that the library paths will be used during execution to resolve dependencies.
    <br />
(Linux/Solaris: <tt>LD_LIBRARY_PATH</tt>, SGI: <tt>LD_LIBRARYN32_PATH</tt>, AIX: <tt>LIBPATH</tt>, Mac OS X: <tt>DYLD_LIBRARY_PATH</tt>, HP-UX: <tt>SHLIB_PATH</tt>)
          <p>
Example (bash shell): <tt>export LD_LIBRARY_PATH=/opt/lib:$LD_LIBRARY_PATH</tt>
or add to your <tt>~/.bashrc</tt> file:
          </p>
          <dl>
            <dd>
              <table bgcolor="#000000" border="0" cellpadding="4" cellspacing="1" width="100%">

                <tbody>
                  <tr bgcolor="#c0c0c0">
                    <td>
                    <pre>...
if [ -d /opt/lib ];
then
   LD_LIBRARY_PATH=/opt/lib:$LD_LIBRARY_PATH
fi

...

export LD_LIBRARY_PATH
      </pre>

                    </td>
                  </tr>
                </tbody>
              </table>

            </dd>
          </dl>

          <br />

This instructs the run time loader to look in the path described by the environment variable <tt>LD_LIBRARY_PATH</tt>, to resolve shared libraries. This will include the path <tt>/opt/lib</tt>.
        </li>
      </ol>

      <p>
Library paths used should conform to the "<a href="http://www.linuxbase.org/">Linux Standard Base</a>" directory structure.
      </p>
      <p>
      </p>
      <hr />
      <table border="0" cellpadding="2" cellspacing="0" width="100%">

        <tbody>
          <tr bgcolor="#c0c0c0">
            <td><b><big>Library Info:</big></b></td>
          </tr>

        </tbody>
      </table>

      <p>
The command "nm" lists symbols contained in the object file or shared library.
      </p>
      <p>
Use the command <tt>nm -D libctest.so.1.0</tt>
      <br />
(or <tt>nm --dynamic libctest.so.1.0</tt>)
      </p>
      <dl>
        <dd>
          <pre>0000000000100988 A __bss_start
000000000000068c T ctest1
00000000000006a0 T ctest2
                 w __cxa_finalize
00000000001007b0 A _DYNAMIC
0000000000100988 A _edata
0000000000100990 A _end
00000000000006f8 T _fini
0000000000100958 A _GLOBAL_OFFSET_TABLE_
                 w __gmon_start__
00000000000005b0 T _init
                 w _Jv_RegisterClasses
      </pre>

        </dd>
      </dl>

      <a href="http://man.yolinux.com/cgi-bin/man2html?cgi_command=nm">Man page for nm</a>
      <dl>
        <dd>
          <table border="1">

            <tbody>
              <tr bgcolor="#c0c0c0">
                <th>Symbol Type</th>
                <th>Description</th>
              </tr>

              <tr>
                <td>A</td>
                <td>The  symbol's  value  is absolute, and will not be changed by further linking.</td>
              </tr>

              <tr>
                <td>B</td>
                <td>Un-initialized data section</td>
              </tr>

              <tr>
                <td>D</td>
                <td>Initialized data section</td>
              </tr>

              <tr>
                <td>T</td>
                <td>Normal code section</td>
              </tr>

              <tr>
                <td>U</td>
                <td>Undefined symbol used but not defined. Dependency on another library.</td>
              </tr>

              <tr>
                <td>W</td>
                <td>Doubly defined symbol. If found, allow definition in another library to resolve dependency.</td>
              </tr>

            </tbody>
          </table>

        </dd>
      </dl>

      <p>
Also see:
      <a href="http://man.yolinux.com/cgi-bin/man2html?cgi_command=objdump">objdump</a> man page
      </p>
      <p>
      </p>
      <hr />
      <table border="0" cellpadding="2" cellspacing="0" width="100%">

        <tbody>
          <tr bgcolor="#ffcc33">
            <td><b><big>Library Versions:</big></b></td>
          </tr>

        </tbody>
      </table>

      <p>Library versions should be specified for shared objects if the
function interfaces are expected to change (C++ public/protected class
definitions), more or fewer functions are included in the library, the
function prototype changes (return data type (int, const int, ...) or
argument list changes) or data type changes (object definitions: class
data members, inheritance, virtual functions, ...). </p>
      <p>The library version can be specified when the shared object
library is created.
If the library is expected to be updated, then a library version should
be specified. This is especially important for shared object libraries
which are dynamically linked. This also avoids the Microsoft "DLL hell"
problem of conflicting libraries where a system upgrade which changes a
standard library breaks an older application expecting an older version
of the the shared object function. </p>
      <p>Versioning occurs with the GNU C/C++ libraries as well. This
often make binaries compiled with one version of the GNU tools
incompatible with binaries compiled with other versions unless those
versions also reside on the system. Multiple versions of the same
library can reside on the same system due to versioning. The version of
the library is included in the symbol name so the linker knows which
version to link with.
</p>
      <p>
One can look at the symbol version used: <tt>nm csub1.o</tt>
      </p>
      <dl>
        <dd>
          <pre>00000000 T ctest1
<br /></pre>

No version is specified in object code by default.
        </dd>
      </dl>

      <a href="http://www.gnu.org/software/binutils/manual/ld-2.9.1/html_mono/ld.html">ld and object file layout</a>
      <p>
There is one GNU C/C++ compiler flag that explicitly deals with symbol versioning.
Specify the version script to use at compile time with the flag: <tt>--version-script=<i>your-version-script-file</i></tt>
      <br />
Note: This is only useful when creating shared libraries. It is assumed
that the programmer knows which libraries to link with when static
linking. Runtime linking allows opportunity for library
incompatibility.
</p>
      <p>
GNU/Linux, see examples of version scripts here:
    <tt>sysdeps/unix/sysv/linux/Versions</tt>
      </p>
      <p>
Some symbols may also get version strings from assembler code which
appears in glibc headers files.  Look at <tt>include/libc-symbols.h</tt>.
      </p>
      <p>
Example: <tt> nm /lib/libc.so.6 | more</tt>
      </p>
      <dl>
        <dd>
          <pre>00000000 A GCC_3.0
00000000 A GLIBC_2.0
00000000 A GLIBC_2.1
00000000 A GLIBC_2.1.1
00000000 A GLIBC_2.1.2
00000000 A GLIBC_2.1.3
00000000 A GLIBC_2.2
00000000 A GLIBC_2.2.1
00000000 A GLIBC_2.2.2
00000000 A GLIBC_2.2.3
00000000 A GLIBC_2.2.4
...
..
      </pre>

Note the use of a version script.
          <p>
Library referencing a versioned library: <tt>nm /lib/libutil-2.2.5.so</tt>
          </p>
          <pre>..
...
         U strcpy@@GLIBC_2.0
         U strncmp@@GLIBC_2.0
         U strncpy@@GLIBC_2.0
...
..
      </pre>

        </dd>
      </dl>

      <p>
      <b>Links:</b>
      </p>
      <ul>

        <li><a href="http://gcc.gnu.org/ml/gcc-help/2004-03/msg00265.html">Symbol versioning</a>
        </li>
        <li><a href="http://www.gnu.org/software/binutils/manual/ld-2.9.1/html_mono/ld.html#SEC25">GNU.org: ld version scripts</a>
        </li>
      </ul>

      <p>
      </p>
      <hr />
      <table border="0" cellpadding="2" cellspacing="0" width="100%">

        <tbody>
          <tr bgcolor="#ffcc33">
            <td><b><big>Dynamic loading and un-loading of shared libraries using libdl:</big></b></td>
          </tr>

        </tbody>
      </table>

      <p> 
These libraries are dynamically loaded / unloaded and linked during execution. 
Usefull for creating a "plug-in" architecture.
      </p>
      <p> 
Prototype include file for the library: <tt>ctest.h</tt>
      </p>
      <dl>
        <dd>
          <table bgcolor="#000000" border="0" cellpadding="4" cellspacing="1" width="100%">

            <tbody>
              <tr bgcolor="#c0c0c0">
                <td>
                <pre>#ifndef CTEST_H
#define CTEST_H

#ifdef __cplusplus
extern "C" {
#endif

void ctest1(int *);
void ctest2(int *);

#ifdef __cplusplus
}
#endif

#endif
      </pre>

                </td>
              </tr>
            </tbody>
          </table>

Use the notation <tt>extern "C"</tt> so the libraries can be used with
C and C++. This statement prevents the C++ from name mangling and thus
creating "unresolved symbols" when linking.
</dd>
      </dl>

      <p> 
Load and unload the library <tt>libctest.so</tt> (created above), dynamically:
      </p>
      <dl>
        <dd>
          <table bgcolor="#000000" border="0" cellpadding="4" cellspacing="1" width="100%">
            <tbody>
              <tr bgcolor="#c0c0c0">
                <td>
                <pre>
#include &lt;stdio.h&gt;
#include &lt;dlfcn.h&gt;
#include "ctest.h"

int main(int argc, char **argv) 
{
   void *lib_handle;
   double (*fn)(int *);
   int x;
   char *error;

   lib_handle = dlopen("/opt/lib/libctest.so", RTLD_LAZY);
   if (!lib_handle) 
   {
      fprintf(stderr, "%s\n", dlerror());
      exit(1);
   }

   fn = dlsym(lib_handle, "ctest1");
   if ((error = dlerror()) != NULL)  
   {
      fprintf(stderr, "%s\n", error);
      exit(1);
   }

   (*fn)(&amp;x);
   printf("Valx=%d\n",x);

   dlclose(lib_handle);
   return 0;
}
                 </pre>
                </td>
              </tr>
            </tbody>
          </table>

        </dd>
      </dl>

      <p> 
      <tt>gcc -rdynamic -o progdl progdl.c -ldl</tt>
      </p>
      <p> 
Explanation:
      </p>
      <ul>

        <li><tt>dlopen("/opt/lib/libctest.so", RTLD_LAZY);</tt>
    <br />
Open shared library named "<tt>libctest.so</tt>".
    <br />
The second argument indicates the binding. See include file <tt>dlfcn.h</tt>.
    <br />
Returns NULL if it fails.
    <br />
Options:
        
          <ul>

        <li>RTLD_LAZY: If specified, Linux is not concerned about unresolved symbols until they are referenced.
        </li>
            <li>RTLD_NOW: All unresolved symbols resolved when <tt>dlopen()</tt> is called.
        </li>
            <li>RTLD_GLOBAL: Make symbol libraries visible.
        </li>
          </ul>

        </li>
        <li><tt>dlsym(lib_handle, "ctest1");</tt>
    <br />
Returns address to the function which has been loaded with the shared library..
    <br />
Returns NULL if it fails.
    <br />
Note: When using C++ functions, first use <tt>nm</tt> to find the 
    "mangled" symbol name or use the <tt>extern "C"</tt> construct to avoid 
    name mangling. 
    <br />
i.e. <tt>extern "C" void <i>function-name</i>();</tt>
        </li>
      </ul>
<p>
<b>Object code location:</b> Object code archive libraries can be located with either the executable or the loadable library. 
Object code routines used by both should not be duplicated in each. 
This is especially true for code which use static variables such as singleton classes. 
A static variable is global and thus can only be represented once. 
Including it twice will provide unexpected results. 
The programmer can specify that specific object code be linked with the executable by using linker commands which are passed on by the compiler.
</p>
<p>
Use the "-Wl" gcc/g++ compiler flag to pass command line arguments on to the GNU "ld" linker.
</p>
<p>Example makefile statement: <tt>g++ -rdynamic -o appexe $(OBJ) $(LINKFLAGS) -Wl,--whole-archive -L{AA_libs} -laa -Wl,--no-whole-archive $(LIBS)</tt>
</p>
<ul>
<li>--whole-archive: This linker directive specifies that the libraries listed following this directive (in this case <tt>AA_libs</tt>) shall be included in the resulting output even though there may not be any calls requiring its presence. This option is used to specify libraries which the loadable libraries will require at run time.</li>
<li>-no-whole-archive: This needs to be specified whether you list additional object files or not. The gcc/g++ compiler will add its own list of archive libraries and you would not want all the object code in the archive library linked in if not needed. It toggles the behavior back to normal for the rest of the archive libraries.</li>
</ul>
      <p> 
      <b>Man pages:</b>
      </p>
      <ul>

        <li><a href="http://man.yolinux.com/cgi-bin/man2html?cgi_command=dlopen">dlopen()</a> - gain access to an executable object file
        </li>
        <li><a href="http://man.yolinux.com/cgi-bin/man2html?cgi_command=dlclose">dclose()</a> - close a dlopen object
        </li>
        <li><a href="http://man.yolinux.com/cgi-bin/man2html?cgi_command=dlsym">dlsym()</a> - obtain the address of a symbol from a dlopen object
        </li>
        <li><a href="http://man.yolinux.com/cgi-bin/man2html?cgi_command=dlvsym">dlvsym()</a> - Programming interface to dynamic linking loader.
        </li>
        <li><a href="http://man.yolinux.com/cgi-bin/man2html?cgi_command=dlerror">dlerror()</a> - get diagnostic information
        </li>
      </ul>

      <p> 
      <b>Links:</b>
      </p>
      <ul>

        <li><a href="http://www.samspublishing.com/articles/article.asp?p=22435&amp;rl=1">Shared Libraries-Dynamic Loading and Unloading</a>
        </li>
        <li><a href="http://developer.gnome.org/doc/API/glib/glib-dynamic-loading-of-modules.html">GNOME Glib dynamic loading of modules</a> - cross platform API for dynamically loading "plug-ins".
        </li>
      </ul>

      <p>
      </p>
      <hr />
      <table border="0" cellpadding="2" cellspacing="0" width="100%">
        <tbody>
          <tr bgcolor="#ffcc33">
            <td><b><big>C++ class objects and dynamic loading:</big></b></td>
          </tr>
        </tbody>
      </table>
      <p>
<b>C++ and name mangling:</b>
      </p>
      <p>
When running the above "C" examples with the "C++" compiler one will quickly find that "C++" function names get mangled and thus will not work unless the function definitions are protected with <tt>extern "C"{}</tt>. 
      <dl>
      <dd>
Note that the following are not equivalent:
      <table bgcolor="#000000" border="0" cellpadding="4" cellspacing="1" width="100%">
      <tbody>
      <tr bgcolor="#c0c0c0">
      <td width="50%">
      <pre>
extern "C"
{
   int functionx();
}
      </pre>
      </td>
      <td valign="top">
      <pre>
extern "C" int functionx();
      </pre>
      </td>
      </tr></tbody></table>
      <p>
      </p>
      The following are equivalent:
      <table bgcolor="#000000" border="0" cellpadding="4" cellspacing="1" width="100%">
      <tbody>
      <tr bgcolor="#c0c0c0">
      <td width="50%">
      <pre>
extern "C"
{
   extern int functionx();
}
      </pre>
      </td>
      <td valign="top">
      <pre>
extern "C" int functionx();
      </pre>
      </td>
      </tr></tbody></table>
      </dd></dl>
      </p>
      <p>
<b>Dynamic loading of C++ classes:</b>
      </p>
      <p>
The dynamic library loading routines enable the programmer to load "C" functions. 
In C++ we would like to load class member functions. In fact the entire class may be in the library and we may want to load and have access to the entire object and all of its member functions.
Do this by passing a "C" class factory function which instantiates the class.
      </p>
      The class ".h" file:
      <dl><dd>
      <table bgcolor="#000000" border="0" cellpadding="4" cellspacing="1" width="100%">
      <tbody>
      <tr bgcolor="#c0c0c0">
      <td>
      <pre>
class Abc {

...
...

};

// Class factory "C" functions

typedef Abc* create_t;
typedef void destroy_t(Abc*);
      </pre>
      </td></tr></tbody></table>
      </dd></dl>
      <p>
      </p>
      The class ".cpp" file:
      <dl><dd>
      <table bgcolor="#000000" border="0" cellpadding="4" cellspacing="1" width="100%">
      <tbody>
      <tr bgcolor="#c0c0c0">
      <td>
      <pre>
Abc::Abc()
{
    ...
}

extern "C"
{
   // These two "C" functions manage the creation and destruction of the class Abc

   Abc* create()
   {
      return new Abc;
   }

   void destroy(Abc* p)
   {
      delete p;   // Can use a base class or derived class pointer here
   }
}
      </pre>
      </td></tr></tbody></table>
This file is the source to the library.
The "C" functions to instantiate (create) and destroy a class defined in the dynamically loaded library where "Abc" is the C++ class.
      </dd></dl>
      <p>
      </p>
      Main executable which calls the loadable libraries:
      <dl><dd>
      <table bgcolor="#000000" border="0" cellpadding="4" cellspacing="1" width="100%">
      <tbody>
      <tr bgcolor="#c0c0c0">
      <td>
      <pre>
// load the symbols
    create_t* create_abc = (create_t*) dlsym(lib_handle, "create");

...
...

    destroy_t* destroy_abc = (destroy_t*) dlsym(lib_handle, "destroy");

...
...
      </pre>
      </td></tr></tbody></table>
      </dd></dl>
<p>
</p>
<b>Pitfalls:</b>
      <ul>
      <li>The new/delete of the C++ class should both be provided by the 
          executable or the library but not split. This is so that there is 
          no surprise if one overloads new/delete in one or the other.
      </li>
      </ul>
<p>
</p>
<b>Links:</b>
      <ul>
      <li><a href="http://www.linuxjournal.com/article/3687">LinuxJournal.com: Dynamic Class Loading for C++ on Linux</a>
      </li>
      <li><a href="http://www.isotton.com/howtos/C++-dlopen-mini-HOWTO/C++-dlopen-mini-HOWTO.html">dlopen howto</a>
      </li>
      </ul>
      <p>
      </p>
      <hr />
      <table border="0" cellpadding="2" cellspacing="0" width="100%">

        <tbody>
          <tr bgcolor="#ffcc33">
            <td><b><big>Comparison to the Microsoft DLL:</big></b></td>
          </tr>

        </tbody>
      </table>

      <p> The Microsoft Windows equivalent to the Linux / Unix shared
object (".so")
is the ".dll".
The Microsoft Windows DLL file usually has the extension ".dll", but
may also use the extension ".ocx". On the old 16 bit windows, the
dynamically linked libraries were also named with the ".exe" suffix.
"Executing" the DLL will load it into memory.
</p>
      <p>
The Visual C++ .NET IDE wizard will create a DLL framework through the GUI, 
and generates a ".def" file. This "module definition file" lists the 
functions to be exported. When exporting C++ functions, the C++ mangled
names are used. Using the Visual C++ compiler to generate a ".map" file
will allow you to discover the C++ mangled name to use in the ".def" file.
The "SECTIONS" label in the ".def" file will define the portions which are
"shared".
Unfortunately the generation of DLLs are tightly coupled to the Microsoft IDE,
so much so that I would not recomend trying to create one without it.
      </p>
      <p>
The Microsoft Windows C++ equivalent functions to libdl are the following functions:
      </p>
      <ul>
        <li>::LoadLibrary() - dlopen()
        </li>
        <li>::GetProcAddress() - dlsym()
        </li>
        <li>::FreeLibrary() - dlclose()
        </li>
      </ul>
      <p>
      <font color="#ff0000">[Potential Pitfall]</font>: Microsoft Visual C++ .NET compilers do not allow the linking controll that the GNU linker "ld" allows (i.e. --whole-archive, -no-whole-archive). 
All symbols need to be resolved by the VC++ compiler for both the loadable library and the application executable individually and thus it can cause duplication of libraries when the library is loaded.
This is especially bad when using static variables (i.e. used in singleton patterns) as you will get two memory locations for the static variable, one used by the loadable library and the other used by the program executable. 
This breaks the whole static variable concept and the singleton pattern. 
Thus you can not use a static variable which is referenced by by both the loadable library and the application executable as they will be unique and different.
To use a unique static variable, you must pass a pointer to that static variable to the other module so that each module (main executable and DLL library) can use the same instatiation.
On MS/Windows you can use shared memory or a memory mapped file so that the main executable and DLL library can share a pointer to an address they both will use.
      </p>
Cross platform (Linux and MS/Windows) C++ code snippet:
      <p>
      </p>
      Include file declaration: (.h or .hpp)
      <dl><dd>
      <table bgcolor="#000000" border="0" cellpadding="4" cellspacing="1" width="100%">
      <tbody>
      <tr bgcolor="#c0c0c0">
      <td>
      <pre>
class Abc{
public:
   static Abc* Instance(); // Function declaration. Could also be used as a public class member function.

private:
   static Abc *mInstance;      // Singleton. Use this declaration in C++ class member variable declaration.
   ...
}
      </pre>
      </td></tr></tbody></table>
      </dd></dl>
      <p>
      </p>
      C/C++ Function source: (.cpp)
      <dl><dd>
      <table bgcolor="#000000" border="0" cellpadding="4" cellspacing="1" width="100%">
      <tbody>
      <tr bgcolor="#c0c0c0">
      <td>
      <pre>
/// Singleton instantiation
Abc* Abc::mInstance = 0;   // Use this declaration for C++ class member variable
                           // (Defined outside of class definition in ".cpp" file)

// Return unique pointer to instance of Abc or create it if it does not exist.
// (Unique to both exe and dll)

static Abc* Abc::Instance() // Singleton
{
#ifdef WIN32
    // If pointer to instance of Abc exists (true) then return instance pointer else look for 
    // instance pointer in memory mapped pointer. If the instance pointer does not exist in
    // memory mapped pointer, return a newly created pointer to an instance of Abc.

    return mInstance ? 
       mInstance : (mInstance = (Abc*) MemoryMappedPointers::getPointer("Abc")) ? 
       mInstance : (mInstance = (Abc*) MemoryMappedPointers::createEntry("Abc",(void*)new Abc));
#else
    // If pointer to instance of Abc exists (true) then return instance pointer 
    // else return a newly created pointer to an instance of Abc.

    return mInstance ? mInstance : (mInstance = new Abc);
#endif
}
      </pre>
      </td></tr></tbody></table>
      </dd></dl>
Windows linker will pull two instances of object, one in exe and one in loadable module. 
Specify one for both to use by using memory mapped pointer so both exe and loadable library point to same variable or object. 
<br />
Note that the GNU linker does not have this problem.
      <p>
      </p>
For more on singletons see the YoLinux.com <a href="C++Singleton.html">C++ singleton software design pattern tutorial</a>.
      <p>
      </p>
      <hr />
      <table border="0" cellpadding="2" cellspacing="0" width="100%">
        <tbody>
          <tr bgcolor="#ffcc33">
            <td><b><big>Cross platform programming of loadable libraries:</big></b></td>
          </tr>
        </tbody>
      </table>
      <p>
      </p>
      <dl>
      <dd>
      <table bgcolor="#000000" border="0" cellpadding="4" cellspacing="1" width="100%">
      <tbody>
      <tr bgcolor="#c0c0c0">
      <td>
      <pre>
#ifndef USE_PRECOMPILED_HEADERS
#ifdef WIN32
#include &lt;direct.h&gt;
#include &lt;windows.h&gt;
#else
#include &lt;sys/types.h&gt;
#include &lt;dlfcn.h&gt;
#endif
#include &lt;iostream&gt;
#endif

    using namespace std;

#ifdef WIN32
    HINSTANCE lib_handle;
#else
    void *lib_handle;
#endif

    // Where retType is the pointer to a return type of the function
    // This return type can be int, float, double, etc or a struct or class.

    typedef retType* func_t;  

    // load the library -------------------------------------------------
#ifdef WIN32
    string nameOfLibToLoad("C:\opt\lib\libctest.dll");
    lib_handle = LoadLibrary(TEXT(nameOfLibToLoad.c_str()));
    if (!lib_handle) {
        cerr &lt;&lt; "Cannot load library: " &lt;&lt; TEXT(nameOfDllToLoad.c_str()) &lt;&lt; endl;
    }
#else
    string nameOfLibToLoad("/opt/lib/libctest.so");
    lib_handle = dlopen(nameOfLibToLoad.c_str(), RTLD_LAZY);
    if (!lib_handle) {
        cerr &lt;&lt; "Cannot load library: " &lt;&lt; dlerror() &lt;&lt; endl;
    }
#endif

...
...
...

    // load the symbols -------------------------------------------------
#ifdef WIN32
    func_t* fn_handle = (func_t*) GetProcAddress(lib_handle, "superfunctionx");
    if (!fn_handle) {
        cerr &lt;&lt; "Cannot load symbol superfunctionx: " &lt;&lt; GetLastError() &lt;&lt; endl;
    }
#else
    // reset errors
    dlerror();

    // load the symbols (handle to function "superfunctionx")
    func_t* fn_handle= (func_t*) dlsym(lib_handle, "superfunctionx");
    const char* dlsym_error = dlerror();
    if (dlsym_error) {
        cerr &lt;&lt; "Cannot load symbol superfunctionx: " &lt;&lt; dlsym_error &lt;&lt; endl;
    }
#endif

...
...
...

    // unload the library -----------------------------------------------

#ifdef WIN32
    FreeLibrary(lib_handle);
#else
    dlclose(lib_handle);
#endif
      </pre>
      </td></tr></tbody></table>
      </dd></dl>

      <p>
      </p>
      <hr />
      <table border="0" cellpadding="2" cellspacing="0" width="100%">
        <tbody>
          <tr bgcolor="#ffcc33">
            <td><b><big>Tools:</big></b></td>
          </tr>
        </tbody>
      </table>

      <p> 
      <b>Man pages:</b>
      </p>
      <ul>

        <li><a href="http://man.yolinux.com/cgi-bin/man2html?cgi_command=ar">ar</a> - create, modify, and extract from archives
        </li>
        <li><a href="http://man.yolinux.com/cgi-bin/man2html?cgi_command=ranlib">ranlib</a> - generate index to archive
        </li>
        <li><a href="http://man.yolinux.com/cgi-bin/man2html?cgi_command=nm">nm</a> - list symbols from object files
        </li>
        <li><a href="http://man.yolinux.com/cgi-bin/man2html?cgi_command=ld">ld</a> - Linker
        </li>
        <li><a href="http://man.yolinux.com/cgi-bin/man2html?cgi_command=ldconfig">ldconfig</a> - configure dynamic linker run-time bindings
    <br />

    <tt>ldconfig -p</tt> : Print the lists of directories and candidate  libraries  stored in the current cache.
    <br />
i.e. <tt>/sbin/ldconfig -p |grep libGL</tt>
        </li>
        <li><a href="http://man.yolinux.com/cgi-bin/man2html?cgi_command=ldd">ldd</a> - print shared library dependencies
        </li>
        <li><a href="http://man.yolinux.com/cgi-bin/man2html?cgi_command=gcc">gcc/g++</a> - GNU project C and C++ compiler
        </li>
        <li>man page to: <a href="http://man.yolinux.com/cgi-bin/man2html?cgi_command=ld.so">ld.so</a> - a.out dynamic linker/loader
        </li>
      </ul>


      <p>
      </p>
      <hr />
      <table border="0" cellpadding="2" cellspacing="0" width="100%">

        <tbody>
          <tr bgcolor="#ffcc33">
            <td><b><big>Notes:</big></b></td>
          </tr>

        </tbody>
      </table>

      <p> 
      </p>
      <ul>

        <li>Direct loader to preload a specific shared library before all others: <tt>export LD_PRELOAD=/usr/lib/libXXX.so.x; exec <i>program</i></tt>. This is specified in the file <tt>/etc/ld.so.preload</tt> and extended with the environment variable <tt>LD_PRELOAD</tt>.
    <br />
Also see:
    
          <ul>

    <li>man page to: <a href="http://man.yolinux.com/cgi-bin/man2html?cgi_command=ld.so">ld.so</a> - a.out dynamic linker/loader
    </li>
            <li><a href="http://www.uberhip.com/people/godber/interception/index.html">LD_PRELOAD and Linux function interception</a>.
    </li>
          </ul>

          <p>
          </p>
        </li>
        <li>Running Red Hat 7.1 (glibc 2.2.2) but compiling for Red Hat 6.2 compatibility. 
    <br />

    See <a href="ftp://herbie.ucs.indiana.edu/pub/linux/redhat/redhat/redhat-7.1-en/os/i386/RELEASE-NOTES">RELEASE-NOTES</a>
    <br />

    
          <pre>        export LD_ASSUME_KERNEL=2.2.5<br />        . /usr/i386-glibc21-linux/bin/i386-glibc21-linux-env.sh<br />    </pre>

          <p>
          </p>
        </li>
        <li>Environment variable to highlight warnings, errors, etc: <tt>export CC="colorgcc"</tt>
          <p>
          </p>
        </li>
      </ul>

      <p>
      </p>
      <hr />
      <table border="0" cellpadding="2" cellspacing="0" width="100%">

        <tbody>
          <tr bgcolor="#ffcc33">
            <td><b><big>YoLinux Software Development Tutorials:</big></b></td>
          </tr>

        </tbody>
      </table>

      <p>
      </p>
      <dl>
        <dd>
          <ul>

            <li>Editors:
    
              <ul>

    <li><a href="LinuxTutorialAdvanced_vi.html"><b>VI</b> / <b>VIM</b> - Advanced editing</a> - tutorial, tips and tricks
    </li>
                <li><a href="LinuxTutorialXemacs.html"><b>X-Emacs</b> - Editing C++ code </a> - Use, settings, code beautification
    </li>
              </ul>


            </li>
            <li>Source Code Control/Revision:
    
              <ul>

    <li><a href="LinuxTutorialCVSintro.html"><b>CVS</b> source code control</a> - Track and manage a set of files and their revisions.
    </li>
                <li><a href="LinuxTutorialRCSinto.html"><b>RCS</b> source code control</a> - Track and manage individual files and their revisions.
    </li>
                <li>IBM/Rational <b>Clearcase</b>:
        
                  <ul>


        <li><a href="LinuxClearcaseClient.html">Clearcase Client Installation</a>
        </li>
                    <li><a href="ClearcaseCommands.html">Clearcase commands</a>
        </li>
                  </ul>

    </li>
              </ul>

            </li>
            <li><a href="LinuxTutorialC++.html">C++ Programming on Linux</a>:
    
              <ul>

    <li><a href="LinuxTutorialC++STL.html">C++ <b>STL</b> (Standard Template Library) example for Linked Lists</a>

    </li>
                <li><a href="LinuxTutorialC++StringClass.html">Linux/<b>Gnu C++ string class</b> examples and tutorial</a>
    </li>
                <li><a href="LinuxTutorialC++Structures.html">Use of structures in C++</a> - This example shows the use of a constructor with a structure. The stuff they never included in the text books.
    </li>
                <li><a href="LinuxTutorialC++CGI.html">Programming web server CGI programs with C++ and the GNU CgiCC library</a>
    </li>
                <li><a href="LinuxTutorialC++CodingStyle.html">C++ Coding styles and practices</a>
    </li>
                <li><a href="C++MemoryCorruptionAndMemoryLeaks.html">C++ Memory corruption and leaks</a>
    </li>
              </ul>

            </li>
            <li><a href="MicrosoftVisualC++Tips.html">Tips and Best Practices for Microsoft Visual C++ users</a>  (So they don't make ugly files and mess up your project.)
            </li>
            <li><a href="CORBA.html"><b>CORBA</b>, OmniORB, C++ and remote services (client/server request)</a>
            </li>
            <li><a href="ForkExecProcesses.html">Fork, exec and process control</a>
            </li>
            <li><b>Threads:</b>
    
              <ul>

    <li><a href="LinuxTutorialPosixThreads.html">POSIX Threads</a> - make your code run faster and more scalable.
    </li>
                <li><a href="GDK_Threads.html">GNOME GDK Threads (g_threads)</a> - Cross platform threads API.
    </li>
              </ul>


            </li>
            <li><a href="GTK+ProgrammingTips.html">GNOME <b>GTK+</b> Programming Tips and Tricks</a> - Using the GTK+ cross platform GUI API to build an interactive application.
            </li>
            <li><a href="GnomeLibXml2.html">XML and parsing <b>XML</b> with <b>Gnome libXML2</b></a>
            </li>
            <li><a href="LinuxTutorialMixingFortranAndC.html">Mixing C and FORTRAN language subroutines</a>
            </li>
            <li><a href="LinuxTutorialJava.html"><b>JAVA</b> on Linux</a>
            </li>
            <li><a href="LinuxTutorialLDAP-SoftwareDevelopment.html">OpenLDAP C programming SDK Man Pages, RFC's and Links</a>
            </li>
          </ul>

          <p> 
          </p>
        </dd>
      </dl>

      <p>
      </p>
      <hr />
      <table border="0" cellpadding="2" cellspacing="0" width="100%">

        <tbody>
          <tr bgcolor="#ffcc33">
            <td><b><big>Links:</big></b></td>
          </tr>

        </tbody>
      </table>

      <p>
      </p>
      <ul>

        <li><a href="http://www.gnu.org/software/libtool/libtool.html">GNU: Libtool</a> - script for portable shared library creation.
        </li>
        <li><a href="http://www.suse.de/%7Ebastian/Export/linking.txt">Suse.de: Discussion of improvement in ld.so for performance improvement.</a>
        </li>
        <li><a href="http://www.tldp.org/HOWTO/Program-Library-HOWTO/">LDP Library Howto</a>
        </li>
      </ul>

      <p>
      </p>
      <hr />
      <table border="0" cellpadding="2" cellspacing="0" width="100%">

        <tbody>
          <tr bgcolor="#ffcc33">
            <td valign="top"><img src="images/book40.gif" alt="Books" /><b><big>Books:</big></b></td>
          </tr>

        </tbody>
      </table>

      <p>
      </p>
      <dl>
        <dd>
          <table cellpadding="5" border="1">

    <tbody>
              <tr>
                <td><img src="../BOOKS/0735710430.01.MZZZZZZZ.jpg" alt="Book image" /></td>

    <td valign="top">
    "Advanced Linux Programming"
    <br />

    by Mark Mitchell, Jeffrey Oldham, Alex Samuel, Jeffery Oldham
    <br />

    ISBN # 0735710430, New Riders
    
                <p>
    Good book for programmers who already know how to program and just need
    to know the Linux specifics. Covers a variety of Linux tools, libraries,
    API's and techniques. If you don't know how to program, start with a
    book on C.
    </p>
                </td>

    <td valign="top">
    <a href="http://www.amazon.com/gp/redirect.html?ie=UTF8&location=http://www.amazon.com/exec/obidos/ASIN/0735710430/&tag=yolinux-20"><img src="images/Amazon-BuyABook88x31.gif" alt="Amazon.com" /></a>
    <br />

    </td>
              </tr>


    <tr>
                <td><img src="../BOOKS/0764546570.01.MZZZZZZZ.jpg" alt="Book image" /></td>

    <td valign="top">
    "Linux Programming Bible"
    <br />

    by John Goerzen
    <br />

    ISBN # 0764546570, Hungry Minds, Inc
    
                <p>
    This covers the next step after "C" programming 101.
    </p>
                </td>

    <td valign="top">
    <a href="http://www.amazon.com/gp/redirect.html?ie=UTF8&location=http://www.amazon.com/exec/obidos/ASIN/0764546570/&tag=yolinux-20"><img src="images/Amazon-BuyABook88x31.gif" alt="Amazon.com" /></a>
    <br />

    </td>
              </tr>

            </tbody>
          </table>

        </dd>
      </dl>

      <p>
      </p>
      </td>

      <td bgcolor="#c0c0c0" valign="top" width="160">
<!-- AddThis Button BEGIN -->
<div id="bookmark" align="left">
&nbsp;&nbsp;&nbsp;
<a href="http://www.addthis.com/bookmark.php?v=250&pub=yolinux" onmouseover="return addthis_open(this, '', '[URL]', '[TITLE]')" onmouseout="addthis_close()" onclick="return addthis_sendto()"><img src="http://s7.addthis.com/static/btn/lg-bookmark-en.gif" width="125" height="16" alt="Bookmark and Share" style="border:0"/></a>
<script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js?pub=yolinux">
</script>
</div>
<!-- AddThis Button END -->
<hr />
<p>
</p>

<div align="center">
<script type="text/javascript"><!--
   e9 = new Object();
   e9.size = "160x600";
   e9.addBlockingCategories="Flashing,Pop-under,Pop-up,Floating,Full-page,Survey";
   e9.noAd = 1;
//--></script>
<script type="text/javascript" src="http://tags.expo9.exponential.com/tags/YoLinuxcom/ROS/tags.js"></script>
</div>

<p>
</p>
<hr />
<p>
</p>

<div align="center">
<!--  Rubicon Project tag -->
<!--  Site: YoLinux.com   Zone: My Site   Size: Wide Skyscraper (160x600)  -->
<script language="JavaScript" type="text/javascript">
var cb = Math.random();
var d = document;
d.write('<script language="JavaScript" type="text/javascript"');
d.write('src="http://optimized-by.rubiconproject.com/a/6142/8070/13650-9.js?cb='+cb+'">');
d.write('<\/scr'+'ipt>');
</script>
<!-- end Rubicon Project tag -->
</div>
<p>
</p>
<hr />
<p>
</p>
<iframe src="http://rcm.amazon.com/e/cm?t=yolinux-20&o=1&p=14&l=bn1&mode=pc-hardware&browse=172455&fc1=000000&lt1=_blank&lc1=3366FF&bg1=C0C0C0&f=ifr" marginwidth="0" marginheight="0" width="160" height="600" border="0" frameborder="0" style="border:none;" scrolling="no"></iframe>

<p>
</p>
<hr />
<p>
</p>
<script src='http://www.enticelabs.com/EL/assets/js/el_dyn.js?c=aa9f7dfd421da13059aafe6994c77783&h=600&w=160&d=https://www.enticelabs.com:443/EL' type='text/javascript'></script>
<p>
</p>
<hr />
<p>
</p>


      </td>

    </tr>
  </tbody>
</table>
<table width="100%" bgcolor="#646664" cellpadding="4" background="images/footer_pattern.png">
<tbody>
<tr>
<td valign="top">
<font size="-1" color="#ffcc33">
<b>
<a class="footer" href="http://www.yolinux.com">YoLinux.com Home Page</a>
<br />
<a class="footer" href="http://yolinux.com/TUTORIALS/">YoLinux Tutorial Index</a> |
<a class="footer" href="http://yolinux.com/YoLinux-Terms.html">Terms</a>
<br />
<a class="footer" href="http://www.yolinux.com/privacy.html">Privacy Policy</a> |
<a class="footer" href="http://www.yolinux.com/YoLinux-Advertising.html">Advertise with us</a> | 
<a class="footer" href="../YoLinuxEmailForm.html">Feedback Form</a> |
<br />
Unauthorized copying or redistribution prohibited.
</b>
</font>
</td>
<td align="center" valign="bottom">
<script src="http://www.stumbleupon.com/hostedbadge.php?s=5"></script>
</td>
<td align="center" valign="bottom">
<a title="Post on Google Buzz" class="google-buzz-button" href="http://www.google.com/buzz/post" data-button-style="normal-count"></a>
<script type="text/javascript" src="http://www.google.com/buzz/api/button.js"></script>
</td>
<td align="center" valign="bottom">
<script src="http://widgets.digg.com/buttons.js" type="text/javascript"></script>
<a class="DiggThisButton DiggMedium" rev="linux_unix">
<span style="display:none">
YoLinux Tutorials. Linux Information Portal includes informative tutorials and links to many Linux sites.
</span>
</a>
</td>
<td align="center" valign="bottom">
<p>
</p>
<script type="text/javascript">reddit_target='[LINUX]'</script>
<script type="text/javascript" src="http://reddit.com/static/button/button1.js"></script>
<br />
<!-- AddThis Button BEGIN -->
<a href="http://www.addthis.com/bookmark.php?v=250&pub=yolinux" onmouseover="return addthis_open(this, '', '[URL]', '[TITLE]')" onmouseout="addthis_close()" onclick="return addthis_sendto()"><img src="http://s7.addthis.com/static/btn/lg-bookmark-en.gif" width="125" height="16" alt="Bookmark and Share" style="border:0"/></a>
<script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js?pub=yolinux">
</script>
<!-- AddThis Button END -->
</td>
<td valign="top">
<div align="right">
<script type="text/javascript"><!--
google_ad_client = "pub-8567479315106986";
/* 234x60, created 4/23/10 */
google_ad_slot = "8214196957";
google_ad_width = 234;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>
</td>
</tr>
</tbody>
</table>
<p>
</p>

<small>Copyright &copy; 2005-2008 by <i>Greg Ippolito</i></small>
<!-- Start Quantcast tag -->
<script type="text/javascript" src="http://edge.quantserve.com/quant.js"></script>
<script type="text/javascript">_qacct="p-ebIBcaVUngFBQ";quantserve();</script>
<noscript>
<a href="http://www.quantcast.com/p-ebIBcaVUngFBQ" target="_blank"><img src="http://pixel.quantserve.com/pixel/p-ebIBcaVUngFBQ.gif" style="display: none;" border="0" height="1" width="1" alt="Quantcast"/></a>
</noscript>
<!-- End Quantcast tag -->
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-3317450-1");
pageTracker._initData();
pageTracker._trackPageview();
</script>

</body>
</html>