Opencv C++ MinGW VSCode fatal error to compile - c++

Hi all I want to use VSCode MinGW C++ and OpenCV to create a simple opencv project but for an unknown reason, I get this error what should I do?
I want to mention that in Visual studio 2017 works I can run the main.cpp on x64 architecture.
The code below is what I want to run on VSCode is the same code I run on the Visual Studio 2017.
After 10 days of trying I give up 50 points if someone proves that with:
VSCODE
C++17
Opencv
on Windows 10 x64 Architecture
A successful build.
src/main.cpp
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main() {
Mat image;
image = imread("./22.png", IMREAD_COLOR); // Read the file
namedWindow("Display window", WINDOW_AUTOSIZE); // Create a window for display.
if (!image.data) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl;
} else { // Image is good!
imshow("Display window", image); // Show our image inside it.
}
waitKey(0);
return 0;
}
In my VSCode editor, I try to build the application using the tasks.json with CTRL + SHIFT + B
tasks.json
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "echo",
"type": "shell",
"command": "",
"args": [
"g++",
"-I", "C:\\vcpkg\\installed\\x64-windows\\include",
"-L", "C:\\vcpkg\\installed\\x64-windows\\lib",
"./src/main.cpp",
"-lopencv_core341",
"-lopencv_highgui341",
"-o app"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
c_cpp_properties.json
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"${workspaceFolder}/src",
"C:/vcpkg/installed/x64-windows/include",
"C:/vcpkg/installed/x64-windows/lib"
],
"browse": {
"path": [
],
"limitSymbolsToIncludedHeaders": true
},
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.17134.0",
"compilerPath": "C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.14.26428/bin/Hostx64/x64/cl.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "msvc-x64",
"C_Cpp.intelliSenseEngine": "Tag Parser"
}
],
"version": 4
}
And I get this error
> Executing task: g++ main.cpp -I C:/vcpkg/installed/x64-windows/include -L C:/vcpkg/installed/x64-windows/lib -lopencv_core341 -lopencv_highgui341 -o app <
C:\Users\giorg\AppData\Local\Temp\ccNFIHQq.o:main.cpp:(.text+0x51): undefined reference to `cv::imread(cv::String const&, int)'
C:\Users\giorg\AppData\Local\Temp\ccNFIHQq.o:main.cpp:(.text+0xa2): undefined reference to `cv::namedWindow(cv::String const&, int)'
C:\Users\giorg\AppData\Local\Temp\ccNFIHQq.o:main.cpp:(.text+0x119): undefined reference to `cv::imshow(cv::String const&, cv::_InputArray const&)'
C:\Users\giorg\AppData\Local\Temp\ccNFIHQq.o:main.cpp:(.text+0x139): undefined reference to `cv::waitKey(int)'
C:\Users\giorg\AppData\Local\Temp\ccNFIHQq.o:main.cpp:(.text$_ZN2cv6StringC1EPKc[__ZN2cv6StringC1EPKc]+0x42): undefined reference to `cv::String::allocate(unsigned int)'
C:\Users\giorg\AppData\Local\Temp\ccNFIHQq.o:main.cpp:(.text$_ZN2cv6StringD1Ev[__ZN2cv6StringD1Ev]+0xf): undefined reference to `cv::String::deallocate()'
C:\Users\giorg\AppData\Local\Temp\ccNFIHQq.o:main.cpp:(.text$_ZN2cv3MatD1Ev[__ZN2cv3MatD1Ev]+0x2d): undefined reference to `cv::fastFree(void*)'
C:\Users\giorg\AppData\Local\Temp\ccNFIHQq.o:main.cpp:(.text$_ZN2cv3Mat7releaseEv[__ZN2cv3Mat7releaseEv]+0x40): undefined reference to `cv::Mat::deallocate()'
C:\Users\giorg\AppData\Local\Temp\ccNFIHQq.o:main.cpp:(.text$_ZN2cv3MataSEOS0_[__ZN2cv3MataSEOS0_]+0xb4): undefined reference to `cv::fastFree(void*)'
collect2.exe: error: ld returned 1 exit status
The terminal process terminated with exit code: 1
Terminal will be reused by tasks, press any key to close it.
I have generated the opencv libs using the vcpkg using this command vcpkg install opencv and I think is doing the job very well.
After I generated all the files with the vspkg I test all the files with Visual Studio 2017 and is works, but my main goal is to make work with the VSCode but I don't have any clue why I get the errors.
To show you that I have generated the files.
C:\vcpkg\installed\x64-windows\include
C:\vcpkg\installed\x64-windows\include\opencv2
C:\vcpkg\installed\x64-windows\lib

I have success trying to build and run a C++ program with OpenCV 3.4.1 using MinGW-w64 g++ in Visual Studio Code on Windows 10 x64. But I don't know if this is actually the "right" way to do it.
Screenshot
Download and Install MinGW-w64 choose x86_64-8.1.0-posix-seh-rt_v6-rev0 version or newer http://mingw-w64.org/doku.php/start.
Open CMD and go to C:\ then clone this repo https://github.com/huihut/OpenCV-MinGW-Build then checkout the OpenCV-3.4.1-x64 branch or just execute this git clone https://github.com/huihut/OpenCV-MinGW-Build.git -b OpenCV-3.4.1-x64
Add Mingw-w64 bin folder to the system path (For me the path is C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin)
Add OpenCV mingw bin folder that you clone earlier to the system path (For me the path is C:\OpenCV-MinGW-Build\x64\mingw\bin)
For now you can already build a program using g++ using CMD just type g++ test.cpp -IC:\OpenCV-MinGW-Build\include -LC:\OpenCV-MinGW-Build\x64\mingw\bin -llibopencv_calib3d341 -llibopencv_core341 -llibopencv_dnn341 -llibopencv_features2d341 -llibopencv_flann341 -llibopencv_highgui341 -llibopencv_imgcodecs341 -llibopencv_imgproc341 -llibopencv_ml341 -llibopencv_objdetect341 -llibopencv_photo341 -llibopencv_shape341 -llibopencv_stitching341 -llibopencv_superres341 -llibopencv_video341 -llibopencv_videoio341 -llibopencv_videostab341 just change the test.cpp to your .cpp file name.
Open your .cpp file using Visual Studio Code then install Code Runner extension https://marketplace.visualstudio.com/items?itemName=formulahendry.code-runner, I use Code Runner to easily run my program.
Press Ctrl + comma in Visual Studio Code then search for "code-runner.executorMap": and change the content on"cpp:" entry to "cd $dir && g++ $fileName -o $fileNameWithoutExt -IC:/OpenCV-MinGW-Build/include -LC:/OpenCV-MinGW-Build/x64/mingw/bin -llibopencv_calib3d341 -llibopencv_core341 -llibopencv_dnn341 -llibopencv_features2d341 -llibopencv_flann341 -llibopencv_highgui341 -llibopencv_imgcodecs341 -llibopencv_imgproc341 -llibopencv_ml341 -llibopencv_objdetect341 -llibopencv_photo341 -llibopencv_shape341 -llibopencv_stitching341 -llibopencv_superres341 -llibopencv_video341 -llibopencv_videoio341 -llibopencv_videostab341 && $dir$fileNameWithoutExt", you can apply this for user settings or just for workspace settings.
`
After that just press Ctrl+Alt+N to Build and Run your program or just press the play button.
I believe you can also apply this to the tasks.json by adding the -IC:\OpenCV-MinGW-Build\include .. to the args part to use the Visual Studio Code C/C++ extension debugging. Also you can add "C:/OpenCV-MinGW-Build/include" to the c_cpp_properties.json include path to enable intellisense. If you want to use another version of OpenCV just replace all the "341" part of the g++ argument to another version for example to use OpenCV 3.3.1 use -llibopencv_calib3d331 and so on.
EDIT: I have try to edit my tasks.json and launch.json and it works with gdb debugger.
tasks.json : https://gist.github.com/agtbaskara/4a2ec9a3a9a963069e719c0477185321
launch.json : https://gist.github.com/agtbaskara/de04db8b6a31522dd1e62c43aa6e0f89
c_cpp_properties.json : https://gist.github.com/agtbaskara/0eb773ac3085557baf7bf20b031fb49e

Here are instructions for setting up OpenCV in MSYS2/mingw64. I can't help with VsCode but perhaps this will help you make progress, or help other people who find this question by searching.
You don't need to actually build OpenCV yourself; there are prepackaged binaries for MinGW-w64. The purpose of MSYS2 is to provide a unix-style shell and act as a package manager. If you haven't used MSYS2 before:
Install MSYS2 and update to latest as shown here.
Open a MSYS2/mingw64 shell (not a MSYS2/msys2 shell). Binaries built in this shell will run as standalone Windows binaries, they do not depend on the MSYS2 environment.
Test that g++ works.
Then you can add OpenCV:
pacman -Ss mingw64/mingw-w64-x86_64-opencv
and it's all ready to go. I compiled your sample program using the following command in MSYS2 shell:
g++ -o main main.cpp -std=c++17 -lopencv_core -lopencv_highgui -lopencv_imgcodecs -lopencv_imgproc
and ran the program successfully.
Note that you do not need to use MSYS2 for the build environment; you can invoke g++ from Windows Command Prompt or from any other IDE if you set up the Include and Library paths to point to where MSYS2's package manager installed the headers and libraries. (Which is under /mingw64/include/opencv* and /mingw64/lib , under the MSYS2 installation root).

Install vcpkg ( MS packager to install windows based open source projects) and use powershell command .\vcpkg install opencv:x64-windows-static. Dependency libraries will be auto installed for you.
Opencv can be auto integrated into your VS project using .\vcpkg integrate install.

Related

Compiling a cpp file with vscode, in Ubuntu

I'm trying to follow this link on how to get started with c++ and vscode in ubuntu.
I have gcc already installed with the latest version.
Running sudo apt-get install build-essential gdb gives:
Reading package lists... Done
Building dependency tree
Reading state information... Done
build-essential is already the newest version (12.8ubuntu1.1).
build-essential set to manually installed.
gdb is already the newest version (9.2-0ubuntu1~20.04.1).
gdb set to manually installed.
0 upgraded, 0 newly installed, 0 to remove and 2 not upgraded.
However, when I get to the stage of creating the config file, I have no option for C/C++: g++ build active file. I only have
So, I choose /usr/bin/cpp.Then I build the file, and get the success message.
However, when run the newly created executable file, I get several error messages:
./helloworld: line 17: namespace: command not found
./helloworld: line 23: syntax error near unexpected token `('
./helloworld: line 23: ` typedef decltype(nullptr) nullptr_t;'
the strange thing is that the lines with code in the helloworld file end on line 16, so I think there's something wrong with the compiler...
Its best to get GCC working in your commandline, then get it working using VS Code tasks.
I suggest that you create the most simplistic project structure you can. Use only a project directory, and a single file named main.cpp.
Something that looks like this:
PROJECT (dir) // path = ./
│
└──> main.cpp (file) // path = ./main.cpp
Once you have a directory with main.cpp do 1 of 2 things:
Use the following command to add a Hello World example to your main.cpp file.
$> echo -e "\n#include <iostream>\n\nusing namespace std;\n\nint main()\n{\n cout << \"Hello World\!\" << endl;\n}" > main.cpp
Or copy and paste the code below into main.cpp
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!" << endl;
}
FYI: You should be doing this from the command-line not vscode (not until you create the vscode task which I will show bellow)
Another thing to note, is your commandline should be pointed to your project directory, the directory you created with main.cpp in it.
From inside your project directory execute the following command.
$> g++ ./main.cpp -o build
if your file compiled & built your executable correctly you should be able to use the ls command to see a new file named build in your project directory.
If all went well, the new build file is an executable. Execute it by entering...
$> ./build
and you should see "Hello World!"
At this point use the following command...
$> code .
VS Code should open to your projects directory.
Now using vscode create another directory, and name it ./.vscode
Then add a file to the ./.vscode directory named tasks.json
The files full pathname will be: ./.vscode/tasks.json
then you will want to add the following to your tasks file
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "GCC: My Compilation Task",
"command": "/usr/bin/g++",
"args": ["-g", "./main.cpp", "-o", "./build"],
"options": {
"cwd": "/usr/bin"
},
"problemMatcher": ["$gcc"],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
then you should be able to press F1 and type RUN TASK, when you see the option in the quick menu that says RUN TASK click it, and then select the tasks with the same name as the label key in your tasks.json file, which is "GCC: My Compilation Task"

Windows: fatal error: gtkmm.h: No such file or directory

I'm complety despairing compiling my C++ program using Gtkmm3..
I read about 30 different Stackoverflow, Microsoft and other forum entries but nothing works.
The problems were:
Using vcpkg doesn't work since I need Gtkmm3 not 4.
Using VS Studio fails to generate the cache.
Using VS Code doesn't find gtkmm.h (see error message in the title) although I did EVERYTHING this page told me to do: https://wiki.gnome.org/Projects/gtkmm/MSWindows several times in several folders. And of course I have set the right Path in the Windows settings.
The compiler used (terminal told me) is in: D:\msys64\mingw64\bin\g++.exe
I also edited the launch.json with:
{
"version": "0.2.1",
"tasks": [
{
"taskName": "build-all",
"appliesTo": "example.cpp",
"contextType": "build",
"type": "launch",
"command": "${env.comspec}",
"args": [
"g++ -std=c++17 -o example.exe -g example.cpp simple `pkg-config gtkmm-3.0 --cflags --libs`"
]
}
]
}
I also tried compiling the .exe with Clion with standard CMakeLists.txt I wrote but that doesn't work as well cause Clion can't find either pkgConfig or gtkmm.h.
I tried CodeBlocks with specific Path setting to MinGW.
Someone on StackOverflow wrote a script for installing Gtkmm3 using MinGW in the terminal (Can't find the link for now sorry)... But of course: It didn't work!
So after almost infinity attemps, 4-5 days of trying I gave up now. Hopefully someone can help me here having the same problem.

Compiling C/C++ in VS Code

I'm trying to compile C/C++ code in VS Code using cl (installed via visual studio 2019). I've set up the json files like the MS website suggests,
https://code.visualstudio.com/docs/cpp/config-msvc,
but I still get the error:
cl.exe : The term 'cl.exe' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
Here are my json files:
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.17763.0",
"compilerPath": "C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.21.27702/bin/Hostx64/x64/cl.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "msvc-x64"
}
],
"version": 4
}
{
"version": "2.0.0",
"tasks": [
{
"label": "msvc build",
"type": "shell",
"command": "cl.exe",
"args": [
"/EHsc",
"/Zi",
"/Fe:",
"helloworld.exe",
"test.c"
],
"group": {
"kind": "build",
"isDefault": true
},
"presentation": {
"reveal":"always"
},
"problemMatcher": "$msCompile"
}
]
}
cl.exe : The term 'cl.exe' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
Your "msvc build" task only specifies "cl.exe" for its command, with no leading path. The problem is that cl.exe isn't on your PATH, or anywhere that VS Code can see when it goes to run your build task.
One solution to this is to open VS Code using the "Developer Command Prompt" for whatever Visual Studio version you have. This version of the command prompt defines the location of the Visual Studio build tools so that any program or command that is run from that cmd prompt will be able to find the programs like "cl.exe".
There is another solution that I prefer to use, which is to write a batch script for your build task.
Put this script in the root of your VSCode workspace and call it build.bat:
:: set the path to your visual studio vcvars script, it is different for every version of Visual Studio.
set VS2017TOOLS="C:\Program Files(x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvars64.bat"
:: make sure we found them
if not exist %VS2017TOOLS% (
echo VS 2017 Build Tools are missing!
exit
)
:: call that script, which essentially sets up the VS Developer Command Prompt
call %VS2017TOOLS%
:: run the compiler with your arguments
cl.exe /EHsc /Zi /Fe: helloworld.exe test.c
exit
Then your task has to be changed to run the batch script:
{
"label": "msvc build",
"type": "shell",
"command": "${workspaceFolder}/build.bat",
"group": {
"kind": "build",
"isDefault": true
},
"presentation": {
"reveal":"always"
},
"problemMatcher": "$msCompile"
}
The advantage to using the batch script this way is that you do not need to run VS Code from the Developer Command Prompt, and the $msCompile problem matcher will still be able to display errors and warnings inside VS Code.
Another note is that the batch script in this answer is specific to just your project. You can continue to update that script to build your project as it gets larger, or you could take advantage of a tool like CMake to handle the generation of the actual build scripts from a configuration file.
Then your build.bat may look more like this:
:: set the path to your visual studio vcvars script, it is different for every version of Visual Studio.
set VS2017TOOLS="C:\Program Files(x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvars64.bat"
:: make sure we found them
if not exist %VS2017TOOLS% (
echo VS 2017 Build Tools are missing!
exit
)
:: call that script, which essentially sets up the VS Developer Command Prompt
call %VS2017TOOLS%
:: Set some variables for the source directory and the build directory
set SrcDir=%CD%
set BuildDir=%CD%\build
:: Make the build directory if it doesn't exist
if not exist "%BuildDir%" mkdir "%BuildDir%"
:: Make sure you configure with CMake from the build directory
cd "%BuildDir%"
:: Call CMake to configure the build (generates the build scripts)
cmake %SrcDir%^
-D CMAKE_C_COMPILER=cl.exe^
-D CMAKE_CXX_COMPILER=cl.exe^
:: Call CMake again to build the project
cmake --build %BuildDir%
exit
I have struggled in the same problem as following the instructions in Configure VS Code for Microsoft C++. Thankfully I found this question and #Romen's great answer.
However, here are some itchy points in his solution:
He hard-coded the executable name and the file name so that you have to change it every time you make a new project.
He put the .bat in the folder of the compiled file but not the .vscode folder so that we couldn't copy one folder directly while making new projects.
For this two flecks, I modified his codes a little bit so that it's more convenient to make new projects debugging with Microsoft C++.
In the build.bat file, change the command
cl.exe /EHsc /Zi /Fe: helloworld.exe helloworld.cpp
to
cl.exe /EHsc /Zi /Fe: %1.exe %1.cpp
Move the build.bat to .vscode folder and change "command": "build.bat" to "command": ".\\.vscode\\build.bat ${fileBasenameNoExtension}".
This is trivial, but I hope it could help some newbies like me and inspire better solutions for more complicated cases :)
For anyone comming here thinking the best solution would be to set the path environment variables, so cl.exe could be run in any command prompt, or power shell terminal, this is not recommended, as per documentation:
The MSVC command-line tools use the PATH, TMP, INCLUDE, LIB, and
LIBPATH environment variables, and also use other environment
variables specific to your installed tools, platforms, and SDKs. Even
a simple Visual Studio installation may set twenty or more environment
variables. Because the values of these environment variables are
specific to your installation and your choice of build configuration,
and can be changed by product updates or upgrades, we strongly
recommend that you use a developer command prompt shortcut or one of
the customized command files to set them, instead of setting them in
the Windows environment yourself.
https://learn.microsoft.com/en-us/cpp/build/setting-the-path-and-environment-variables-for-command-line-builds?view=vs-2019
What worked for me:
Following this guide, I made this change in task.json:
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "cl.exe build active file",
"command": "build.bat", //calling build.bat instead
"args": [
"/Zi",
"/EHsc",
"/Fe:",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"${file}"
],
"problemMatcher": [
"$msCompile"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
Then my build.bat (sDevCmd.bat is called when the developer terminal is opened):
call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\Tools\VsDevCmd.bat"
echo Building...
cl.exe %*
Then Ctrl + Shift + B.
Default terminal of VSCode does not have cl.exe PATH, you need to use Developer Command Prompt :
Open Developer Command Prompt
https://code.visualstudio.com/docs/cpp/config-msvc:
To open the Developer Command Prompt for VS, start typing 'developer' in the Windows Start menu, and you should see it appear in the list of suggestions. The exact name depends on which version of Visual Studio or the Visual Studio Build Tools you have installed. Click on the item to open the prompt.
Go to project parent folder
Lets say it is D:\workspace and project name is myproject
Ctrl+Shift+B to build

Setup VSCode for C++ on Windows (MSVC)

I'm a little befuddled that I'm not able to setup Visual Studio Code to do C++ development on Windows using MSVC. All over the web people say how pleased they are with how easy everything is to set up and use, but I don't find any straightforward guides; most of them just skip the setup part and show how nice everything works including code completion/intellisense and debugging support.
I have installed Visual Studio 2015 Community Edition (including the debugging tools etc.), Visual Studio Code and the C++ extension by Microsoft.
What do I need to do next?
Edit:
Intellisense works out of the box these days, that's great. But my auto-generated tasks.json doesn't seem to do the trick for building, here's what it looks like:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"taskName": "build",
"type": "process",
"command": "msbuild",
"args": [
// Ask msbuild to generate full paths for file names.
"/property:GenerateFullPaths=true",
"/t:build"
],
"group": "build",
"presentation": {
// Reveal the output only if unrecognized errors occur.
"reveal": "always"
},
// Use the standard MS compiler pattern to detect errors, warnings and infos
"problemMatcher": "$msCompile"
}
]
}
When I run this task it seems to run infinitely and only outputs to the following:
Executing task: msbuild /property:GenerateFullPaths=true /t:build <
Any ideas?
For MSVC 2017 version:
add these to your include path:
"D:/Program Files/Microsoft/MSVC2017/VC/Tools/MSVC/14.12.25827/include/*",
"C:/Program Files (x86)/Windows Kits/10/Include/10.0.10240.0/ucrt",
"C:/Program Files (x86)/Windows Kits/10/Lib/10.0.10240.0/ucrt/x64",
You may change the drive name and folder name accordingly.
2.change your shell setting in settings.json:
"terminal.integrated.shell.windows": "C:\WINDOWS\System32\cmd.exe",
"terminal.integrated.shellArgs.windows": ["/k", "D:/Program Files/Microsoft/MSVC2017/Common7/Tools/VsDevCmd.bat"]
which will enable the cl command in your integrated terminal.
3.Press ctrl+` to enable integrated terminal, type cl /EHsc your_cpp_program.cpp to compile.
I think easiest way to get it - open visual studio 2017 command prompt console and run Visual Studio Code IDE from there so it will pick all necessary compiler environment variables

How to define the task.json to compile C/C++ code in vscode by using the cl.exe on windows?

I have installed the Microsoft Visual C++ Build Tools 2015 on my 64bit win10 , and can use the cl.exe to compile and link the C/C++ program in a plain Command Prompt window by the following steps (some instructions from Setting the Path and Environment Variables for Command-Line Builds):
1. cd "\Program Files (x86)\Microsoft Visual Studio 14.0\VC"
2. vcvarsall amd64
3. cl helloworld.c
The helloworld.c is just a simple C source file to print "Hello world!". I aslo try to congfigure the task.json to directly compile and link C/C++ programs in the vs code. Here is my task.json:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "0.1.0",
"command": "vcvarsall amd64 && cl",
"isShellCommand": true,
"args": ["${file}"],
"showOutput": "always"
}
And the path of vsvarsall and cl have been added in the PATH. But it still doesn't work (the output is put at the end of the post). So my question is that: how can I to define the task.json which can first run the vcvarsall amd64 to set system variables and then execute the cl command to compile and link programs.
As Rudolfs Bundulis said, make a batch file and just call it, inside it do everything you need to do.
tasks.json:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "0.1.0",
"command": "build.bat",
"isShellCommand": true,
"args": [],
"showOutput": "always"
}
And in your project have the build.bat goodness.
build.bat:
#echo off
call "E:\programs\VS2015\VC\vcvarsall.bat" x64 <----- update your path to vcvarsall.bat
..
cl %YourCompilerFlags% main.cpp %YourLinkerFlags%
..
I would mention that you'd like to have another visual studio code bootstraper batch that would set the vcvars environment and then starts up the editor so you don't set the vcvars for every build. Like so:
#echo off
call "E:\programs\VS2015\VC\vcvarsall.bat" x64 <----- update your path to vcvarsall.bat
code
This way you can omit setting the vcvarsall.bat every time you compile the code. Minimal rebuild flag will also help you a lot so you only compile changed files.