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


站內搜尋



本站其他服務

本站其他軟體



  • 國道一路通(FreeWay)

    打開定位即實際地圖上繪製所在地中心附近的車況圖,可縮放地圖,不再是小小的縮小圖,快速了解高速公路的路況。


  • 標案快訊

    讓你可以輕鬆追蹤含有您想要關注關鍵詞的任何採購標案,只要有最新的資訊,「標案快訊」即會推播通知給你.


  • 下一班公車(nextBus)

    這個 app 只要開啟後,就根據定位幫你過濾出附近站牌的時刻表,以及提供相關公車預計到站的時間,方便您在很快時間內確定要坐的哪一班公車


  • 油價快訊App (OilPrices)

    依據油價及匯率,估算台灣下週油價,另外提供三週、一年以及三年的歷史變化,以及週末下午推播通知最新油價預估或公告。


  • 照片去背(PhotoEraser)

    一款方便移除背景的工具,產生透明背景圖可以存回原本相簿,也可分享到其他 App 使用.


  • 台灣空污警報(AirInfo)

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