不需要手動編寫Makefiles。調用自定義外部工具的Makefile可以通過項目文件.pro
中的qmake
生成。
需要使用QMAKE_EXTRA_TARGETS
創建自定義目標。然後,主目標應該被設置爲在該自定義對象(自定義的目標名稱應該被添加到)denendent,例如How to modify the PATH variable in Qt Creator's project file (.pro)
該工具應代形式頭的後運行,因此自定義的目標應該取決於文件customtarget1.depends = ui_mainwindow.h
:
customtarget1.target = form_scanner
customtarget1.commands = tool_win_bat_or_linux_shell.sh
customtarget1.depends = ui_mainwindow.h
QMAKE_EXTRA_TARGETS += customtarget1
PRE_TARGETDEPS += form_scanner
上面qmake
命令創建以下Makefile
規則:
# the form header depends on mainwindow.ui
ui_mainwindow.h: ..\test\mainwindow.ui
<tab>#build command...
# form scanner depends on ui_mainwindow.h
form_scanner: ui_mainwindow.h
<tab>tool_win_bat_or_linux_shell.sh
# the final target depends on form scanner
$(DESTDIR_TARGET): form_scanner ui_mainwindowm.h $(OBJECTS)
如果有多種形式,可以創建多個自定義目標或創建一個目標依賴於所有形式的文件:
for (form, FORMS) {
# autogenerated form headers are located in root of build directory
FILE_NAME = $$basename(form)
# prepend ui_ and replace ending .ui by .h
FORM_HEADERS += ui_$$replace(FILE_NAME, .ui$, .h)
}
customtarget1.target = form_scanner
customtarget1.commands = tool_win_bat_or_linux_shell.sh
customtarget1.depends = $$FORM_HEADERS
QMAKE_EXTRA_TARGETS += customtarget1
PRE_TARGETDEPS += form_scanner
因此,這個命令tool_win_bat_or_linux_shell.sh
產生的所有形式的標題時,才執行。
也可以從項目目錄$$PWD
運行shell腳本,並通過命令行參數的形式頭文件名:
customtarget1.commands = $$PWD/tool_win_bat_or_linux_shell.sh $$FORM_HEADERS
現在,shell腳本可以爲每個窗體標題tool_win_bat_or_linux_shell.sh
運行一些命令:
# for each command line argument
for file in "[email protected]"
do
echo "$file"
ls -l $file
done
謝謝 - 這看起來就像票。我還沒有檢查過它,但它肯定比手動編輯Makefile更有意義 – mike