1. 程式人生 > 實用技巧 >Qt程式設計中qmake的使用詳解

Qt程式設計中qmake的使用詳解

Qt程式設計中qmake的使用詳解

首先說一下qt程式設計的步驟,然後再仔細說一下qmake的使用。我看書上的都是編寫好cpp檔案後,分別執行命令: qmake -project、qmake、make,然後執行就可以了,但是要是我在一個資料夾下有多個cpp檔案,qmake怎麼識別?make生成的最後執行檔案的名,我怎麼能隨心所欲的自己定?帶著疑問,找了網上的資料,不如所衣,然後嘗試著gcc編譯的思想竟然撞到了。

其實我感覺要要真正瞭解qmake的使用方法,直接在終端下輸入命令:qmake -help就可以了,我也是這樣做的,幫助內容如下

[plain]view plain
copyprint?
  1. Usage:qmake[mode][options][files]
  2. QMakehastwomodes,onemodeforgeneratingprojectfilesbasedon
  3. someheuristics,andtheotherforgeneratingmakefiles.Normallyyou
  4. shouldn'tneedtospecifyamode,asmakefilegenerationisthedefault
  5. modeforqmake,butyoumayusethistotestqmakeonanexistingproject
  6. Mode:
  7. -projectPutqmakeintoprojectfilegenerationmode
  8. Inthismodeqmakeinterpretsfilesasfilesto
  9. bebuilt,
  10. defaultsto*.c;*.ui;*.y;*.l;*.ts;*.xlf;*.qrc;*.h;*.hpp;*.hh;*.hxx;*.H;*.cpp;*.cc;*.cxx;*.C
  11. Note:Thecreated.profileprobablywill
  12. needtobeedited.ForexampleaddtheQTvariableto
  13. specifywhatmodulesarerequired.
  14. -makefilePutqmakeintomakefilegenerationmode(default)
  15. Inthismodeqmakeinterpretsfilesasprojectfilesto
  16. beprocessed,ifskippedqmakewilltrytofindaproject
  17. fileinyourcurrentworkingdirectory
  18. WarningsOptions:
  19. -WnoneTurnoffallwarnings;specificonesmaybere-enabledby
  20. later-Woptions
  21. -WallTurnonallwarnings
  22. -WparserTurnonparserwarnings
  23. -WlogicTurnonlogicwarnings(onbydefault)
  24. -WdeprecatedTurnondeprecationwarnings(onbydefault)
  25. Options:
  26. *Youcanplaceanyvariableassignmentinoptionsanditwillbe*
  27. *processedasifitwasin[files].Theseassignmentswillbeparsed*
  28. *before[files].*
  29. -ofileWriteoutputtofile
  30. -dIncreasedebuglevel
  31. -ttemplOverridesTEMPLATEastempl
  32. -tpprefixOverridesTEMPLATEsothatprefixisprefixedintothevalue
  33. -helpThishelp
  34. -vVersioninformation
  35. -afterAllvariableassignmentsafterthiswillbe
  36. parsedafter[files]
  37. -norecursiveDon'tdoarecursivesearch
  38. -recursiveDoarecursivesearch
  39. -set<prop><value>Setpersistentproperty
  40. -unset<prop>Unsetpersistentproperty
  41. -query<prop>Querypersistentproperty.Showallif<prop>isempty.
  42. -cachefileUsefileascache[makefilemodeonly]
  43. -specspecUsespecasQMAKESPEC[makefilemodeonly]
  44. -nocacheDon'tuseacachefile[makefilemodeonly]
  45. -nodependDon'tgeneratedependencies[makefilemodeonly]
  46. -nomocDon'tgeneratemoctargets[makefilemodeonly]
  47. -nopwdDon'tlookforfilesinpwd[projectmodeonly]
  48. song@ubuntu:~/lianxi/qt$clear
  49. song@ubuntu:~/lianxi/qt$qmake-help
  50. Usage:qmake[mode][options][files]
  51. QMakehastwomodes,onemodeforgeneratingprojectfilesbasedon
  52. someheuristics,andtheotherforgeneratingmakefiles.Normallyyou
  53. shouldn'tneedtospecifyamode,asmakefilegenerationisthedefault
  54. modeforqmake,butyoumayusethistotestqmakeonanexistingproject
  55. Mode:
  56. -projectPutqmakeintoprojectfilegenerationmode
  57. Inthismodeqmakeinterpretsfilesasfilesto
  58. bebuilt,
  59. defaultsto*.c;*.ui;*.y;*.l;*.ts;*.xlf;*.qrc;*.h;*.hpp;*.hh;*.hxx;*.H;*.cpp;*.cc;*.cxx;*.C
  60. Note:Thecreated.profileprobablywill
  61. needtobeedited.ForexampleaddtheQTvariableto
  62. specifywhatmodulesarerequired.
  63. -makefilePutqmakeintomakefilegenerationmode(default)
  64. Inthismodeqmakeinterpretsfilesasprojectfilesto
  65. beprocessed,ifskippedqmakewilltrytofindaproject
  66. fileinyourcurrentworkingdirectory
  67. WarningsOptions:
  68. -WnoneTurnoffallwarnings;specificonesmaybere-enabledby
  69. later-Woptions
  70. -WallTurnonallwarnings
  71. -WparserTurnonparserwarnings
  72. -WlogicTurnonlogicwarnings(onbydefault)
  73. -WdeprecatedTurnondeprecationwarnings(onbydefault)
  74. Options:
  75. *Youcanplaceanyvariableassignmentinoptionsanditwillbe*
  76. *processedasifitwasin[files].Theseassignmentswillbeparsed*
  77. *before[files].*
  78. -ofileWriteoutputtofile
  79. -dIncreasedebuglevel
  80. -ttemplOverridesTEMPLATEastempl
  81. -tpprefixOverridesTEMPLATEsothatprefixisprefixedintothevalue
  82. -helpThishelp
  83. -vVersioninformation
  84. -afterAllvariableassignmentsafterthiswillbe
  85. parsedafter[files]
  86. -norecursiveDon'tdoarecursivesearch
  87. -recursiveDoarecursivesearch
  88. -set<prop><value>Setpersistentproperty
  89. -unset<prop>Unsetpersistentproperty
  90. -query<prop>Querypersistentproperty.Showallif<prop>isempty.
  91. -cachefileUsefileascache[makefilemodeonly]
  92. -specspecUsespecasQMAKESPEC[makefilemodeonly]
  93. -nocacheDon'tuseacachefile[makefilemodeonly]
  94. -nodependDon'tgeneratedependencies[makefilemodeonly]
  95. -nomocDon'tgeneratemoctargets[makefilemodeonly]
  96. -nopwdDon'tlookforfilesinpwd[projectmodeonly]
Usage: qmake [mode] [options] [files]

QMake has two modes, one mode for generating project files based on
some heuristics, and the other for generating makefiles. Normally you
shouldn't need to specify a mode, as makefile generation is the default
mode for qmake, but you may use this to test qmake on an existing project

Mode:
  -project       Put qmake into project file generation mode
                 In this mode qmake interprets files as files to
                 be built,
                 defaults to *.c; *.ui; *.y; *.l; *.ts; *.xlf; *.qrc; *.h; *.hpp; *.hh; *.hxx; *.H; *.cpp; *.cc; *.cxx; *.C
                 Note: The created .pro file probably will 
                 need to be edited. For example add the QT variable to 
                 specify what modules are required.
  -makefile      Put qmake into makefile generation mode (default)
                 In this mode qmake interprets files as project files to
                 be processed, if skipped qmake will try to find a project
                 file in your current working directory

Warnings Options:
  -Wnone         Turn off all warnings; specific ones may be re-enabled by
                 later -W options
  -Wall          Turn on all warnings
  -Wparser       Turn on parser warnings
  -Wlogic        Turn on logic warnings (on by default)
  -Wdeprecated   Turn on deprecation warnings (on by default)

Options:
   * You can place any variable assignment in options and it will be     *
   * processed as if it was in [files]. These assignments will be parsed *
   * before [files].                                                     *
  -o file        Write output to file
  -d             Increase debug level
  -t templ       Overrides TEMPLATE as templ
  -tp prefix     Overrides TEMPLATE so that prefix is prefixed into the value
  -help          This help
  -v             Version information
  -after         All variable assignments after this will be
                 parsed after [files]
  -norecursive   Don't do a recursive search
  -recursive     Do a recursive search
  -set <prop> <value> Set persistent property
  -unset <prop>  Unset persistent property
  -query <prop>  Query persistent property. Show all if <prop> is empty.
  -cache file    Use file as cache           [makefile mode only]
  -spec spec     Use spec as QMAKESPEC       [makefile mode only]
  -nocache       Don't use a cache file      [makefile mode only]
  -nodepend      Don't generate dependencies [makefile mode only]
  -nomoc         Don't generate moc targets  [makefile mode only]
  -nopwd         Don't look for files in pwd [project mode only]
song@ubuntu:~/lianxi/qt$ clear

song@ubuntu:~/lianxi/qt$ qmake -help
Usage: qmake [mode] [options] [files]

QMake has two modes, one mode for generating project files based on
some heuristics, and the other for generating makefiles. Normally you
shouldn't need to specify a mode, as makefile generation is the default
mode for qmake, but you may use this to test qmake on an existing project

Mode:
  -project       Put qmake into project file generation mode
                 In this mode qmake interprets files as files to
                 be built,
                 defaults to *.c; *.ui; *.y; *.l; *.ts; *.xlf; *.qrc; *.h; *.hpp; *.hh; *.hxx; *.H; *.cpp; *.cc; *.cxx; *.C
                 Note: The created .pro file probably will 
                 need to be edited. For example add the QT variable to 
                 specify what modules are required.
  -makefile      Put qmake into makefile generation mode (default)
                 In this mode qmake interprets files as project files to
                 be processed, if skipped qmake will try to find a project
                 file in your current working directory

Warnings Options:
  -Wnone         Turn off all warnings; specific ones may be re-enabled by
                 later -W options
  -Wall          Turn on all warnings
  -Wparser       Turn on parser warnings
  -Wlogic        Turn on logic warnings (on by default)
  -Wdeprecated   Turn on deprecation warnings (on by default)

Options:
   * You can place any variable assignment in options and it will be     *
   * processed as if it was in [files]. These assignments will be parsed *
   * before [files].                                                     *
  -o file        Write output to file
  -d             Increase debug level
  -t templ       Overrides TEMPLATE as templ
  -tp prefix     Overrides TEMPLATE so that prefix is prefixed into the value
  -help          This help
  -v             Version information
  -after         All variable assignments after this will be
                 parsed after [files]
  -norecursive   Don't do a recursive search
  -recursive     Do a recursive search
  -set <prop> <value> Set persistent property
  -unset <prop>  Unset persistent property
  -query <prop>  Query persistent property. Show all if <prop> is empty.
  -cache file    Use file as cache           [makefile mode only]
  -spec spec     Use spec as QMAKESPEC       [makefile mode only]
  -nocache       Don't use a cache file      [makefile mode only]
  -nodepend      Don't generate dependencies [makefile mode only]
  -nomoc         Don't generate moc targets  [makefile mode only]
  -nopwd         Don't look for files in pwd [project mode only]

qmake命令格式

qmake [mode] [options] [files]

mode選項

-project 生成.pro檔案

-makefile 生成Makefile檔案

options選項(這裡介紹幾個常用的,其它的自己去翻譯上面,其實我也是剛學,用到的時候再學習哈)

-o file 輸出檔名,比如qmake -project hello.cpp -o hello.pro,就會生成一個hello.pro檔案,如果是qmake -project hello.cpp -o hello11.pro,就會生成一個hello11.pro檔案

實驗1

1、編寫程式碼,命名為hello.cpp,如下

hello.cpp程式碼如下

[cpp]view plaincopyprint?
  1. #include<qapplication.h>
  2. #include<qpushbutton.h>
  3. intmain(intargc,char*argv[])
  4. {
  5. QApplicationa(argc,argv);
  6. QPushButtonhellobtn("HelloWorld!",0);
  7. hellobtn.resize(200,50);
  8. hellobtn.show();
  9. returna.exec();
  10. }
#include<qapplication.h>
#include<qpushbutton.h>

int main(int argc,char *argv[])
{
    QApplication a(argc,argv);
    QPushButton hellobtn("Hello World!",0);
    hellobtn.resize(200,50);
    hellobtn.show();
    return a.exec();
}

2、qmake -project(用於建立.pro檔案,將所有的檔案編譯成一個與平臺無關的工程檔案)

可見預設生成的檔名為 qt.pro

3、qmake(讀取本身的Qt設定,生成與庫一致的相應的Makefile)

4、make(根據生成的Makefile,將檔案編譯為二進位制可執行程式)

可見生成了qt可執行程式

5、執行命令:./qt

實驗2

本實驗中並沒有用到上面所講的方式,而是直接使用了預設值,但是假設說該資料夾下有兩個cpp檔案,如下圖

這時候再執行命令:qmake -project會怎麼樣呢?會出現下面的結果

有點意思,竟然不出錯,但是你知道它生成的qt.pro是哪個cpp檔案的嗎?我不知道,加入這裡我想對test.cpp操作,這時候就要按照上面的格式了。

1、qmake -project test.cpp -o test.pro

這時候生成了test.pro

2、qmake -makefile test.pro,這裡生成test.pro的Makefile

3、make

4、執行命令 ./test