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


站內搜尋



本站其他服務

本站其他軟體



  • 下一班火車 (nextRail)

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


  • 批踢踢快訊 (pttNews)

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


  • 標案快訊

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


  • 台灣空污警報(AirInfo)

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


  • 藝文快訊

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


  • 下一班高鐵 (nextTHSR)

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