To create C++ with VSCode, install the Visual C++ build tool in Visual studio build tools, set environment variables, and press Build
> Executing task: cl.exe /Zi /EHsc /Fe: c:\Users\Administrator\Desktop\C++\hello.exe c:\Users\Administrator\Desktop\C++\hello.cpp <
Microsoft(R) C/C++ Optimization Compiler Version 19.26.28806 (x64)
Copyright (c) Microsoft Corporation. All rights reserved.
hello.cpp
c:\Users\Administrator\Desktop\C++\hello.cpp(1):fatal error C1034: iostream: No inclusion path has been set.
Terminal process terminated with exit code 1
I don't know what the problem is because it's printed like this.
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "C/C++: cl.exe build active file",
"command": "cl.exe",
"args": [
"/Zi",
"/EHsc",
"/Fe:",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"${file}"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$msCompile"
],
"group": "build"
}
]
}
The problem is caused by insufficient environment variable settings.
VsDevCmd.bat
automatically sets the environment variable for building C++.
You can run it before you enter the cl.exe
command, but you can create the task as shown below.
{
"type": "shell",
"label": "C/C++: cl.exe build active file",
"command": "%BUILD%",
"args": [
"/Zi",
"/EHsc",
"/Fe:",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"${file}"
],
"options": {
"cwd": "${workspaceFolder}",
"env": {
"PATH": "${env:PATH};C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\Common7\\Tools",
"BUILD": "VsDevCmd.bat && cl.exe"
}
},
"problemMatcher": [
"$msCompile"
],
"group": {
"kind": "build",
"isDefault": true
}
}
Where C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\Community\\Common7\\Tools
can be replaced by the path where VsDevCmd.bat
is located, depending on your development environment.
Personally, I don't think it's a good idea to use cl.exe
right away.
One of the reasons C++ has a high barrier to entry is that the build system is fragmented and not unified.
To build the same source code, it is very difficult to write different commands for each OS and toolchain.
There are several ways to resolve this issue, including CMake.
In Visual Studio Code, the CMake Tools extension is available, and if you create the CMakeLists.txt
file as follows, you will configure the main.cpp
file named created in the same directory.
cmake_minimum_required(VERSION 3.4)
add_executable(myprogram main.cpp)
Run the >cmake configure
command once for the first time in the Visual Studio Code.
The configuration command succeeds and can be built using the F7
key or the >cmake build
command for future builds.
Debugging can be done via the Ctrl + F5
or >cmake debug
command.
© 2024 OneMinuteCode. All rights reserved.