Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Qt with Visual Studio Code (Windows)

The following are installed:

  • Visual Studio Code (1.45.1)
  • Visual Studio 2019 Community (in order to use the MSVC cl.exe compiler)
  • Qt 5.15.0 (installed to C:\Qt)

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.

like image 556
AlainD Avatar asked Oct 24 '25 00:10

AlainD


1 Answers

There has been a detailled guide on the KDAB blog recently.

Overview of VS Code for Qt developers:

  • https://www.kdab.com/using-visual-studio-code-for-writing-qt-applications/

Detailled instructions:

  • https://www.kdab.com/using-visual-studio-code-for-qt-apps-pt-1/
  • https://www.kdab.com/using-visual-studio-code-for-qt-apps-pt-2/
  • https://www.kdab.com/using-vsc-for-qt-apps-part-3/

"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.

like image 99
FourtyTwo Avatar answered Oct 26 '25 16:10

FourtyTwo