/*
SList.h
單鏈表的實現
*/
#ifndef SLIST_H_
#define SLIST_H_
#include <iostream>
class Node
...{
public:
int info;
Node *next;
Node(int el,Node* ptr=0)
...{
info = el;
next = ptr;
}
};
class SList...{
public:
SList()...{
head = tail = 0;
}
int isEmpty()...{
return head == 0;
}
void addToHead(int);
int deleteFromHead();
friend std::ostream& operator<<(std::ostream&, const SList&);
virtual ~SList();
private:
Node *head,*tail;
};
#endif //SLIST_H_
/**//* SList.cpp */
#include "SList.h"
SList::~SList()...{
for(Node* p;!isEmpty();)...{
p = head->next;
delete p;
head = p;
}
}
void SList::addToHead(int el)...{
head = new Node(el,head);
if(tail == 0)
tail = head;
}
int SList::deleteFromHead()...{
int el = head->info;
Node *tmp = head;
if(head == tail)
head = tail = 0;
else head = head->next;
delete tmp;
return el;
}
std::ostream& operator<<(std::ostream& os, const SList& ss)...{
Node* p;
p=ss.head;
os<<p->info;
ign="top" /> for(p=p->next;p;p=p->next)
os<<"->"<<p->info;
return os;
}
/**//* main.cpp */
#include <iostream>
#include "SList.h"
using namespace std;
int main(int argc, char* argv[])
...{
int ss[]=...{20,11,21,23,10,19,17,4};
size_t n = sizeof(ss)/sizeof(int);
SList sl;
for(int i=0;i<n;++i)
sl.addToHead(ss[i]);
cout<<sl<<endl;
return 0;
}
一、建立共用庫
1 單獨編譯SList.cpp,編譯時間需要傳入-fPIC選項,告訴編譯器產生位置無關代碼.
位置無關代碼可以被載入到地址空間的任意位置而不需要修改.
[root@LEE src]# ls
main.cpp SList.cpp SList.h
[root@LEE src]# g++ -fPIC -g -c SList.cpp
2 當連結到庫時,為連結器傳入-shared選項,把目標檔案SList.o連結為共用對象
libSList.so.1.0.1
每個共用庫都有一個特定的搜尋名(soname).搜尋名約定如下:
lib+庫名+.so+.版本號碼
在檔案系統中,搜尋名是一個指向實名的符號串連.每個共用庫也有一個特定的實名,約定如下:
搜尋名+.子版本號碼+.發布號
你可以使用一個特殊的編譯器選項-Wl,option,將option傳給ld,用逗號分隔多個option,
為了在所有的系統上得到最好結果,連結libSList到標準C++庫上
[root@LEE src]# g++ -g -shared -Wl,-soname,libSList.so.1 -o libSList.so.1.0.1 SList.o -lstdc++
[root@LEE src]# ls
libSList.so.1.0.1 main.cpp SList.cpp SList.h SList.o
libSList.so.1是搜尋名,libSList.so.1.0.1是實名,SList.o是目標檔案(也可以是多個目標檔案的列表)
,-lstdc++是庫需要訪問的庫(也可以是庫的列表-llibrary,關於此選項參考附錄.)
3 建立一個從soname連結到庫
[root@LEE src]# ln -fs libSList.so.1.0.1 libSList.so.1
[root@LEE src]# ln -fs libSList.so.1 libSList.so
4 使用-L使連結器在目前的目錄中尋找庫,並且使用-lSList告訴它要連結哪個庫
[root@LEE src]# g++ -g -c main.cpp -o main.o
[root@LEE src]# g++ -g -o main main.o -L. -lSList
[root@LEE src]# ls
libSList.so libSList.so.1.0.1 main.cpp SList.cpp SList.o
libSList.so.1 main main.o SList.h
5 運行命令
[root@LEE src]# LD_LIBRARY_PATH=$(pwd) ./main
4->17->19->10->23->21->11->20
LD_LIBRARY_PATH
提供用來搜尋庫的目錄路徑,以冒號作為間隔.正常情況下它不應該被設定,因為系統檔案
/ect/ld.so.conf提供了預設的路徑.
二、使用連結庫
當運行一個程式時,動態裝載器通常在/ect/ld.so.conf.d目錄尋找程式所需要的庫.但是,如果
LD_LIBRARY_PATH 環境變數被設定,它首先掃描在LD_LIBRARY_PATH 中列出的目錄.對於上一節5,如果直接運行
命令 ./main,會出現找不到庫的現象.如:
[root@LEE src]# ./main
./main: error while loading shared libraries: libSList.so.1: cannot open shared object file: No such file or directory
上述問題的解決方案有兩種(我能想到的):
1 如果LD_LIBRARY_PATH 沒設定:
拷貝libSList.so到目錄 /usr/local/lib (同理也可以拷到/usr/lib下)
[root@LEE src]# cp libSList.so /usr/local/lib
[root@LEE src]# ldconfig /usr/local/lib
[root@LEE src]# ./main
4->17->19->10->23->21->11->20
2 如果LD_LIBRARY_PATH 設定:
編輯.bash_profile檔案
添加LD_LIBRARY_PATH:=$LD_LIBRARY_PATH:/path/to/libSList.so(不包括 libSList.so)
執行.bash_profile 檔案.
[root@LEE src]# . /root/.bash_profile
[root@LEE src]# ./main
4->17->19->10->23->21->11->20
這些天看了一些關於靜態/動態連結程式庫的貼子和書,將這幾天的研究整理成文,決定發表一下.我很早就註冊了CSDN,也很想成為一名軟體工程師,這個願望在不久前實現了,所以更加努力,登陸CSDN的次數也有了數量級的增加.我有過一些Linux下的C++程式開發經曆,相對於同專業(數學類)的同學,我有著較好的Linux和C++技能.最近在CSDN上看了很多優秀的文章,從中受益良多.這是我第一次發表文章,希望各位多多指教.