2013年8月17日 星期六

[C++] 印 endl而非 '\n'


  • 問題: 
    • C++中, 平常印訊息用 cout << endl 好, 還是 cout "\n" 好?
  • 解法: 
    • 傾向使用 cout << endl, 原因是, IO的 buffer不會在程式 crash時自動清空 (flush), 也就是有可能一直在錯誤的地方 debug(在程式根本沒執行到的地方 debug, 因為誤以為印出的訊息是正確的), 但 endl中隱含了 flush buffer的動作, 因此可避免此一情形

2013年8月11日 星期日

[Qt] Qt Quick error in re-writing


  • 問題:
    • Qt 利用 Qt Design 設計 UI時, 有時在存檔時會遇到無法更新存檔, 甚至 Qt當掉的問題
    • Qt Quick application在 Build時出現 module QtQuick 1.1 not installed
  • 解法
    • 基本上就是沒有安裝 QtQuick 1.1, 在 main.qml裡頭又 import多個 QtQuick modules造成的, 請檢查 .qml檔的 import設定即可

2013年8月10日 星期六

[CMake] Install command line fail


  • 問題
    • 在安裝 CMake command line tools可能會發生 "Failed create symlink installation may be incomplete" 的錯誤
  • 原因
    • 主要原因是之前安裝過其他版本的 CMake而造成的衝突
  • 解法
    • 自行 link吧!
    • 在 /usr/bin中找到以下相關 binary
      • ccmake
      • cmake
      • cmake-gui
      • cmakexbuild
      • cpack
      • ctest
    • 刪除其 link, 並建立新的link
      • e.g.  sudo ln -s /Applications/CMake\ 2.8-11.app/Contents/bin/cmake /usr/bin/cmake

[Development] Memory leak check -Valgrind


  • 簡介
    • 相信寫 C, C++的人應該不陌生, 該 new空間沒 new, 或是使用完沒 delete, 都會造成 memory的問題, 而 Valgrind便是一套 tool可以幫忙做 memory check
  • 安裝
    • 目前一般 linux已內建, Mac可藉由 MacPort安裝 (sudo port install valgrind)
  • 使用

2013年8月9日 星期五

[Development] qmake


  • 簡介:
    • qmake為 qt提供的 make tool, 可產生不同平台所需的 Makefile
    • .pro 為其特殊的專案檔格式
    • 重要: Qt 專案 build的過程
  • 常用變數:
    • TEMPLATE
      • 可等於 app、lib、subdirs, e.g. TEMPLATE=app, 可用來產生不同的專案類型, 產生的是應用程式、函式庫或子目錄(遞迴式搜尋子資料夾), 預設值為 app
    • HEADERS
      • 設定相關 header files
    • SOURCES
      • 指定所要 build的 c++檔
    • FORMS
      • 指定 Qt Designer產生的 .ui檔, 會呼叫 Qt的工具程式 uic來處理
    • RESOURCES
      • 由程式 rcc來處理 .qrc檔
    • DEFINES
      • 可定義預處理器符號
    • INCLUDEPATH
      • 可指定 C++搜尋目錄, 以設定全域的標頭檔案
    • LIBS
      • 指定連結至專案的函式庫, 可使用絕對路徑或 Unix的 -L, -l flags
    • CONFIG
      • 可設定多種專案與編譯器組態
        • debug
        • release
        • warn_off: default是開啓
        • qt: 預設開啓
        • dll
        • staticlib
        • plugins: 相當於 dll
        • console: 這比較重要, 表示 output需要被寫到 console window, e.g. 使用 cout、cerr等, 對於拿來寫 console application來說, 需設定此項
        • app_bundle、lib_bundle: Mac OS X專用
    • QT
      • 設定所使用的 QT modules, 預設為 core gui (QtCore與 QtGui)
    • VERSION
    • TARGET
      • app 或 lib的名稱, 不包含 prefix, version等
    • DESTDIR
      • build後的目錄
    • DLLdESTDIR
  • 產生特定平台專案檔(假設 Qt專案檔為 hello.pro)
    • General
      • qmke hello.pro, 接著可以用 make or nmake去 build
    • MicroSoft Visual Studio專案檔
      • qmake -tp vc hello.pro
    • Mac OS X 
      • Xcode project
        • qmake -spec macx-xcode hello.pro
      • makefile
        • qmake -spec macx-g++ hello.pro
      • spec選項可以指定"平台-編譯器"的組合, 如上述 macx(平台)-xcode(編譯器)
  • 自動產生 .pro檔
    • qmake -project
      • 會搜尋當前目錄中的 .cpp, .h, .ui檔以產生 .pro
  • 註解: #
  • 相關運算子
    • +=, -=
      • CONFIG = qt
      • CONFIG += release
      • CONFIG -= warn_off
    • *=
      • 當該變數為空, 則 assign, 否則不做任何動作
      • SOURCES *= main.cpp
      • 上式等同於, 若 SOURCES沒指定任何檔案, 則指定為 main.cpp
    • ~=
      • 可替換任何值, 支援 sed語法
      • SOURCES ~= s/\.cpp\b/.cxx/
      • 上式代表將所有 .cpp的附檔名改為 .cxx
  • 存取變數
    • $$variable or $${variable}  -- 在 .pro檔中的值
    • $$(variable) -- 在 qmake執行環境中的環境變數值
    • $(variable) -- makefile 執行環境中的環境變數值
    • $$[variable] -- Qt組態選項值
  • 判斷式
    • 語法
      • condition{
        • then-case
      • }else{
        • else-case
      • }
    • 其中的 condiction可為平台名稱, e.g. win32, unix, or macx, 例如:
      • win32{
        • SOURCES += serial_win.cpp
      • }else{
        • SOURCES += serial_unix.cpp
      • }
    • 單行式
      • macx: SOURCES += serial_mac.cpp
  • project include
    • include(../common.pri)
  • 利用 CMake 產生 Qt應用程式

2013年8月3日 星期六

[C++] 慣用簡潔的 pointer算式

int *p, *q分別指向 source array與 destination array, 若要 copy, 可用 *q++=*p++;

如此直接完成了三個動作:
*q=*p;
q++;
p++;