Shared Libraries

2006/02/07
~ 阿亮 ~
最近寫的程式莫名其妙地越來越肥,所以,花時間研究一下 shared libraries.
Under Unix
  • Compile 的方式
    • gcc -fPIC -g -c -Wall a.c
    • gcc -fPIC -g -c -Wall b.c
    • gcc -shared -W1,-soname, libmystuff.so.1 -o libmystuff.so.1.0.1 a.o b.o -lc
      • 在 Solaris 下可能會發生 “relocations remain against allocatable but non-writable sections and relocation error”,加上 -mimpure-text 和 -shared 一起用。
      • -mimpure-text, used in addition to -shared, tells the compiler to not pass -assert pure-text to the linker when linking a shared object.
  • Link 的方式
    • 不要用 LD_LIBRARY_PATH 環境設定的方式,在於用非系統 lib 要用 suid 者,會有安全方面的問題。
    • Link Environment 的設定方式
      • Linux
        • 開發時,以 root 的身分在 /etc/ld.so.conf 加入開發 library 的路徑(比如 /home/derjohng/libs/),執行 ldconfig。
      • Solaris
        • 使用 crle -u -l <lib path>,就會加到 /var/ld/ld.config
    • ln -s libmystuff.so.1.0.1 libmystuff.so (不然,可能會有 error while loading shared libraries 的訊息)
    • gcc -o test test.c -lmystuff
  • 可參考 Program-Library-HOWTO 

Under Windows (VC++)

  • 在要包成 Dll 的程式檔頭 (.h) 加入下述的宣告

#if _DLL
# ifdef DLL_SRC
# define DLL_EXP __declspec(dllexport)
# else
# define DLL_EXP __declspec(dllimport)
# endif
#else
# define DLL_EXP
#endif

  • 在要包成 Dll 的程式檔 (.cpp) 前加入
    • #define DLL_SRC
  • 並將要 export 出去的 function 和 class 加上 export tag.
    • 比如 class DLL_EXP class_name
  • Compile
    • cl -c a.c -Foa.obj
    • cl -c b.c -Fob.obj
    • link /dll /implib:$c.lib -out:c.dll a.obj b.obj
  • Link
    • link c.lib -out:test.exe test.obj


站內搜尋



本站其他服務

本站其他軟體



  • 臉書粉絲專頁搜尋 (FPSearch)

    不用登入臉書即可搜尋臉書粉絲專頁(臉書粉絲團)所公開的文章。您可以指定特定日期範圍之前的文章,也可設定搜尋粉絲專頁內包含特定關鍵詞的文章。


  • 姓名筆畫吉凶查詢系統

    這是一個提供中文字康熙筆畫的小軟體,並根據農民曆計算每個名字或公司名的總筆畫以及最後的吉凶數,共有四種模式


  • 當令蔬果花卉(AgriInfo)

    是不是常常在超市看到水果蔬菜的價格,總是不確定是當季蔬果?這個服務就是幫你很快判斷眼前的蔬果花卉的價格是否便宜。


  • 條碼掃描器(QRCode)

    支援 QRCode and Barcodes、可連續快速掃描、自動對焦、可打開手電筒供掃描時使用


  • 台灣空污警報(AirInfo)

    設定特定站點為推播通知關注點後,當該站點空氣品質變糟時,即時推播通知給您。另外提供站點附近基本天氣預測資料。


  • 下一班火車 (nextRail)

    這個 app 只要開啟後,就根據定位幫你過濾出最近火車站的時刻表,不用再按任何按鈕了,方便您在很快時間內確定要坐的哪一班火車