qmake工具生成项目文件与Makefile文件

    技术2023-11-02  110

    qmake

    是一个协助简化跨平台进行专案开发的构建过程的工具程式,也是Qt附带工具之一。可以根据项目环境构建.pro项目文件,再根据项目文件生成Makefile文件。相对于手写一个项目Makefile来说,使用qmake生成Makefile比较简便。

    查看qmake

    Ubuntu下已经安装好Qt

    qmake -v

    可能会出现错误

    qmake: could not exec ‘/usr/lib/x86_64-linux-gnu/qt4/bin/qmake’: No such file or directory

    是因为在/usr/lib/x86_64-linux-gnu/qt4/bin/qmake这个目录下没有qmake程式文件。 找到路径:/usr/lib/x86_64-linux-gnu/qt-default/qtchooser

    vim default.conf

    将第一行:/usr/lib/x86_64-linux-gnu/qt4/bin/qmake改成自己Qt安装的路径/opt/Qt5.5.0/5.5/gcc_64/bin 再执行

    qmake -v

    可以看到qt版本。

    查看qmake帮助文档

    qmake - h Usage: /opt/Qt5.5.0/5.5/gcc_64/bin/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 *; *; *; *.ts; *.xlf; *.qrc 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]

    模式有两种: -project:根据工程生成.pro项目文件。 -makefile:根据.pro项目文件生成makefile文件。

    qmake生成makefile文件简单测试

    创建一个test工程目录,目录下有三个目录分别是:a,b,m;a目录下有a.c,a.h;b目录下有b.c,b.h;m目录下有main.c。

    a.c

    #include <stdio.h> #include "a.h" int a(void) { printf("call a!\n"); return 0; }

    a.h

    #ifndef _A_H #define _A_H int a(void); #endif

    b.c

    #include <stdio.h> #include "b.h" int b(void) { printf("call b!\n"); return 0; }

    b.h

    #ifndef _B_H #define _B_H int b(void); #endif

    main.c

    #include <stdio.h> #include "../a/a.h" #include "../b/b.h" int main(int argc, char ** argv) { if(argc > 1){ printf("%s\n",argv[1]); } printf("hello, linux world!\n"); a(); b(); return 0; }

    在test目录下生成.pro

    qmake -project

    生成Makefile

    qmake -makefile test.pro -o Makefile

    make生成执行文件

    Processed: 0.009, SQL: 9