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


站內搜尋



本站其他服務

本站其他軟體



  • 條碼掃描器(QRCode)

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


  • 當令蔬果花卉(AgriInfo)

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


  • 批踢踢快訊 (pttNews)

    身為鄉民的您,是不是常常覺得現在的批踢踢 (PTT) 閱讀器,明明您就只要看幾個板,都幫您分類好,但不是您要的?甚至您只想關注某個人(或某件事)的消息,卻散布在不同東西,找出來很辛苦? 這個 App 可以解決您這些問題,快來用吧!


  • 國道一路通(FreeWay)

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


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

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


  • 照片去背(PhotoEraser)

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