Using Qt with Visual Studio Code (Windows)

Viewed 8100

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.

1 Answers
Related