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

原文地址:https://www.cnblogs.com/wwwbdabc/p/11677260.html