Skip to content

Instantly share code, notes, and snippets.

@mishurov
Last active April 9, 2024 21:06
Show Gist options
  • Star 22 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mishurov/8134532 to your computer and use it in GitHub Desktop.
Save mishurov/8134532 to your computer and use it in GitHub Desktop.
Makefile to build QT projects (Linux) without qmake
CC=g++
MOC=moc-qt4
CFLAGS=-Wall
SOURCES=hello.cc hello_cls.cc
MOC_HEADERS=hello_cls.h
EXECUTABLE=hello
INCDIRS=-I/usr/include/qt4 -I/usr/include/qt4/QtGui -I/usr/include/qt4/QtCore
LIBS=-lQtCore -lQtGui
# Change postfixes
MOC_SOURCES=$(MOC_HEADERS:.h=.moc.cc)
OBJECTS=$(SOURCES:.cc=.o) $(MOC_SOURCES:.cc=.o)
all: $(EXECUTABLE)
@echo Done!
$(EXECUTABLE): $(OBJECTS)
$(CC) $^ $(LIBS) -o $@
# Generate object files, rule to change postfix
%.o: %.cc
$(CC) $(CFLAGS) $(INCDIRS) -c $< -o $@
# Generate cc from h via Qt's Meta Object Compiler, rule to change postfix
%.moc.cc: %.h
$(MOC) $(INCDIRS) $< -o $@
.PHONY: tags clean
clean:
rm *.o
# Generate ctags file for all included files (autocomplete and jump to source)
tags:
gcc -M $(INCDIRS) $(SOURCES) | \
sed -e 's/[\\ ]/\n/g' | \
sed -e '/^$$/d' -e '/\.o:[ \t]*$$/d' | \
ctags -L - --c++-kinds=+p --fields=+iaS --extra=+q
@habaneropep2019
Copy link

This variant is pretty much useless to anyone doing anything modern, but if you're using Qt2 or Osiris (which is my modernized-ish fork of Qt2.3.2), here's a modified version that should work with Qt2 and Osiris. If using Qt2, change $(OSIRISDIR) to $(QTDIR). You'll still need to manually run uic before running make if you're using .ui files.

CC=g++
MOC=moc
CFLAGS=-Wall
SOURCES=main.cpp wizard.cpp
MOC_HEADERS=wizard.h
EXECUTABLE=wizard
INCDIRS=-I$(OSIRISDIR)/include
LIBS=-lqt -L$(OSIRISDIR)/lib
# Change postfixes
MOC_SOURCES=$(MOC_HEADERS:.h=.moc.cc)
OBJECTS=$(SOURCES:.cc=.o) $(MOC_SOURCES:.cc=.o)

all: $(EXECUTABLE)
        @echo Done!

$(EXECUTABLE): $(OBJECTS)
        $(CC) $^ $(INCDIRS) $(LIBS) -o $@

# Generate object files, rule to change postfix
%.o: %.cc
        $(CC) $(CFLAGS) $(INCDIRS) -c $< -o $@

# Generate cc from h via Qt's Meta Object Compiler, rule to change postfix
%.moc.cc: %.h
        $(MOC) $< -o $@

.PHONY: tags clean

clean:
        rm *.o

# Generate ctags file for all included files (autocomplete and jump to source)
tags:
        gcc -M $(INCDIRS) $(SOURCES) | \
        sed -e 's/[\\ ]/\n/g' | \
        sed -e '/^$$/d' -e '/\.o:[ \t]*$$/d' | \
        ctags -L - --c++-kinds=+p --fields=+iaS --extra=+q

@mishurov
Copy link
Author

mishurov commented Aug 7, 2023

@habaneropep2019 it is as old as mammoth shite. I'm working on time series rendered on GPU and QML.

@habaneropep2019
Copy link

@mishurov is there any chance I could get your permission to include your makefile with my Qt fork, under GPLv2 or BSD?

@mishurov
Copy link
Author

mishurov commented Aug 7, 2023

Why even ask that. Sure, of course. I'm not a lawyer. Everything I share, it is for public use.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment