QT(一).hello world(2)

2021-09-15 19:51:18 浏览数 (1)

qmake

生成 Qt 应用一般会经历如下三个步骤

代码语言:javascript复制
qmake -project
qmake
make

那 qmake 是干什么用的呢?

qmake --help 中可以获知

代码语言:javascript复制
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

qmake 是用来干两件事的

  • 根据源码产生项目文件 *.pro
  • 根据源码和项目文件产生 Makefile

一般而言对于一个新项目

  • 第一步产生项目文件使用 -project 模式
  • 第二步产生 Makefile 使用 -makefile 模式 ,但是这个模式是默认模式,不用加 -makefile 参数就代表这个模式

模式的详细信息可以参考

代码语言:javascript复制
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

至于最后一个 make 步骤,就是在通过qmake生成的 Makefile 来编译成目标文件和最终的可执行文件

qmake -project

qmake -project 的结果是生成了一个 *.pro 文件

在这个例子中就是 hello.pro 文件

我们可以来看看其中的内容

代码语言:javascript复制
[emacs@h102 hello]$ cat hello.pro 
######################################################################
# Automatically generated by qmake (2.01a) Thu Feb 23 17:27:01 2017
######################################################################

TEMPLATE = app
TARGET = 
DEPENDPATH  = .
INCLUDEPATH  = .

# Input
SOURCES  = main.cpp
[emacs@h102 hello]$

从中可知,这个文件是在将所有有源代码包含进来

qmake

qmake 不加参数就是代表的 qmake -makefile,即 makefile 模式,结果是生成了一个 Makefile 文件

在这个例子中就是 Makefile 文件

我们可以来看看其中的内容

代码语言:javascript复制
[emacs@h102 hello]$ cat Makefile 
#############################################################################
# Makefile for building: hello
# Generated by qmake (2.01a) (Qt 4.8.6) on: Thu Feb 23 17:27:08 2017
# Project:  hello.pro
# Template: app
# Command: /usr/local/Trolltech/Qt-4.8.6/bin/qmake -o Makefile hello.pro
#############################################################################

####### Compiler, tools and options

CC            = gcc
CXX           = g  
DEFINES       = -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED
CFLAGS        = -pipe -O2 -Wall -W -D_REENTRANT $(DEFINES)
CXXFLAGS      = -pipe -O2 -Wall -W -D_REENTRANT $(DEFINES)
INCPATH       = -I/usr/local/Trolltech/Qt-4.8.6/mkspecs/linux-g   -I. -I/usr/local/Trolltech/Qt-4.8.6/include/QtCore -I/usr/local/Trolltech/Qt-4.8.6/include/QtGui -I/usr/local/Trolltech/Qt-4.8.6/include -I. -I.
LINK          = g  
LFLAGS        = -Wl,-O1 -Wl,-rpath,/usr/local/Trolltech/Qt-4.8.6/lib
LIBS          = $(SUBLIBS)  -L/usr/local/Trolltech/Qt-4.8.6/lib -lQtGui -L/usr/local/Trolltech/Qt-4.8.6/lib -L/usr/X11R6/lib -lQtCore -lpthread 
AR            = ar cqs
RANLIB        = 
QMAKE         = /usr/local/Trolltech/Qt-4.8.6/bin/qmake
TAR           = tar -cf
COMPRESS      = gzip -9f
COPY          = cp -f
SED           = sed
COPY_FILE     = $(COPY)
COPY_DIR      = $(COPY) -r
STRIP         = strip
INSTALL_FILE  = install -m 644 -p
INSTALL_DIR   = $(COPY_DIR)
INSTALL_PROGRAM = install -m 755 -p
DEL_FILE      = rm -f
SYMLINK       = ln -f -s
DEL_DIR       = rmdir
MOVE          = mv -f
CHK_DIR_EXISTS= test -d
MKDIR         = mkdir -p

####### Output directory

OBJECTS_DIR   = ./

####### Files

SOURCES       = main.cpp 
OBJECTS       = main.o
DIST          = /usr/local/Trolltech/Qt-4.8.6/mkspecs/common/unix.conf 
		/usr/local/Trolltech/Qt-4.8.6/mkspecs/common/linux.conf 
		/usr/local/Trolltech/Qt-4.8.6/mkspecs/common/gcc-base.conf 
		/usr/local/Trolltech/Qt-4.8.6/mkspecs/common/gcc-base-unix.conf 
		/usr/local/Trolltech/Qt-4.8.6/mkspecs/common/g  -base.conf 
		/usr/local/Trolltech/Qt-4.8.6/mkspecs/common/g  -unix.conf 
		/usr/local/Trolltech/Qt-4.8.6/mkspecs/qconfig.pri 
		/usr/local/Trolltech/Qt-4.8.6/mkspecs/features/qt_functions.prf 
		/usr/local/Trolltech/Qt-4.8.6/mkspecs/features/qt_config.prf 
		/usr/local/Trolltech/Qt-4.8.6/mkspecs/features/exclusive_builds.prf 
		/usr/local/Trolltech/Qt-4.8.6/mkspecs/features/default_pre.prf 
		/usr/local/Trolltech/Qt-4.8.6/mkspecs/features/release.prf 
		/usr/local/Trolltech/Qt-4.8.6/mkspecs/features/default_post.prf 
		/usr/local/Trolltech/Qt-4.8.6/mkspecs/features/shared.prf 
		/usr/local/Trolltech/Qt-4.8.6/mkspecs/features/unix/gdb_dwarf_index.prf 
		/usr/local/Trolltech/Qt-4.8.6/mkspecs/features/warn_on.prf 
		/usr/local/Trolltech/Qt-4.8.6/mkspecs/features/qt.prf 
		/usr/local/Trolltech/Qt-4.8.6/mkspecs/features/unix/thread.prf 
		/usr/local/Trolltech/Qt-4.8.6/mkspecs/features/moc.prf 
		/usr/local/Trolltech/Qt-4.8.6/mkspecs/features/resources.prf 
		/usr/local/Trolltech/Qt-4.8.6/mkspecs/features/uic.prf 
		/usr/local/Trolltech/Qt-4.8.6/mkspecs/features/yacc.prf 
		/usr/local/Trolltech/Qt-4.8.6/mkspecs/features/lex.prf 
		/usr/local/Trolltech/Qt-4.8.6/mkspecs/features/include_source_dir.prf 
		hello.pro
QMAKE_TARGET  = hello
DESTDIR       = 
TARGET        = hello

first: all
####### Implicit rules

.SUFFIXES: .o .c .cpp .cc .cxx .C

.cpp.o:
	$(CXX) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<"

.cc.o:
	$(CXX) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<"

.cxx.o:
	$(CXX) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<"

.C.o:
	$(CXX) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<"

.c.o:
	$(CC) -c $(CFLAGS) $(INCPATH) -o "$@" "$<"

####### Build rules

all: Makefile $(TARGET)

$(TARGET):  $(OBJECTS)  
	$(LINK) $(LFLAGS) -o $(TARGET) $(OBJECTS) $(OBJCOMP) $(LIBS)

Makefile: hello.pro  /usr/local/Trolltech/Qt-4.8.6/mkspecs/linux-g  /qmake.conf /usr/local/Trolltech/Qt-4.8.6/mkspecs/common/unix.conf 
		/usr/local/Trolltech/Qt-4.8.6/mkspecs/common/linux.conf 
		/usr/local/Trolltech/Qt-4.8.6/mkspecs/common/gcc-base.conf 
		/usr/local/Trolltech/Qt-4.8.6/mkspecs/common/gcc-base-unix.conf 
		/usr/local/Trolltech/Qt-4.8.6/mkspecs/common/g  -base.conf 
		/usr/local/Trolltech/Qt-4.8.6/mkspecs/common/g  -unix.conf 
		/usr/local/Trolltech/Qt-4.8.6/mkspecs/qconfig.pri 
		/usr/local/Trolltech/Qt-4.8.6/mkspecs/features/qt_functions.prf 
		/usr/local/Trolltech/Qt-4.8.6/mkspecs/features/qt_config.prf 
		/usr/local/Trolltech/Qt-4.8.6/mkspecs/features/exclusive_builds.prf 
		/usr/local/Trolltech/Qt-4.8.6/mkspecs/features/default_pre.prf 
		/usr/local/Trolltech/Qt-4.8.6/mkspecs/features/release.prf 
		/usr/local/Trolltech/Qt-4.8.6/mkspecs/features/default_post.prf 
		/usr/local/Trolltech/Qt-4.8.6/mkspecs/features/shared.prf 
		/usr/local/Trolltech/Qt-4.8.6/mkspecs/features/unix/gdb_dwarf_index.prf 
		/usr/local/Trolltech/Qt-4.8.6/mkspecs/features/warn_on.prf 
		/usr/local/Trolltech/Qt-4.8.6/mkspecs/features/qt.prf 
		/usr/local/Trolltech/Qt-4.8.6/mkspecs/features/unix/thread.prf 
		/usr/local/Trolltech/Qt-4.8.6/mkspecs/features/moc.prf 
		/usr/local/Trolltech/Qt-4.8.6/mkspecs/features/resources.prf 
		/usr/local/Trolltech/Qt-4.8.6/mkspecs/features/uic.prf 
		/usr/local/Trolltech/Qt-4.8.6/mkspecs/features/yacc.prf 
		/usr/local/Trolltech/Qt-4.8.6/mkspecs/features/lex.prf 
		/usr/local/Trolltech/Qt-4.8.6/mkspecs/features/include_source_dir.prf 
		/usr/local/Trolltech/Qt-4.8.6/lib/libQtGui.prl 
		/usr/local/Trolltech/Qt-4.8.6/lib/libQtCore.prl
	$(QMAKE) -o Makefile hello.pro
/usr/local/Trolltech/Qt-4.8.6/mkspecs/common/unix.conf:
/usr/local/Trolltech/Qt-4.8.6/mkspecs/common/linux.conf:
/usr/local/Trolltech/Qt-4.8.6/mkspecs/common/gcc-base.conf:
/usr/local/Trolltech/Qt-4.8.6/mkspecs/common/gcc-base-unix.conf:
/usr/local/Trolltech/Qt-4.8.6/mkspecs/common/g  -base.conf:
/usr/local/Trolltech/Qt-4.8.6/mkspecs/common/g  -unix.conf:
/usr/local/Trolltech/Qt-4.8.6/mkspecs/qconfig.pri:
/usr/local/Trolltech/Qt-4.8.6/mkspecs/features/qt_functions.prf:
/usr/local/Trolltech/Qt-4.8.6/mkspecs/features/qt_config.prf:
/usr/local/Trolltech/Qt-4.8.6/mkspecs/features/exclusive_builds.prf:
/usr/local/Trolltech/Qt-4.8.6/mkspecs/features/default_pre.prf:
/usr/local/Trolltech/Qt-4.8.6/mkspecs/features/release.prf:
/usr/local/Trolltech/Qt-4.8.6/mkspecs/features/default_post.prf:
/usr/local/Trolltech/Qt-4.8.6/mkspecs/features/shared.prf:
/usr/local/Trolltech/Qt-4.8.6/mkspecs/features/unix/gdb_dwarf_index.prf:
/usr/local/Trolltech/Qt-4.8.6/mkspecs/features/warn_on.prf:
/usr/local/Trolltech/Qt-4.8.6/mkspecs/features/qt.prf:
/usr/local/Trolltech/Qt-4.8.6/mkspecs/features/unix/thread.prf:
/usr/local/Trolltech/Qt-4.8.6/mkspecs/features/moc.prf:
/usr/local/Trolltech/Qt-4.8.6/mkspecs/features/resources.prf:
/usr/local/Trolltech/Qt-4.8.6/mkspecs/features/uic.prf:
/usr/local/Trolltech/Qt-4.8.6/mkspecs/features/yacc.prf:
/usr/local/Trolltech/Qt-4.8.6/mkspecs/features/lex.prf:
/usr/local/Trolltech/Qt-4.8.6/mkspecs/features/include_source_dir.prf:
/usr/local/Trolltech/Qt-4.8.6/lib/libQtGui.prl:
/usr/local/Trolltech/Qt-4.8.6/lib/libQtCore.prl:
qmake:  FORCE
	@$(QMAKE) -o Makefile hello.pro

dist: 
	@$(CHK_DIR_EXISTS) .tmp/hello1.0.0 || $(MKDIR) .tmp/hello1.0.0 
	$(COPY_FILE) --parents $(SOURCES) $(DIST) .tmp/hello1.0.0/ && $(COPY_FILE) --parents main.cpp .tmp/hello1.0.0/ && (cd `dirname .tmp/hello1.0.0` && $(TAR) hello1.0.0.tar hello1.0.0 && $(COMPRESS) hello1.0.0.tar) && $(MOVE) `dirname .tmp/hello1.0.0`/hello1.0.0.tar.gz . && $(DEL_FILE) -r .tmp/hello1.0.0


clean:compiler_clean 
	-$(DEL_FILE) $(OBJECTS)
	-$(DEL_FILE) *~ core *.core


####### Sub-libraries

distclean: clean
	-$(DEL_FILE) $(TARGET) 
	-$(DEL_FILE) Makefile


check: first

mocclean: compiler_moc_header_clean compiler_moc_source_clean

mocables: compiler_moc_header_make_all compiler_moc_source_make_all

compiler_moc_header_make_all:
compiler_moc_header_clean:
compiler_rcc_make_all:
compiler_rcc_clean:
compiler_image_collection_make_all: qmake_image_collection.cpp
compiler_image_collection_clean:
	-$(DEL_FILE) qmake_image_collection.cpp
compiler_moc_source_make_all:
compiler_moc_source_clean:
compiler_uic_make_all:
compiler_uic_clean:
compiler_yacc_decl_make_all:
compiler_yacc_decl_clean:
compiler_yacc_impl_make_all:
compiler_yacc_impl_clean:
compiler_lex_make_all:
compiler_lex_clean:
compiler_clean: 

####### Compile

main.o: main.cpp 
	$(CXX) -c $(CXXFLAGS) $(INCPATH) -o main.o main.cpp

####### Install

install:   FORCE

uninstall:   FORCE

FORCE:

[emacs@h102 hello]$ 

这是一个常规的 Makefile, 从banner上可以获知一些头信息

代码语言:javascript复制
#############################################################################
# Makefile for building: hello
# Generated by qmake (2.01a) (Qt 4.8.6) on: Thu Feb 23 17:27:08 2017
# Project:  hello.pro
# Template: app
# Command: /usr/local/Trolltech/Qt-4.8.6/bin/qmake -o Makefile hello.pro
#############################################################################

其实是在自动调用这条命令 /usr/local/Trolltech/Qt-4.8.6/bin/qmake -o Makefile hello.pro

这个文件非常重要,因为所有的编译信息都配置在其中

Tip: 如果有新的库要加入,就得在这个文件中进行手动调整 ,如要加入 sqlite 库,就要将 LIBS = $(SUBLIBS) -L/usr/local/Trolltech/Qt-4.8.6/lib -lQtGui -L/usr/local/Trolltech/Qt-4.8.6/lib -L/usr/X11R6/lib -lQtCore -lpthread 手动改为 LIBS = $(SUBLIBS) -L/usr/local/Trolltech/Qt-4.8.6/lib -lQtGui -L/usr/local/Trolltech/Qt-4.8.6/lib -L/usr/X11R6/lib -lQtCore -lpthread -lsqlite3

总体上分为如下几个主要部分

代码语言:javascript复制
[emacs@h102 hello]$ grep '#' Makefile 
#############################################################################
# Makefile for building: hello
# Generated by qmake (2.01a) (Qt 4.8.6) on: Thu Feb 23 17:27:08 2017
# Project:  hello.pro
# Template: app
# Command: /usr/local/Trolltech/Qt-4.8.6/bin/qmake -o Makefile hello.pro
#############################################################################
####### Compiler, tools and options
####### Output directory
####### Files
####### Implicit rules
####### Build rules
####### Sub-libraries
####### Compile
####### Install
[emacs@h102 hello]$
  • 头部信息
  • 编译工具和选项
  • 输出目录
  • 文件
  • 隐式规则
  • 构建规则
  • 子库
  • 编译命令
  • 安装

关于Makefile中的细节,可以查阅网络上的相关资料


make

通过 Makefile 来编译成目标文件和最终的可执行文件

在这个例子中就是 main.ohello


总结

  • gcc -v
  • qmake -v
  • mkdir hello
  • qmake -project
  • qmake
  • make
  • echo $?
  • ./hello
  • cat hello.pro
  • cat Makefile
  • grep '#' Makefile

原文地址

0 人点赞