安装了以下内容:
- Visual Studio 代码 (1.45.1)
- Visual Studio 2019 Community(为了使用 MSVC
cl.exe
编译器)
- Qt 5.15.0(安装到 C:\Qt)
Visual Studio Code运行后已启动Visual Studio Command Prompt
以便正确设置环境cl.exe
. The ms-vscode.cpptools
扩展已安装在 Visual Studio Code 中并且includePath
被设定为:
"includePath": [
"${workspaceFolder}/**",
"${INCLUDE}",
"C:/Qt/5.15.0/msvc2019_64/include/**"
],
这个文件hw.cpp
编译并运行良好:
#include <iostream>
int main()
{
std::cout << "Hello world!";
return 0;
}
该命令使用于tasks.json
is:
"command": "cl.exe",
"args": [
"/Zi",
"/EHsc",
"/Fe:",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"${file}"
],
或者,从 Visual Studio Code 内的内置终端中,命令“cl /EHsc /MD /O2 hw.cpp /link /out:hw.exe”正确编译所有内容并hw.exe
可以被执行。
但是当我尝试按如下方式使用 Qt 时,它无法编译:
#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).
我缺少什么?
UPDATE
感谢@rioV8的评论,我已经调查过了/link
选项cl.exe
。任务参数已更新为:
"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"
],
这稍微改善了情况。hw.cpp
现在编译并生成hw.obj
,但现在我收到链接器错误(每个错误一个.lib
):
warning LNK4272: library machine type 'x64' conflicts with target machine type 'x86'
接下来是fatal error LNK1120: 2 unresolved externals
.
越来越近,但仍然没有联系。