Skip to content

Zeroplex 生活隨筆

軟體開發和生活瑣事

小 縮小字型大小。 中 重設字型大小。 大 放大字型大小。

標籤: 程式設計

qmake to cmake

Posted on 2012 年 4 月 7 日2021 年 3 月 12 日 By 日落 在〈qmake to cmake〉中尚無留言
qmake to cmake

以 Qt Creator 預設的 GUI Appplication 為例:

zero@zero-desktop:~/dev$ tree example/
example/
|-- example.pro
|-- main.cpp
|-- mainwindow.cpp
|-- mainwindow.h
`-- mainwindow.ui

建立 CMakeLists.txt,先寫入最常用到的幾個設定:

project( example )
cmake_minimum_required(VERSION 2.6)

set( SRC main.cpp mainwindow.cpp )
set( HEADER mainwindow.h )

讓 cmake 先載入 Qt 會用到參數:

find_package( Qt4 REQUIRED )

加入 Qt 函式庫位置:

include( ${QT_USE_FILE} )
add_definitions( ${QT_DEFINITIONS} )

因為 Qt 在編譯前會先透過 moc、uic 等對程式做處理,將 SIGNAL/SLOT、ui 轉成程式碼再做編譯。Cmake 透過 wrapper 做設定:

set( FORM mainwindow.ui )

qt4_wrap_cpp( HEADER_MOC ${HEADER} )
qt4_wrap_ui( FORM_HEADER ${FORM} )

如果專案中有使用到 qrc 檔,則需加入:

qt4_add_resources( RESOURCES_RCC resource.qrc )

有時編譯位置與程式碼放置位置不同,編譯器會讀不到 moc 轉出來的程式碼,所以要將編譯位置加入:

include_directories( ${CMAKE_CURRENT_BINARY_DIR} )

最後,編譯設定:

add_executable( exe ${SRC} ${HEADER_MOC} ${FORM_HEADER} )
target_link_libraries( exe ${QT_LIBRARIES} )

完整的 CMakeLists.txt 大致上長這樣:

project( example )
cmake_minimum_required(VERSION 2.6)
find_package( Qt4 REQUIRED )

include( ${QT_USE_FILE} )
add_definitions( ${QT_DEFINITIONS} )

set( SRC main.cpp mainwindow.cpp )
set( HEADER mainwindow.h )
set( FORM mainwindow.ui )

qt4_wrap_cpp( HEADER_MOC ${HEADER} )
qt4_wrap_ui( FORM_HEADER ${FORM} )

include_directories( ${CMAKE_CURRENT_BINARY_DIR} )

add_executable( exe ${SRC} ${HEADER_MOC} ${FORM_HEADER} )
target_link_libraries( exe ${QT_LIBRARIES} )

建立 Makefile:

zero@zero-desktop:~/dev/example$ mkdir build
zero@zero-desktop:~/dev/example$ cd build/
zero@zero-desktop:~/dev/example/build$ cmake ..
-- The C compiler identification is GNU
-- The CXX compiler identification is GNU
-- Check for working C compiler: /usr/bin/gcc
-- Check for working C compiler: /usr/bin/gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Looking for Q_WS_X11
-- Looking for Q_WS_X11 - found
-- Looking for Q_WS_WIN
-- Looking for Q_WS_WIN - not found.
-- Looking for Q_WS_QWS
-- Looking for Q_WS_QWS - not found.
-- Looking for Q_WS_MAC
-- Looking for Q_WS_MAC - not found.
-- Found Qt-Version 4.6.2 (using /usr/bin/qmake)
-- Looking for _POSIX_TIMERS
-- Looking for _POSIX_TIMERS - found
-- Configuring done
-- Generating done
-- Build files have been written to: /home/zero/dev/example/build

沒有錯誤訊息的話,目錄下應該就會有 Makefile 了,接下來 make 吧!

Reference:
Using CMake to Build Qt Projects | Qt Developer Network
http://qt-project.org/quarterly/view/using_cmake_to_build_qt_projects

「 」: CMake to QMake note
https://b2.zeroplex.tw/2009/12/cmake-to-qmake-note.html

Tags:Qt, 程式設計

「Code Smart, Don’t Code hard」by CrBoy

Posted on 2012 年 4 月 5 日2021 年 3 月 12 日 By 日落 在〈「Code Smart, Don’t Code hard」by CrBoy〉中尚無留言

備註:CrBoy’s Blog:「那些老師沒教的事」簡報釋出

其中印象最深刻的應該是傻傻的用 printf 除錯,如果只是一般型態還好,遇到物件等之資料型態用 printf 真會把自己搞死。

另外把壓縮檔塞進版本庫裡面也很經典!SVN 或是 git 等版本管理工具其中一大功能就是比對 code 的異動,程式碼壓縮後這些工具就沒辦法做比對。誇張的是有人還會好心的把 *.o、*.exe 一起打包 commit …. XD

ps. 用 cmake 好像還蠻威的!

Tags:C/C++, 分享, 程式設計

SQLite 單引號處理

Posted on 2012 年 4 月 5 日2021 年 3 月 12 日 By 日落 在〈SQLite 單引號處理〉中尚無留言

一般處理 SQL 查詢時,遇到單引號或雙引號都會加上反斜線做跳脫。不過字串是餵給 SQLite 時,處理單引號的方式不同,是將一個單引號改為二個。

例如:

select * from content where text like '%Zero's note%'

上述字串「Zero’s note」中的單引號會造成查詢錯誤,所以將單引號修改後成為:

select * from content where text like '%Zero''s note%'

Reference:
How do I use a string literal that contains an embedded single-quote (‘) character?

Tags:程式設計, 資訊學習

for 迴圈

Posted on 2011 年 12 月 24 日2021 年 3 月 12 日 By 日落 在〈for 迴圈〉中尚無留言

以前用 for 迴圈跑字串,都要寫好常一串:

char s[5]="123";
int index = 0;
while( s[index] != '' ){
   // get s[index];
   index++;
}

無意間看到有人這樣寫:

char s[5]="123";
int index;
for(index=0; s[index]; index++){
   // get s[index];
}

原因是 會被當作 false,換做其他資料型別的陣列就沒辦法這樣寫。

Tags:C/C++, 程式設計

PHP Closing Tags

Posted on 2011 年 12 月 23 日2021 年 3 月 12 日 By 日落 在〈PHP Closing Tags〉中尚無留言

一般寫 PHP 會將系統設定、資料庫連結等功能分開放在多個不同的檔案中。

index.php:

<?php
   require('settings.php');

   // something else

   session_start();
?>

settings.php:

<?php
   // some settings
?>
 

以上程式執行以後會噴錯誤訊息:
session_start(): Cannot send session cache limiter - headers already sent
原因是在 index.php 呼叫 session_start() 之前已經輸出資料,不過到底是哪裡有輸出資料?仔細看才會發現 settings.php 在「?>」後方還換了一行,換行是在 PHP closing tag 之外,所以就被當作一般資料送出。

當程式很龐大時,這種 bug 實在會讓人摔鍵盤、丟滑鼠、凹螢幕。

事實上 PHP 程式不加上結尾的「?>」也是可以執行的,直譯器到檔案結尾會自動當作該程式段落結束,所以程式可以改寫成這樣:

index.php

<?php
   require('settings.php');

   // something else

   session_start();
 

settings.php

<?php
   // some settings
 
 
Tags:PHP, 程式設計

文章分頁

上一頁 1 ... 3 4 5 ... 15 下一頁

其他

關於我  (About me)

  文章 RSS Feed

  留言 RSS Feed

Apache AWS Bash C/C++ Docker FreeBSD GCP Git Google Java JavaScript Laravel Linux Microsoft MSSQL MySQL Nginx PHP PHPUnit PostgreSQL Python Qt Raspberry Pi Ubuntu Unix Vim Web Windows XD 作業系統 分享 好站推薦 專題 攝影 新奇搞笑 新聞 旅遊 生活雜記 程式設計 網路架站 網頁設計 資訊學習 資訊安全 遊戲 音樂


創用 CC 授權條款
本著作係採用創用 CC 姓名標示-相同方式分享 4.0 國際 授權條款授權.