how to compile Qt files with g++, VSCode
-
wrote on 16 Jul 2022, 20:20 last edited by Lightshadown
Hi, im using VScode and made a makefile to compile all my files, i used to develop on Netbeans but once i made the switch now i have to compile manually everything, my problems is: i can compile my cpp files but when i try to link all together and create the exe file, i got tons of warnings -- undefined reference to `_imp___ZN11QMetaObject10ConnectionD1Ev' -- and every other Qt related object i have used on my program, i did used UIC for compiling the ui interface thinking that was the problem, still same issue.
i have like 8 files of my own, but i did notice there are moc files as well (from previous netbeans ide), and i have no idea if the ui.h files need to be included when linking or just on my code
this is how i compile every file inside the makefile
COMPILER := C:\Qt\Qt5.11.3\Tools\mingw530_32\bin\g++.exe INCLUDE := C:\Qt\Qt5.11.3\5.11.3\mingw53_32\include LIB := C:\Qt\Qt5.11.3\5.11.3\mingw53_32\lib $(COMPILER) -std=c++14 -c -Wall -Wpragmas -mthreads -I $(INCLUDE) -I $(INCLUDE)\QtCore -I $(INCLUDE)\QtSql -I $(INCLUDE)\QtWidgets -I $(INCLUDE)\QtPrintSupport -I $(INCLUDE)\QtNetwork -I $(INCLUDE)\QtGui -I $(INCLUDE)\QtConcurrent -lmingw32 -lshell32 -L C:\MinGW\lib\libiphlpapi.a -L $(LIB) -L $(LIB)\libqtmaind.a -L $(LIB)\libQt5Cored.a -L $(LIB)\libQt5Sqld.a -L $(LIB)\libQt5PrintSupportd.a -L $(LIB)\libQt5Widgetsd.a -L $(LIB)\libQt5Guid.a -L $(LIB)\libQt5Networkd.a -o $(BUILD_DIR)/Silo_Sas.o Cpp/Silo_SaS.cpp
the code above works but when i try to compile the next code i just get warnings and no exe file
C:\Qt\Qt5.11.3\Tools\mingw530_32\bin\g++.exe -g build\Debug\Vscode_Windows\Silo_Sas.o build\Debug\Vscode_Windows\main_Pantalla_Principal.o build\Debug\Vscode_Windows\Generales.o build\Debug\Vscode_Windows\Impresora.o build\Debug\Vscode_Windows\Launcher.o build\Debug\Vscode_Windows\Login.o build\Debug\Vscode_Windows\Servidor_Rpi.o -o dist\Debug\Vscode_Windows\Sistema_Silo_Windows_VScode_1_4.exe
in essence it should link everything im incluiding the libs when i compile, but for some reason behaves like they are not included, the real question is: Do i need to link other libraries? or im missing any flag on the compiler?
-
Hi, im using VScode and made a makefile to compile all my files, i used to develop on Netbeans but once i made the switch now i have to compile manually everything, my problems is: i can compile my cpp files but when i try to link all together and create the exe file, i got tons of warnings -- undefined reference to `_imp___ZN11QMetaObject10ConnectionD1Ev' -- and every other Qt related object i have used on my program, i did used UIC for compiling the ui interface thinking that was the problem, still same issue.
i have like 8 files of my own, but i did notice there are moc files as well (from previous netbeans ide), and i have no idea if the ui.h files need to be included when linking or just on my code
this is how i compile every file inside the makefile
COMPILER := C:\Qt\Qt5.11.3\Tools\mingw530_32\bin\g++.exe INCLUDE := C:\Qt\Qt5.11.3\5.11.3\mingw53_32\include LIB := C:\Qt\Qt5.11.3\5.11.3\mingw53_32\lib $(COMPILER) -std=c++14 -c -Wall -Wpragmas -mthreads -I $(INCLUDE) -I $(INCLUDE)\QtCore -I $(INCLUDE)\QtSql -I $(INCLUDE)\QtWidgets -I $(INCLUDE)\QtPrintSupport -I $(INCLUDE)\QtNetwork -I $(INCLUDE)\QtGui -I $(INCLUDE)\QtConcurrent -lmingw32 -lshell32 -L C:\MinGW\lib\libiphlpapi.a -L $(LIB) -L $(LIB)\libqtmaind.a -L $(LIB)\libQt5Cored.a -L $(LIB)\libQt5Sqld.a -L $(LIB)\libQt5PrintSupportd.a -L $(LIB)\libQt5Widgetsd.a -L $(LIB)\libQt5Guid.a -L $(LIB)\libQt5Networkd.a -o $(BUILD_DIR)/Silo_Sas.o Cpp/Silo_SaS.cpp
the code above works but when i try to compile the next code i just get warnings and no exe file
C:\Qt\Qt5.11.3\Tools\mingw530_32\bin\g++.exe -g build\Debug\Vscode_Windows\Silo_Sas.o build\Debug\Vscode_Windows\main_Pantalla_Principal.o build\Debug\Vscode_Windows\Generales.o build\Debug\Vscode_Windows\Impresora.o build\Debug\Vscode_Windows\Launcher.o build\Debug\Vscode_Windows\Login.o build\Debug\Vscode_Windows\Servidor_Rpi.o -o dist\Debug\Vscode_Windows\Sistema_Silo_Windows_VScode_1_4.exe
in essence it should link everything im incluiding the libs when i compile, but for some reason behaves like they are not included, the real question is: Do i need to link other libraries? or im missing any flag on the compiler?
@Lightshadown said in how to compile Qt files with g++, VSCode:
and made a makefile
This is wrong - use qmake or better cmake and let the Makefile create them for you, then all other stuff like moc (which you're missing) is added automatically.
-
@Lightshadown said in how to compile Qt files with g++, VSCode:
and made a makefile
This is wrong - use qmake or better cmake and let the Makefile create them for you, then all other stuff like moc (which you're missing) is added automatically.
wrote on 16 Jul 2022, 20:46 last edited by@Christian-Ehrlicher sso basically makefile is useless in order to compile Qt applications? also, theres a Qt tools addon on vscode market place, is that a good option or just stick with cmake/qmake?
-
Don’t know which tools you mean and which ones suggest building Makefiles manually. cmake/ninja and qmake are the recommended and supported options, as @Christian-Ehrlicher has pointed out.
-
wrote on 16 Jul 2022, 21:48 last edited by
@Lightshadown said in how to compile Qt files with g++, VSCode:
basically makefile is useless in order to compile Qt applications?
No. qmake and /or cmake write a Makefile for you that captures all the elements that compiling and linking a Qt program requires. Both tools take a (typically) straightforward project description. You are, of course, welcome to craft your own Makefile but this will require more knowledge than using either of the tools.
You would need to at add:
- Passing any header file containing a QObject class (Q_OBJECT macro) through
moc
- Passing any ui file through
uic
- Add the output of the
moc
anduic
processes to your CPP sources for compilation - Add all the relevant Qt libraries to your compiler
-L
and-l
options e.g.,-lQt5Core -lQT5Gui
- Passing any header file containing a QObject class (Q_OBJECT macro) through
-
wrote on 16 Jul 2022, 22:21 last edited by
Ok i get it, ill check qmake to do my proyect, i tought it was easier to do it manually but i see its more complicated than expected, thanks everyone for your answers, i wont waste more time making my own makefile, i just want to compile it and keep coding
-
wrote on 16 Jul 2022, 22:57 last edited by
just one question , if i add a name with spaces like ui_Sistem window 2.5.h do i simply add it like any other name in the Headers tag inside my project.pro file or it should be "ui_Sistem window 2.5.h" with the quotes?. not sure on that last part, other than that already checking tutorials about qmake
-
wrote on 17 Jul 2022, 00:31 last edited by
My mistake, this:
Add the output of the moc and uic processes to your CPP sources for compilation
should only be the
moc
output. The UI file output is #included in your sources.As for space in file names, I think double quotes would do it for qmake. However, I would avoid spaces entirely because at some point in the processing the quotes will get lost/be missing and things will break in non-obvious ways.
-
just one question , if i add a name with spaces like ui_Sistem window 2.5.h do i simply add it like any other name in the Headers tag inside my project.pro file or it should be "ui_Sistem window 2.5.h" with the quotes?. not sure on that last part, other than that already checking tutorials about qmake
@Lightshadown said in how to compile Qt files with g++, VSCode:
just one question , if i add a name with spaces like ui_Sistem window 2.5.h do i simply add it like any other name in the Headers tag inside my project.pro file or it should be "ui_Sistem window 2.5.h" with the quotes
Neither - such a file is generated by uic and must not be added to your source tree.
When you use qmake you have to add the ui file to FORMS. And don't add spaces to your file names for your own pleasure - you can be sure it just makes problems somewhere in your build chain.
1/9