Qt編程中qmake的使用詳解


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

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

 

  1. Usage: qmake [mode] [options] [files]  
  2.   
  3. QMake has two modes, one mode for generating project files based on  
  4. some heuristics, and the other for generating makefiles. Normally you  
  5. shouldn't need to specify a mode, as makefile generation is the default  
  6. mode for qmake, but you may use this to test qmake on an existing project  
  7.   
  8. Mode:  
  9.   -project       Put qmake into project file generation mode  
  10.                  In this mode qmake interprets files as files to  
  11.                  be built,  
  12.                  defaults to *.c; *.ui; *.y; *.l; *.ts; *.xlf; *.qrc; *.h; *.hpp; *.hh; *.hxx; *.H; *.cpp; *.cc; *.cxx; *.C  
  13.                  Note: The created .pro file probably will   
  14.                  need to be edited. For example add the QT variable to   
  15.                  specify what modules are required.  
  16.   -makefile      Put qmake into makefile generation mode (default)  
  17.                  In this mode qmake interprets files as project files to  
  18.                  be processed, if skipped qmake will try to find a project  
  19.                  file in your current working directory  
  20.   
  21. Warnings Options:  
  22.   -Wnone         Turn off all warnings; specific ones may be re-enabled by  
  23.                  later -W options  
  24.   -Wall          Turn on all warnings  
  25.   -Wparser       Turn on parser warnings  
  26.   -Wlogic        Turn on logic warnings (on by default)  
  27.   -Wdeprecated   Turn on deprecation warnings (on by default)  
  28.   
  29. Options:  
  30.    * You can place any variable assignment in options and it will be     *  
  31.    * processed as if it was in [files]. These assignments will be parsed *  
  32.    * before [files].                                                     *  
  33.   -o file        Write output to file  
  34.   -d             Increase debug level  
  35.   -t templ       Overrides TEMPLATE as templ  
  36.   -tp prefix     Overrides TEMPLATE so that prefix is prefixed into the value  
  37.   -help          This help  
  38.   -v             Version information  
  39.   -after         All variable assignments after this will be  
  40.                  parsed after [files]  
  41.   -norecursive   Don't do a recursive search  
  42.   -recursive     Do a recursive search  
  43.   -set <prop> <value> Set persistent property  
  44.   -unset <prop>  Unset persistent property  
  45.   -query <prop>  Query persistent property. Show all if <prop> is empty.  
  46.   -cache file    Use file as cache           [makefile mode only]  
  47.   -spec spec     Use spec as QMAKESPEC       [makefile mode only]  
  48.   -nocache       Don't use a cache file      [makefile mode only]  
  49.   -nodepend      Don't generate dependencies [makefile mode only]  
  50.   -nomoc         Don't generate moc targets  [makefile mode only]  
  51.   -nopwd         Don't look for files in pwd [project mode only]  
  52. song@ubuntu:~/lianxi/qt$ clear  
  53.   
  54. song@ubuntu:~/lianxi/qt$ qmake -help  
  55. Usage: qmake [mode] [options] [files]  
  56.   
  57. QMake has two modes, one mode for generating project files based on  
  58. some heuristics, and the other for generating makefiles. Normally you  
  59. shouldn't need to specify a mode, as makefile generation is the default  
  60. mode for qmake, but you may use this to test qmake on an existing project  
  61.   
  62. Mode:  
  63.   -project       Put qmake into project file generation mode  
  64.                  In this mode qmake interprets files as files to  
  65.                  be built,  
  66.                  defaults to *.c; *.ui; *.y; *.l; *.ts; *.xlf; *.qrc; *.h; *.hpp; *.hh; *.hxx; *.H; *.cpp; *.cc; *.cxx; *.C  
  67.                  Note: The created .pro file probably will   
  68.                  need to be edited. For example add the QT variable to   
  69.                  specify what modules are required.  
  70.   -makefile      Put qmake into makefile generation mode (default)  
  71.                  In this mode qmake interprets files as project files to  
  72.                  be processed, if skipped qmake will try to find a project  
  73.                  file in your current working directory  
  74.   
  75. Warnings Options:  
  76.   -Wnone         Turn off all warnings; specific ones may be re-enabled by  
  77.                  later -W options  
  78.   -Wall          Turn on all warnings  
  79.   -Wparser       Turn on parser warnings  
  80.   -Wlogic        Turn on logic warnings (on by default)  
  81.   -Wdeprecated   Turn on deprecation warnings (on by default)  
  82.   
  83. Options:  
  84.    * You can place any variable assignment in options and it will be     *  
  85.    * processed as if it was in [files]. These assignments will be parsed *  
  86.    * before [files].                                                     *  
  87.   -o file        Write output to file  
  88.   -d             Increase debug level  
  89.   -t templ       Overrides TEMPLATE as templ  
  90.   -tp prefix     Overrides TEMPLATE so that prefix is prefixed into the value  
  91.   -help          This help  
  92.   -v             Version information  
  93.   -after         All variable assignments after this will be  
  94.                  parsed after [files]  
  95.   -norecursive   Don't do a recursive search  
  96.   -recursive     Do a recursive search  
  97.   -set <prop> <value> Set persistent property  
  98.   -unset <prop>  Unset persistent property  
  99.   -query <prop>  Query persistent property. Show all if <prop> is empty.  
  100.   -cache file    Use file as cache           [makefile mode only]  
  101.   -spec spec     Use spec as QMAKESPEC       [makefile mode only]  
  102.   -nocache       Don't use a cache file      [makefile mode only]  
  103.   -nodepend      Don't generate dependencies [makefile mode only]  
  104.   -nomoc         Don't generate moc targets  [makefile mode only]  
  105.   -nopwd         Don't look for files in pwd [project mode only]  
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代碼如下

  

  1. #include<qapplication.h>  
  2. #include<qpushbutton.h>  
  3.   
  4. int main(int argc,char *argv[])  
  5. {  
  6.     QApplication a(argc,argv);  
  7.     QPushButton hellobtn("Hello World!",0);  
  8.     hellobtn.resize(200,50);  
  9.     hellobtn.show();  
  10.     return a.exec();  
  11. }  
#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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM