這些文章屬於 '程式筆記' 類別

C++ 筆記

Friday, February 17th, 2006
  • Alarm Clock
    • 在 Solaris 下,有時會出現 "ALARM CLOCK",然後中斷。
    • 有可能是 usleep() 等造成的? 不太確定。
    • 方法:略去 SIGALRM 的 signal.
      • signal(SIGALRM, SIG_IGN);
  • 在 DLL 內實作 template class 有煩人的 explicit instantiation 的問題...
    • 參考這裡 
    • 另一篇有關 Template 實作的文章: 這裡 ,有一段 the "traditional" way of organizing source code (declarations in *.h files, and definitions in *.cpp files) does not work with templates,所以,我看到的 template 實作似乎都是整個放在 .h 。
    • 結論在 DLL 內實作 template class 有點煩人。
  • 少用 strdup,用 new/delete
    • 這樣常常會有問題 (不知為什麼?)
      • string pSrc;
      • strdup(pTarget, pSrc.c_str());
      • free(pTarget);
    • 改用這樣就沒問題
      • string pSrc;
      • pTarget = new char[pSrc.length()+1];
      • delete []pTarget;

Shared Libraries

Tuesday, February 7th, 2006
最近寫的程式莫名其妙地越來越肥,所以,花時間研究一下 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

C++ Portable

Monday, January 16th, 2006
看完 http://blog.linux.org.tw/~jserv/archives/001436.html  的文章,再看 C++ portability guide  以及 Writing Portable C++  一文,我一直知道在 porting 時會有許多問題要考量,雖然我目前碰的軟體,號稱在 Windows, Linux, Solaris and AIX,都有產品,但我目前能做到Windows 為主就很累哩,而偶而會遇到Linux,就要辛苦去 porting 一陣子,甚至就想說,能勸說不要碰 Solaris/AIX 來解脫就一定要勸說,just say "No" to bad old platforms :p
要寫能 porting 到不同平台的 Code 真的很痛苦,特別是 thread 方面的東西,當然,這是各平台的呈現都有點不一樣,而特別在 Windows 和其他的平台更是不同,很難想像不用 Qt/Mono 之類的人,寫 GUI
方面不知怎樣寫? 用Java 寫 GUI? 好像更難? 我也寫過一陣子 Java(當然 GUI 不會),但還是覺得 C/C++ 寫的習慣 ^_^

C#中調用Windows API

Thursday, December 15th, 2005
最近要試 C# 去呼叫我原有的 C/C++ Dll API,因為沒寫過 C#,整死我也~~
特別是 wchar_t 要轉成 multi-bytes/utf8 的問題,整理一下,以供備查。
(1) 來自中軟網,主要參考文章