在Fedora裡面安裝BerkeleyDB資料庫

來源:互聯網
上載者:User

一、首先到Oracle的官網下載Berkeley db資料庫源檔案

http://download.oracle.com/otn/berkeley-db/db-5.3.15.tar.gz

二、下載之後的檔案是一個打包好的檔案,需要在命令列裡面利用tar來解壓(當然你也可以利用一些視覺化檢視來解壓),步驟如下

在命令列裡面輸入 tar -zxvf  db-5.3.15.tar.gz

解壓之後進入db-5.3.15目錄有以下檔案及檔案夾

進入build_unix目錄

cd build_unix

之後運行../dist/configure

configure工具檢查環境之後,併產生編譯器所需要的檔案,當它成功運行之後,接著在命令列裡面輸入

make

當編譯結束之後,你需要運行“make install(需要root許可權)”,之後庫檔案和所需要的開發檔案將會安裝到你的系統中去。

這時安裝好的檔案將會預設放在系統的/usr/local/BerkeleyDB.5.3目錄裡面,為了能夠在編程環境中利用它,你需要在/etc/ld.so.conf檔案中加入/usr/local/BerkeleyDB.5.3/lib,這樣你的程式才能正確找到這個庫檔案,最後運行ldconfig命令更新你的系統。好了,現在你就可以編寫DB資料庫代碼了。

  1. #include <stdio.h>   
  2. #include <db.h>   
  3. #include<string.h>   
  4. #define DATABASE "employees.db"   
  5.   
  6. int main()  
  7. {  
  8.    DBT key, data;  
  9.    DB *dbp;  
  10.    int ret;  
  11.    struct data_struct {  
  12.       int empid;  
  13.       char lastname[50];  
  14.       char firstname[50];  
  15.       float salary;  
  16.    } emp;  
  17.   
  18.    ret = db_create(&dbp, NULL, 0);  
  19.    if (ret != 0)  
  20.    {  
  21.       perror("create");  
  22.       return 1;  
  23.    }  
  24.   
  25.    ret = dbp->open(dbp, NULL, DATABASE, NULL, DB_BTREE, DB_CREATE, 0);  
  26.    if (ret != 0)  
  27.    {  
  28.       perror("open: ");  
  29.       return 1;  
  30.    }  
  31.   
  32.    while(1)  
  33.    {  
  34.       printf("Enter Employee ID: ");  
  35.       scanf("%d", &emp.empid);  
  36.       if (emp.empid == 0)  
  37.          break;  
  38.       printf("Enter Last name: ");  
  39.       scanf("%s", &emp.lastname);  
  40.       printf("Enter First name: ");  
  41.       scanf("%s", &emp.firstname);  
  42.       printf("Enter Salary: ");  
  43.       scanf("%f", &emp.salary);  
  44.   
  45.       memset(&key, 0, sizeof(DBT));  
  46.       memset(&data, 0, sizeof(DBT));  
  47.   
  48.       key.data = &(emp.empid);  
  49.       key.size = sizeof(emp.empid);  
  50.       data.data = &emp;  
  51.       data.size = sizeof(emp);  
  52.   
  53.       ret = dbp->put(dbp, NULL, &key, &data, DB_NOOVERWRITE);  
  54.       if (ret != 0)  
  55.       {  
  56.          printf("Employee ID exists\n");  
  57.       }  
  58.    }  
  59.   
  60.    dbp->close(dbp, 0);  
  61.    return 0;  
  62. }  

編譯代碼

gcc -I/usr/local/BerkeleyDB.5.3/include -o newemployee newemployee.c -L/usr/local/BerkeleyDB.5.3 -ldb

按照上面的編譯將會出現一個錯誤

/usr/bin/ld: cannot find -ldb

collect2: ld 返回 1

這時在命令列裡面輸入下面的語言問題就可以解決

ln -s /usr/local/BerkeleyDB.5.3/lib/libdb.so /usr/lib/libdb.so

再編譯

gcc -I/usr/local/BerkeleyDB.5.3/include -o newemployee newemployee.c -L/usr/local/BerkeleyDB.5.3 -ldb

好了,最後代碼編譯成功。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.