C++ 我可以在没有 qmake 或 Qt Creator 的情况下使用 Qt 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3632038/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I use Qt without qmake or Qt Creator?
提问by Squall
I want to program using Qt, but I don't want to use special compilers or IDE such as Qt Creator and qmake. I want to write with Kate and compile with g++.
我想使用 Qt 进行编程,但我不想使用特殊的编译器或 IDE,例如 Qt Creator 和 qmake。我想用 Kate 编写并用 g++ 编译。
Can I compile a program that uses Qt with g++? How do I compile it with g++?
我可以编译一个使用 Qt 和 g++ 的程序吗?我如何用 g++ 编译它?
采纳答案by jpalecek
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)
(在生成文件中)
Update - invoking moc
:
更新 - 调用moc
:
Quote from moc manpage:
来自moc 联机帮助页的引用:
Here is a useful makefile rule if you only use GNU make:
m%.cpp: %.h moc $< -o $@
如果您只使用 GNU make,这是一个有用的 makefile 规则:
m%.cpp: %.h moc $< -o $@
I'd personally name the output rather %.moc.cpp
(than m%.cpp
). You then add the dependency of my_prog
on my_prog.moc.cpp
我个人命名输出而不是%.moc.cpp
(而不是m%.cpp
)。然后,添加的依赖my_prog
上my_prog.moc.cpp
my_prog: my_prog.cpp my_prog.moc.cpp
Similarly for uic. The situation here is more complicated, since you have to generate rules for headers andsource 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:
同样,对于UIC。这里的情况更复杂,因为您必须为头文件和源文件生成规则,并且必须添加对头文件的依赖以确保它在源代码编译之前生成。像这样的事情可能会奏效:
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
回答by Troubadour
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
. All these subclasses require the Q_OBJECT
macro in their definition which enables the signal/slot syntax. This syntax is not regular C++ and cannot be compiled using g++. Files containing class definitions with Q_OBJECT
must be run through Qt's meta-object compilerwhich is called 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++
. This is the reason that Qt supplies qmake
. It generates the correct rules in the Makefile for you.
要在 Qt 中做任何有趣的事情,您将不可避免地以子类化结束QObject
。所有这些子类都需要Q_OBJECT
在其定义中使用宏来启用信号/插槽语法。此语法不是常规的 C++,不能使用 g++ 编译。包含类定义的文件Q_OBJECT
必须通过 Qt 的元对象编译器运行,该编译器称为moc
. 这意味着您必须确定哪些文件需要moc
应用于它们,然后moc
在它们上运行,然后使用 .cpp 编译生成的 cpp 文件g++
。这就是 Qt 提供qmake
. 它会在 Makefile 中为您生成正确的规则。
Qt .pro project files are really quite straightforward to work with and I would seriously recommend that you use them.
Remember, qmake
is a command line tool just like g++
. Also, it can actually create a skeleton project file for you by supplying the -project
option so to get started you can just do
Qt .pro 项目文件使用起来非常简单,我强烈建议您使用它们。请记住,qmake
是一个命令行工具,就像g++
. 此外,它实际上可以通过提供-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
.
回答by Quent42340
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
回答by AechoLiu
Some pre-compilers are necessary for Qt projcet, like moc, uic, ...,etc. Qt Creator + qmake are convenient to do such things and generate a makefile for g++ or msvc compilers.
Qt 项目需要一些预编译器,如 moc、uic、...等。Qt Creator + qmake 可以方便地做这样的事情,并为 g++ 或 msvc 编译器生成一个 makefile。