Can I use Qt without qmake or Qt Creator?

I want to program using Qt, but I do not want to use special compilers or IDEs like Qt Creator and qmake. I want to write with Kate and compile with g ++.

Is it possible to compile a program using Qt with g ++? How to compile it with g ++?

+61
c ++ qt compilation
Sep 02 '10 at 23:31
source share
4 answers

Sure. Although this is convenient with qmake or CMake, you can do:

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 makefile)

Update - call moc :

Quote from moc manpage :

Here is a useful makefile rule if you use only GNU make:

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

I would call the output more likely %.moc.cpp (than m%.cpp ). Then you add the my_prog dependency to my_prog.moc.cpp

 my_prog: my_prog.cpp my_prog.moc.cpp 

Similarly for uic . The situation here is more complicated, since you must generate rules for the headers and source files, and you need to add a dependency on the header file to ensure that it is received before the sources are compiled. Maybe something like this:

 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 
+34
Sep 02 '10 at 23:37
source share
— -

You certainly do not need to use QtCreator to write a Qt program.

You also don't need to use qmake , but you ask for the problem without using it.

To do anything even remotely interesting in Qt, you will inevitably end up subclassing QObject . All of these subclasses require the Q_OBJECT macro in their definition, which allows signal / slot syntax. This syntax is not regular C ++ and cannot be compiled using g ++. Files containing class definitions using Q_OBJECT must be executed through the Qt metaobject compiler called moc . This means that you need to decide which files the moc should have for them, then run moc on them, and then compile the resulting cpp file with g++ . It is for this reason that Qt delivers qmake . It generates the correct rules in the Makefile for you.

The Qt.pro project files are really very easy to use, and I would seriously recommend using them. Remember that qmake is a command line tool similar to g++ . In addition, it can actually create a skeleton project file for you by providing the -project , so for starters you can just do

 qmake -project qmake make 

and you're done. In practice, I found that in the generated project file there might be no declaration of any additional Qt libraries that I could use, so you may need to add a line like

 QT += opengl 

if, for example, you included something like QGLWidget .

+18
Sep 03 2018-10-09T00:
source share

Here is my makefile for any Qt project without using qmake:

 #--------------------------------------------------------------------------------- # 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 headers other than QObject, here is its source code:

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

+5
Nov 02 '11 at 15:05
source share

Some pre-compilers are needed for Qt projcet, such as moc, uic, ... etc. Qt Creator + qmake is convenient to do such things and generate a make file for g ++ or msvc compilers.

0
Sep 03 2018-10-09T00:
source share



All Articles