linker error in vs code
-
wrote on 31 Aug 2023, 15:51 last edited by
I wrote this program in vs-code for testing.
#include <QtWidgets/QApplication> #include <QtWidgets/QPushButton> int main(int argc, char **argv){ QApplication app (argc, argv); QPushButton button ("Hello world !"); button.show(); return app.exec(); }
the c_cpp_properties.json file is:
{ "configurations": [ { "name": "Win32", "includePath": [ "${workspaceFolder}/**", "C:/Qt/6.5.0/mingw_64/include" ], "defines": [ "_DEBUG", "UNICODE", "_UNICODE" ], "compilerPath": "C:\\msys64\\mingw64\\bin\\gcc.exe", "cStandard": "c17", "cppStandard": "gnu++17", "intelliSenseMode": "windows-gcc-x64" } ], "version": 4 }
and the tasks.json file is:
{ "tasks": [ { "type": "cppbuild", "label": "C/C++: g++.exe build active file", "command": "C:\\msys64\\mingw64\\bin\\g++.exe", "args": [ "-fdiagnostics-color=always", "-g", "${file}", "-I", "C:/Qt/6.5.0/mingw_64/include", "-L", "C:/Qt/6.5.0/mingw_64/lib", "-o", "${fileDirname}\\${fileBasenameNoExtension}.exe" ], "options": { "cwd": "${fileDirname}" }, "problemMatcher": [ "$gcc" ], "group": { "kind": "build", "isDefault": true }, "detail": "Task generated by Debugger." } ], "version": "2.0.0" }
but I got these errors
Starting build... C:\msys64\mingw64\bin\g++.exe -fdiagnostics-color=always -g E:\3_Document\Codes\C++\GameOfLife\main.cpp -I C:/Qt/6.5.0/mingw_64/include -L C:/Qt/6.5.0/mingw_64/lib -L C:/Qt/6.5.0/mingw_64/lib/cmake -o E:\3_Document\Codes\C++\GameOfLife\main.exe C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\aas\AppData\Local\Temp\ccUP2mrt.o: in function `main': E:/3_Document/Codes/C++/GameOfLife/main.cpp:5: undefined reference to `__imp__ZN12QApplicationC1ERiPPci' C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: E:/3_Document/Codes/C++/GameOfLife/main.cpp:6: undefined reference to `__imp__ZN11QPushButtonC1ERK7QStringP7QWidget' C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: E:/3_Document/Codes/C++/GameOfLife/main.cpp:7: undefined reference to `__imp__ZN7QWidget4showEv' C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: E:/3_Document/Codes/C++/GameOfLife/main.cpp:8: undefined reference to `__imp__ZN12QApplication4execEv' C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: E:/3_Document/Codes/C++/GameOfLife/main.cpp:9: undefined reference to `__imp__ZN11QPushButtonD1Ev' C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: E:/3_Document/Codes/C++/GameOfLife/main.cpp:9: undefined reference to `__imp__ZN12QApplicationD1Ev' C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: E:/3_Document/Codes/C++/GameOfLife/main.cpp:9: undefined reference to `__imp__ZN11QPushButtonD1Ev' C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: E:/3_Document/Codes/C++/GameOfLife/main.cpp:9: undefined reference to `__imp__ZN12QApplicationD1Ev' C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\aas\AppData\Local\Temp\ccUP2mrt.o:C:/Qt/6.5.0/mingw_64/include/QtCore/qversiontagging.h:125: undefined reference to `__imp_qt_version_tag_6_5' C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\aas\AppData\Local\Temp\ccUP2mrt.o: in function `QString::QString(char const*)': C:/Qt/6.5.0/mingw_64/include/QtCore/qstring.h:1029: undefined reference to `__imp__ZN7QString8fromUtf8E14QByteArrayView' C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\aas\AppData\Local\Temp\ccUP2mrt.o: in function `QByteArrayView::QByteArrayView<char, true>(char const*, long long)': C:/Qt/6.5.0/mingw_64/include/QtCore/qbytearrayview.h:137: undefined reference to `__imp__Z9qt_assertPKcS0_i' C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Qt/6.5.0/mingw_64/include/QtCore/qbytearrayview.h:137: undefined reference to `__imp__Z9qt_assertPKcS0_i' C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\aas\AppData\Local\Temp\ccUP2mrt.o: in function `QtPrivate::QPodArrayOps<char16_t>::destroyAll()': C:/Qt/6.5.0/mingw_64/include/QtCore/qarraydataops.h:95: undefined reference to `__imp__Z9qt_assertPKcS0_i' C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Qt/6.5.0/mingw_64/include/QtCore/qarraydataops.h:96: undefined reference to `__imp__Z9qt_assertPKcS0_i' C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\aas\AppData\Local\Temp\ccUP2mrt.o: in function `QTypedArrayData<char16_t>::deallocate(QArrayData*)': C:/Qt/6.5.0/mingw_64/include/QtCore/qarraydata.h:124: undefined reference to `__imp__ZN10QArrayData10deallocateEPS_xx' collect2.exe: error: ld returned 1 exit status Build finished with error(s).
how I can solve it?
-
I wrote this program in vs-code for testing.
#include <QtWidgets/QApplication> #include <QtWidgets/QPushButton> int main(int argc, char **argv){ QApplication app (argc, argv); QPushButton button ("Hello world !"); button.show(); return app.exec(); }
the c_cpp_properties.json file is:
{ "configurations": [ { "name": "Win32", "includePath": [ "${workspaceFolder}/**", "C:/Qt/6.5.0/mingw_64/include" ], "defines": [ "_DEBUG", "UNICODE", "_UNICODE" ], "compilerPath": "C:\\msys64\\mingw64\\bin\\gcc.exe", "cStandard": "c17", "cppStandard": "gnu++17", "intelliSenseMode": "windows-gcc-x64" } ], "version": 4 }
and the tasks.json file is:
{ "tasks": [ { "type": "cppbuild", "label": "C/C++: g++.exe build active file", "command": "C:\\msys64\\mingw64\\bin\\g++.exe", "args": [ "-fdiagnostics-color=always", "-g", "${file}", "-I", "C:/Qt/6.5.0/mingw_64/include", "-L", "C:/Qt/6.5.0/mingw_64/lib", "-o", "${fileDirname}\\${fileBasenameNoExtension}.exe" ], "options": { "cwd": "${fileDirname}" }, "problemMatcher": [ "$gcc" ], "group": { "kind": "build", "isDefault": true }, "detail": "Task generated by Debugger." } ], "version": "2.0.0" }
but I got these errors
Starting build... C:\msys64\mingw64\bin\g++.exe -fdiagnostics-color=always -g E:\3_Document\Codes\C++\GameOfLife\main.cpp -I C:/Qt/6.5.0/mingw_64/include -L C:/Qt/6.5.0/mingw_64/lib -L C:/Qt/6.5.0/mingw_64/lib/cmake -o E:\3_Document\Codes\C++\GameOfLife\main.exe C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\aas\AppData\Local\Temp\ccUP2mrt.o: in function `main': E:/3_Document/Codes/C++/GameOfLife/main.cpp:5: undefined reference to `__imp__ZN12QApplicationC1ERiPPci' C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: E:/3_Document/Codes/C++/GameOfLife/main.cpp:6: undefined reference to `__imp__ZN11QPushButtonC1ERK7QStringP7QWidget' C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: E:/3_Document/Codes/C++/GameOfLife/main.cpp:7: undefined reference to `__imp__ZN7QWidget4showEv' C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: E:/3_Document/Codes/C++/GameOfLife/main.cpp:8: undefined reference to `__imp__ZN12QApplication4execEv' C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: E:/3_Document/Codes/C++/GameOfLife/main.cpp:9: undefined reference to `__imp__ZN11QPushButtonD1Ev' C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: E:/3_Document/Codes/C++/GameOfLife/main.cpp:9: undefined reference to `__imp__ZN12QApplicationD1Ev' C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: E:/3_Document/Codes/C++/GameOfLife/main.cpp:9: undefined reference to `__imp__ZN11QPushButtonD1Ev' C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: E:/3_Document/Codes/C++/GameOfLife/main.cpp:9: undefined reference to `__imp__ZN12QApplicationD1Ev' C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\aas\AppData\Local\Temp\ccUP2mrt.o:C:/Qt/6.5.0/mingw_64/include/QtCore/qversiontagging.h:125: undefined reference to `__imp_qt_version_tag_6_5' C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\aas\AppData\Local\Temp\ccUP2mrt.o: in function `QString::QString(char const*)': C:/Qt/6.5.0/mingw_64/include/QtCore/qstring.h:1029: undefined reference to `__imp__ZN7QString8fromUtf8E14QByteArrayView' C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\aas\AppData\Local\Temp\ccUP2mrt.o: in function `QByteArrayView::QByteArrayView<char, true>(char const*, long long)': C:/Qt/6.5.0/mingw_64/include/QtCore/qbytearrayview.h:137: undefined reference to `__imp__Z9qt_assertPKcS0_i' C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Qt/6.5.0/mingw_64/include/QtCore/qbytearrayview.h:137: undefined reference to `__imp__Z9qt_assertPKcS0_i' C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\aas\AppData\Local\Temp\ccUP2mrt.o: in function `QtPrivate::QPodArrayOps<char16_t>::destroyAll()': C:/Qt/6.5.0/mingw_64/include/QtCore/qarraydataops.h:95: undefined reference to `__imp__Z9qt_assertPKcS0_i' C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Qt/6.5.0/mingw_64/include/QtCore/qarraydataops.h:96: undefined reference to `__imp__Z9qt_assertPKcS0_i' C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\aas\AppData\Local\Temp\ccUP2mrt.o: in function `QTypedArrayData<char16_t>::deallocate(QArrayData*)': C:/Qt/6.5.0/mingw_64/include/QtCore/qarraydata.h:124: undefined reference to `__imp__ZN10QArrayData10deallocateEPS_xx' collect2.exe: error: ld returned 1 exit status Build finished with error(s).
how I can solve it?
@kamran78nasirizad
Probably the widget library is not linked in the CMake file (or not added if you use qmake).
Can you show the CMakeLists.txt or the .pro file? -
@kamran78nasirizad
Probably the widget library is not linked in the CMake file (or not added if you use qmake).
Can you show the CMakeLists.txt or the .pro file?wrote on 31 Aug 2023, 16:24 last edited by@Axel-Spoerl
there is no .txt or .pro file in project -
C Christian Ehrlicher moved this topic from General and Desktop on 31 Aug 2023, 16:29
-
@Axel-Spoerl
there is no .txt or .pro file in project@kamran78nasirizad
Oh, sorry, I completely overlooked that you are using vscode. The issue remains the same: the Qt shared libraries are not linked. But I do not know vscode at all. Maybe this helps. -
wrote on 1 Sept 2023, 10:25 last edited by
You need to either use both MinGW and Qt from Msys2 or from Qt.
Currently you are mixing the compiler from Msys2 and Qt from the Qt SDK.
Please do install MinGW from the Qt SDK and you should not have compatibility issues.
But instead of using
-I
and-L
with the Qtinclude
andlib
paths you might want to use a CMake preset with vscode. Or just use Qt Creator 😅 -
wrote on 1 Sept 2023, 10:54 last edited by 8Observer8 9 Jan 2023, 11:23
I use Sublime Text 4 for regular coding as it requires the least resources of my laptop. My laptop works silently with this editor. It helps me in learning Qt because there is no autocomplete in the editor. There is an autocomplete plugin for Sublime Text 4 but I don't want to use it. I use Qt Creator when I need to debug a program, or when I need to create an APK (Android executable), or when I need to debug a program on an Android emulator.
You need to check how your application compiles from the console:
- Add a target name to your .pro file:
TARGET = app
- Navigate to your project folder from the console and type:
qmake -makefile
- Compile your code:
mingw32-make
- Run the application:
release/app
I think you will have the same errors as above. Add these paths to the Path variable:
C:\Qt\6.2.4\mingw_64\bin
C:\Qt\Tools\mingw1120_64\binP.S. To open a window with your Path variable, you can copy this command:
rundll32 sysdm.cpl,EditEnvironmentVariables
and paste it into the Far Manager console. In this case, you can very quickly open a window with your Path variable by typingrun
in the Far Manager console, and autocomplete will add the rest of the command, since Far Manager will save the command after the first time the command is run.P.S.S If you need to output something to the console with
std::cout
from Windows from a program that you run from the console, you need to add this code to main.cpp:#ifdef _WIN32 #include <windows.h> #endif #include "main_window.h" #include <QtWidgets/QApplication> int main(int argc, char *argv[]) { #ifdef _WIN32 if (AttachConsole(ATTACH_PARENT_PROCESS)) { freopen("CONOUT$", "w", stdout); freopen("CONOUT$", "w", stderr); } #endif QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); }
- Add a target name to your .pro file:
-
You need to either use both MinGW and Qt from Msys2 or from Qt.
Currently you are mixing the compiler from Msys2 and Qt from the Qt SDK.
Please do install MinGW from the Qt SDK and you should not have compatibility issues.
But instead of using
-I
and-L
with the Qtinclude
andlib
paths you might want to use a CMake preset with vscode. Or just use Qt Creator 😅wrote on 3 Sept 2023, 17:15 last edited by@cristian-adam
how I must install minGW from QT SDK and how I must use CMake preset in vs-code?
my Qt Creator free trial is finished and I no longer have access to it and as I know it is not free any more -
@cristian-adam
how I must install minGW from QT SDK and how I must use CMake preset in vs-code?
my Qt Creator free trial is finished and I no longer have access to it and as I know it is not free any more@kamran78nasirizad said in linker error in vs code:
Qt Creator free trial
QtCreator is open source and free to use for everyone.
To install MinGW simply use Qt Maintenance Tool from your Qt installation. -
@kamran78nasirizad said in linker error in vs code:
Qt Creator free trial
QtCreator is open source and free to use for everyone.
To install MinGW simply use Qt Maintenance Tool from your Qt installation.wrote on 6 Sept 2023, 18:36 last edited by@jsulm when I open the Qt creator this message shown.
and if I click on close the entire program would close -
@jsulm when I open the Qt creator this message shown.
and if I click on close the entire program would closewrote on 6 Sept 2023, 18:53 last edited by@kamran78nasirizad
I think you need to explain exactly what/how you installed Qt. Also had you installed any other version of Qt on that machine at an earlier date? -
@jsulm when I open the Qt creator this message shown.
and if I click on close the entire program would close@kamran78nasirizad Please explain how you installed QtCreator...
-
wrote on 7 Sept 2023, 17:03 last edited by
I went to the Qt.io website, and clicked on the "Download. Try." button, then in "https://www.qt.io/download" I clicked on "Download Qt for Windows", and after completing the form and downloading the Qt installer ran it.
Is this the wrong method to download and install Qt and Qt Creator?
-
@kamran78nasirizad
I think you need to explain exactly what/how you installed Qt. Also had you installed any other version of Qt on that machine at an earlier date?wrote on 7 Sept 2023, 17:32 last edited by@JonB No the system was fresh before installing this Qt.
-
I went to the Qt.io website, and clicked on the "Download. Try." button, then in "https://www.qt.io/download" I clicked on "Download Qt for Windows", and after completing the form and downloading the Qt installer ran it.
Is this the wrong method to download and install Qt and Qt Creator?
@kamran78nasirizad said in linker error in vs code:
Is this the wrong method to download and install Qt and Qt Creator?
You should go to https://www.qt.io/download-open-source and download the installer from there.
1/14