来自:https://www.cnblogs.com/nuoforever/p/14005426.html
使用qmake构建项目时,需要在make时拷贝一些文件到指定位置,非执行make install,下面总结列举一下我了解的方式
示例pro
COPY_DIR = $$PWD/copys cp_kk.files += \ aaa \ bbb cp_kk.path += $$INSTALL_DIR/copys COPIES += cp_kkk
执行qmake后查看Makefile文件
COPY = cp -f COPY_FILE = cp -f COPY_DIR = cp -f -R distdir: FORCE @test -d $(DISTDIR) || mkdir -p $(DISTDIR) $(COPY_FILE) --parents $(DIST) $(DISTDIR)/ $(COPY_FILE) --parents /home/XXX/Qt/mkspecs/features/data/dummy.cpp $(DISTDIR)/ $(COPY_FILE) --parents main.cpp $(DISTDIR)/ $(COPY_FILE) --parents aaa bbb $(DISTDIR)/compiler_copy_cp_kk_make_all: copys/aaa copys/bbbcopys/aaa: aaa$(QINSTALL) aaa copys/aaacopys/bbb: bbb$(QINSTALL) bbb copys/bbb
另外执行完qmake之后就可以看到创建了copys目录,但是在make前目录是空的。make之后,文件aaa和bbb被拷贝过来了。
示例pro
PRE_DIR = $$PWD/pre QMAKE_PRE_LINK += $$QMAKE_COPY_FILE $$PWD/ccc $$PRE_DIR POST_DIR = $$PWD/post QMAKE_POST_LINK += $$QMAKE_COPY_FILE $$PWD/ddd $$POST_DIR
查看Makefile
cp -f /home/XXX/testpro/ccc /home/XXX/testpro/pre cp -f /home/XXX/testpro/ddd /home/XXX/testpro/post
这个示例中需要在当前目录下有pre和post两个目录,如果没有的话make会报错。执行qmake和make后可以看到两个文件已经分别拷贝了。
QMAKE_PRE_LINK和QMAKE_POST_LINK的功能远不止于此,后面可以跟更多的命令
它类似于QMAKE_PRE_LINK和QMAKE_POST_LINK,功能也是非常的丰富。
示例pro
CPEXT_DIR = $$PWD/extra cp_ext.commands += $$QMAKE_COPY_FILE $$PWD/eee $$CPEXT_DIR QMAKE_EXTRA_TARGETS += cp_ext PRE_TARGETDEPS += cp_ext
执行qmake,查看Makefile文件
cp_ext: cp -f /home/XXX/testpro/eee /home/XXX/testpro/extra $(TARGET): cp_ext
执行make后可以看到文件eee已经拷贝成功。
示例pro
cp_et.input = $$PWD/fff cp_et.output = $$CPEXT_DIR/ggg QMAKE_SUBSTITUTES += cp_et
执行qmake和make后可以看到文件已经复制并改名。
==============================
QMAKE_COPY 不同平台对应的命令
来源 https://stackoverflow.com/questions/20324061/where-are-variables-such-as-mkdir-and-copy-dir-defined
Before a .pro file is processed and your Makefiles are generated by qmake several other files are pre-processed based on your compiler and platform. These files have the extension .prf and .conf and are loaded from a directory called mkspecs.
The values of MKDIR and COPY_DIR variables in your Makefiles are generated by the values of QMAKE_MKDIR and QMAKE_COPY_DIR variables defined in the following files:
C:\Qt\Qt5.0.2\5.0.2\msvc2010\mkspecs\common\shell-unix.conf:
QMAKE_TAR = tar -cf
QMAKE_GZIP = gzip -9f
QMAKE_COPY = cp -f
QMAKE_COPY_FILE = $$QMAKE_COPY
QMAKE_COPY_DIR = $$QMAKE_COPY -R
QMAKE_MOVE = mv -f
QMAKE_DEL_FILE = rm -f
QMAKE_DEL_DIR = rmdir
QMAKE_CHK_EXISTS = test -e %1 ||
QMAKE_CHK_DIR_EXISTS = test -d # legacy
QMAKE_MKDIR = mkdir -p # legacy
QMAKE_MKDIR_CMD = test -d %1 || mkdir -p %1
QMAKE_STREAM_EDITOR = sed
C:\Qt\Qt5.0.2\5.0.2\msvc2010\mkspecs\common\shell-win32.conf:
QMAKE_ZIP = zip -r -9
QMAKE_COPY = copy /y
QMAKE_COPY_DIR = xcopy /s /q /y /i
QMAKE_MOVE = move
QMAKE_DEL_FILE = del
QMAKE_DEL_DIR = rmdir
QMAKE_CHK_EXISTS = if not exist %1
QMAKE_CHK_DIR_EXISTS = if not exist # legacy
QMAKE_MKDIR = mkdir # legacy
QMAKE_MKDIR_CMD = if not exist %1 mkdir %1 & if not exist %1 exit 1
# xcopy copies the contained files if source is a directory. Deal with it.
CONFIG += copy_dir_files
(As you can see I am using Qt 5.0.2 so the path might be different on your machine.)
==========================qmake建立文件夹和条件判断=================
1、qmake如何建立文件夹?
上面有命令QMAKE_MKDIR和QMAKE_MKDIR_CMD,看下怎么调用:
!exists($$DESTDIR/redistribute){ QMAKE_POST_LINK += & $$quote($$QMAKE_MKDIR $$shell_path($$DESTDIR/redistribute) ) }
注意,如果不加exists,第一次没问题,如果windows文件夹存在那就会报错了,不像linux那样mkdir -p 不报错。
注意上面多了一个 & 号,那是因为有多条postlink命令,如果只有一条,那么 & 不要。
另外 QMAKE_MKDIR_CMD 创建不成功,要问qt那帮人怎么用的。
2、qmake如何实现条件判断?
共有2种方法:
1)、DEFINES 大法: DEFINES += FFMPEG_4_2_2 contains(DEFINES, FFMPEG_4_0_2){ message("ffmpeg4.0.2 64bit") }
2)、CONFIG 大法: CONFIG += qt5 qt5{ LIBS += -lmydll_qt5 }else{ LIBS += -lmydll } 也可以: CONFIG(debug, debug|release) { }
注意,左括号 { 最好位于同一行免得出问题。
===================== qmake 多工程subdirs编译依赖方法=====================
注意,以下写法是错的:
TEMPLATE = subdirs SUBDIRS = \ src/app \ # relative paths src/lib \ src/lib2 app.depends = lib lib2
虽然qmake可以解析出子工程叫lib 和lib2,但是就是无法depends!要用工程名+子目录 .subdir 架构才能识别:
TEMPLATE = subdirs SUBDIRS = \ lib2 \ # sub-project names lib \ app # where to find the sub projects - give the folders lib2.subdir = src/lib2 lib.subdir = src/lib app.subdir = src/app # what subproject depends on others app.depends = lib lib2
介绍在:https://wiki.qt.io/SUBDIRS_-_handling_dependencies
就问你坑不坑爹?!