The following are installed:
cl.exe compiler)Visual Studio Code has been launched after running Visual Studio Command Prompt so that the environment is set correctly for cl.exe. The ms-vscode.cpptools extension has been installed in Visual Studio Code and includePath is set to:
"includePath": [
"${workspaceFolder}/**",
"${INCLUDE}",
"C:/Qt/5.15.0/msvc2019_64/include/**"
],
This file hw.cppcompiles and runs fine:
#include <iostream>
int main()
{
std::cout << "Hello world!";
return 0;
}
The command used in tasks.json is:
"command": "cl.exe",
"args": [
"/Zi",
"/EHsc",
"/Fe:",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"${file}"
],
Alternatively, from the in-built Terminal inside Visual Studio Code, the command "cl /EHsc /MD /O2 hw.cpp /link /out:hw.exe" compiles everything correctly and hw.exe can be executed.
But when I attempt to use Qt as follows it fails to compile:
#include <QString>
int main()
{
QString test("Hello world!");
qDebug() << test;
return 0;
}
The compiler reports "fatal error C1083: Cannot open include file: 'QString': No such file or directory". IntelliSense does find QString.h, which opens when I press Ctrl and click QString (at the top).
What am I missing?
UPDATE
Thanks to comments from @rioV8, I've investigated /link options for cl.exe. The task arguments have been updated to:
"args": [
"/EHsc",
"/MD",
"/O2",
"/IC:\\Qt\\5.15.0\\msvc2019_64\\include",
"/IC:\\Qt\\5.15.0\\msvc2019_64\\include\\QtCore",
"${file}",
"/link",
"/LIBPATH:C:\\Qt\\5.15.0\\msvc2019_64\\lib",
"Qt5Core.lib",
"qtmain.lib",
"/OUT:${fileDirname}\\${fileBasenameNoExtension}.exe"
],
This has improved things slightly. hw.cpp now compiles and generates hw.obj, but now I get linker errors (one for each .lib):
warning LNK4272: library machine type 'x64' conflicts with target machine type 'x86'
This is followed by fatal error LNK1120: 2 unresolved externals.
Getting closer, but still not linking.
There has been a detailled guide on the KDAB blog recently.
Overview of VS Code for Qt developers:
Detailled instructions:
"VS Code for Qt Applications – Part 2" summarizes the basic setup for both QMake and CMake projects.
The easiest way to setup the build (that the OP was struggling with) is to use CMake, which is Qt's suggested build system as of now (2023) with Qt 6. Just install the cmake extension, auto-configure your project and make sure that it finds your Qt installation and a compatible C++ toolchain.
To make sure the correct Qt is found, you can add the installation path to CMAKE_PREFIX_PATH, which can be defined via cmake.configureSettings of the cmake tools plugin.
It also makes very much sense to configure a shadow build this way:
"cmake.buildDirectory": "${workspaceFolder}/../build-cmake-project"
That gives you a basic setup to work with. There's a lot more fine-tuning in the linked blog posts, but whatever you do, be aware that VS Code is not a full IDE for Qt projects like Qt Creator: You miss the designer and editing support for resource files, working with QML is not fully supported, and you'll have to do some manual steps for every new project you open.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With