Compiling on MacOs with a Makefile
-
I've installed Qt on MacOs Ventura via Homebrew with:
brew install qt
Which seems to have given me a bunch of headers and other stuff I'd expect to be able to compile programs with. So I created a 'hello world' program of sorts:
#include <QApplication> #include <QLabel> int main(int argc, char *argv[]) { QApplication app(argc, argv); QLabel *label = new QLabel("Hello, World!"); label->show(); return app.exec(); }
And an accompanying Makefile:
PREFIX = /usr/local/opt/qt@5 INCLUDE = -I$(PREFIX)/include \ -I$(PREFIX)/include/QtCore \ -I$(PREFIX)/include/QtWidgets \ -I$(PREFIX)/include/QtGui LIB = -L$(PREFIX)/lib main: main.cpp g++ -fPIC -std=c++17 $(INCLUDE) $(LIB) -lQtCore -lQtGui -lQtWidgets $< -o $@
The program compiles but when linking doesn't find QtCore. Which isn't surprising, because there is no QtCore.dylib either in the lib directory I've specified or in any of the files the brew command installed. There is also no QtCore.a or QtCore.so. I wondered if this was just a mistake on the part of the Homebrew maintainers, however I also tried the same thing with Qt5:
brew install qt@5
I changed my include and library paths to match but the same thing happened. So my question is, where are the actual libraries? Should I be creating an issue on the Homebrew page, or am I missing something fundamental here?
-
Hi and welcome to devnet,
You're on macOS not Linux/Unix.
Libraries end in .dylib but most important many of them including Qt are provided in the form of frameworks. To use them you have make use of -F and -framework in place of -L and -l.
That said, I would recommend using a project manager such as CMake or even qmake but it has been deprecated in Qt 6.
-
@SGaist
Thank you for getting back to me. Once I googled what a framework was it was all relatively simple to converge on a solution. For anyone else wanting the same thing, this Makefile does the trick:PREFIX = /usr/local/opt/qt INCLUDE = -I$(PREFIX)/include \ -I$(PREFIX)/include/QtCore \ -I$(PREFIX)/include/QtWidgets \ -I$(PREFIX)/include/QtGui LIB = -F$(PREFIX)/Frameworks main: main.cpp g++ -fPIC -std=c++17 $(INCLUDE) $(LIB) -framework QtCore -framework QtGui -framework QtWidgets $< -o $@
-