简体   繁体   English

我可以在没有 qmake 或 Qt Creator 的情况下使用 Qt 吗?

[英]Can I use Qt without qmake or Qt Creator?

I want to program using Qt, but I don't want to use special compilers or IDE such as Qt Creator and qmake.我想使用 Qt 进行编程,但我不想使用特殊的编译器或 IDE,例如 Qt Creator 和 qmake。 I want to write with Kate and compile with g++.我想用 Kate 编写并用 g++ 编译。

Can I compile a program that uses Qt with g++?我可以编译一个使用 Qt 和 g++ 的程序吗? How do I compile it with g++?我如何用 g++ 编译它?

Sure you can. 你当然可以。 Although it is more convenient with qmake or CMake, you can do: 尽管使用qmake或CMake更方便,但是您可以执行以下操作:

CXXFLAGS += -Ipath_to_your_qt_includes
LDFLAGS += -Lpath_to_your_qt_libs

LDLIBS += -lqt-mt (for Qt3)

or 要么

LDLIBS += -lQtCore -lQtGui (for Qt4, add what you need)

my_prog: my_prog.cpp

(in a makefile) (在makefile中)

Update - invoking moc : 更新-调用moc

Quote from moc manpage : 引用来自moc手册页

Here is a useful makefile rule if you only use GNU make: 如果仅使用GNU make,这是一个有用的makefile规则:

 m%.cpp: %.h moc $< -o $@ 

I'd personally name the output rather %.moc.cpp (than m%.cpp ). 我个人将输出命名为%.moc.cpp (比m%.cpp )。 You then add the dependency of my_prog on my_prog.moc.cpp 然后,在my_prog上添加my_prog的依赖my_prog.moc.cpp

my_prog: my_prog.cpp my_prog.moc.cpp

Similarly for uic . 对于uic同样如此。 The situation here is more complicated, since you have to generate rules for headers and source files, and you have to add a dependency on a header file to ensure it gets generated before the sources are compiled. 这里的情况更加复杂,因为您必须为标头源文件生成规则,并且必须在标头文件上添加依赖项以确保在编译源文件之前就生成了它。 Something like this might work: 这样的事情可能会起作用:

my_prog: my_prog.o my_prog.moc.o my_prog.ui.o
        $(CXX)  $(LDFLAGS) -o my_prog $^ $(LDLIBS)

my_prog.o: my_prog.cpp my_prog.ui.h

You certainly don't have to use QtCreator to write a Qt program. 您当然不必使用QtCreator编写Qt程序。

You also don't have to use qmake but you are asking for trouble by not using it. 您也不必使用qmake但您通过不使用它来解决问题。

To do anything even remotely interesting in Qt you will inevitably end up subclassing QObject . 为了在Qt中做什至遥不可及的事情,您将不可避免地最终QObject All these subclasses require the Q_OBJECT macro in their definition which enables the signal/slot syntax. 所有这些子类在其定义中都需要Q_OBJECT宏,以启用信号/插槽语法。 This syntax is not regular C++ and cannot be compiled using g++. 此语法不是常规的C ++,不能使用g ++进行编译。 Files containing class definitions with Q_OBJECT must be run through Qt's meta-object compiler which is called moc . 包含带有Q_OBJECT类定义的文件必须通过Qt的元对象编译器 (称为moc This means you have to work out which files need to have moc applied to them, then run moc on them, and then compile the resulting cpp file with g++ . 这意味着您必须确定哪些文件需要应用moc ,然后对它们运行moc ,然后使用g++编译生成的cpp文件。 This is the reason that Qt supplies qmake . 这就是Qt提供qmake的原因。 It generates the correct rules in the Makefile for you. 它会在Makefile中为您生成正确的规则。

Qt .pro project files are really quite straightforward to work with and I would seriously recommend that you use them. Qt .pro项目文件确实非常易于使用,我强烈建议您使用它们。 Remember, qmake is a command line tool just like g++ . 请记住, qmakeg++一样是命令行工具。 Also, it can actually create a skeleton project file for you by supplying the -project option so to get started you can just do 另外,它实际上可以通过提供-project选项为您创建一个骨架项目文件,因此,您只需执行

qmake -project
qmake
make

and you are done. 到此为止。 In practice I find that the generated project file may be missing the declaration of any extra Qt libraries I might be using so you might have to add a line like 在实践中,我发现生成的项目文件可能缺少我可能正在使用的任何其他Qt库的声明,因此您可能必须添加一行

QT += opengl

if, for example, you have included something like QGLWidget . 例如,如果您包括了QGLWidget东西。

Here is my makefile for any Qt project without using qmake: 这是我不使用qmake的任何Qt项目的makefile:

#---------------------------------------------------------------------------------
# Compiler executables
#---------------------------------------------------------------------------------
CC      :=  gcc
CXX     :=  g++

#---------------------------------------------------------------------------------
# Options for code generation
#---------------------------------------------------------------------------------
DEFINES :=  -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED
CFLAGS  :=  -g -Wall $(DEFINES)
CXXFLAGS:=  $(CFLAGS)
LDFLAGS :=  -g -Wl

#---------------------------------------------------------------------------------
# Any extra libraries you wish to link with your project
#---------------------------------------------------------------------------------
LIBS    :=  -lQtGui -lQtCore -lpthread

#---------------------------------------------------------------------------------
# Some more include paths
#---------------------------------------------------------------------------------
INCPATHS:=  -I/usr/share/qt4/mkspecs/default -I/usr/include/QtGui -I/usr/include/QtCore

#---------------------------------------------------------------------------------
# Source folders and executable name
#---------------------------------------------------------------------------------
TARGET  :=  $(shell basename $(CURDIR))
BUILD   :=  build
SOURCES :=  source
INCLUDES:=  source include

#---------------------------------------------------------------------------------
# Source files
#---------------------------------------------------------------------------------
ifneq ($(BUILD),$(notdir $(CURDIR)))
#---------------------------------------------------------------------------------
export OUTPUT   :=  $(CURDIR)/$(TARGET)

export VPATH    :=  $(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) \
                    $(foreach dir,$(INCLUDES),$(CURDIR)/$(dir))

CFILES      :=  $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c)))
CPPFILES    :=  $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp)))
HFILES      :=  $(foreach dir,$(INCLUDES),$(notdir $(wildcard $(dir)/*.h)))

#---------------------------------------------------------------------------------
# Use CXX for linking C++ projects, CC for standard C
#---------------------------------------------------------------------------------
ifeq ($(strip $(CPPFILES)),)
#---------------------------------------------------------------------------------
    export LD   :=  $(CC)
#---------------------------------------------------------------------------------
else
#---------------------------------------------------------------------------------
    export LD   :=  $(CXX)
#---------------------------------------------------------------------------------
endif
#---------------------------------------------------------------------------------

export OFILES   :=  $(CPPFILES:.cpp=.o) $(CFILES:.c=.o) $(HFILES:.h=.moc.o)

export INCLUDE  :=  $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) $(INCPATHS)

#---------------------------------------------------------------------------------
.PHONY: $(BUILD) clean install uninstall
#------------------------------------------------------------------------------
$(BUILD):
    @[ -d $@ ] || mkdir -p $@
    @make --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile

#---------------------------------------------------------------------------------
clean:
    @echo clean ...
    @rm -fr $(BUILD) $(TARGET)

#---------------------------------------------------------------------------------
install:
    @cp -u $(TARGET) /usr/bin/$(TARGET)
    @echo installed.

#---------------------------------------------------------------------------------
uninstall:
    @rm -f /usr/bin/$(TARGET)
    @echo uninstalled.

#---------------------------------------------------------------------------------
else
#---------------------------------------------------------------------------------
# Makefile targets
#---------------------------------------------------------------------------------
all: $(OUTPUT)

#---------------------------------------------------------------------------------
$(OUTPUT): $(OFILES)
    @echo built ... $(notdir $@)
    @$(LD) $(LDFLAGS) $(OFILES) -o $@ $(LIBS)

#---------------------------------------------------------------------------------
%.o: %.c
#---------------------------------------------------------------------------------
    @echo $(notdir $<)
    @$(C) $(CFLAGS) $(INCLUDE) -c $< -o $@

#---------------------------------------------------------------------------------
%.o: %.cpp
#---------------------------------------------------------------------------------
    @echo $(notdir $<)
    @$(CXX) $(CXXFLAGS) $(INCLUDE) -c $< -o $@

#---------------------------------------------------------------------------------
%.moc.cpp: %.h
#---------------------------------------------------------------------------------
    @echo $(notdir $<)
    @moctool $< $(DEFINES) $(INCLUDE) -o $@

#---------------------------------------------------------------------------------
%.moc.o: %.moc.cpp
#---------------------------------------------------------------------------------
    @echo $(notdir $<)
    @$(CXX) $(CXXFLAGS) $(INCLUDE) -c $< -o $@

#---------------------------------------------------------------------------------
endif
#---------------------------------------------------------------------------------

Here, moctool is a simple tool that helps for non-QObject headers, here is its source code: 在这里,moctool是一个简单的工具,可帮助处理非QObject标头,这是其源代码:

https://github.com/Quent42340/EasyLib/blob/master/tools/moctool/source/main.cpp https://github.com/Quent42340/EasyLib/blob/master/tools/moctool/source/main.cpp

Some pre-compilers are necessary for Qt projcet, like moc, uic, ...,etc. Qt项目需要一些预编译器,例如moc,uic等。 Qt Creator + qmake are convenient to do such things and generate a makefile for g++ or msvc compilers. Qt Creator + qmake可以很方便地执行此类操作并为g ++或msvc编译器生成makefile。

I like to program with Qt using Vim and reduced version of Qt Designer... this last one helps me to make window prototypes... then i translate the hierarchy from Qt Designer into my handcoded window header on Vim...我喜欢使用 Vim 和 Qt Designer 的简化版本使用 Qt 进行编程……最后一个帮助我制作窗口原型……然后我将 Qt Designer 中的层次结构转换为我在 Vim 上手工编码的窗口标题……

The result is that all my code is maintained by myself.. no .ui, no ui_.cpp .. no ui pointer... so i can manipulate my code freely.结果是我所有的代码都是我自己维护的……没有 .ui,没有 ui_.cpp ……没有 ui 指针……所以我可以自由地操纵我的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM