How to build and execute cpp file in vscode ctrl+shift+B - c++

tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "C/C++: g++.exe build active file",
"command": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe",
"args": ["-g", "${file}", "-o", "${fileDirname}\\${fileBasenameNoExtension}.exe"],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": ["$gcc"],
"group": {
"kind": "build",
"isDefault": true
}
},{
"label": "Run task...",
"type": "process",
"command": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"windows": {
"command": "${fileDirname}\\${fileBasenameNoExtension}.exe"
},
"group": "build",
"options": {
"cwd": "${workspaceFolder}"
},
"dependsOn":[
"C/C++: g++.exe build active file"
]
}
]
}
I went through how to build and run and delete executable file and dummy file in vscode and wrote the above script to build and run .cpp files.
Although, .cpp files are being compiled into .exe but it's not running with the above command.
To, run .exe file i need to run manually by typing this ./path_to_file.exe in the terminal.
It's running fine, but i would like run the file when i press ctrl+shift+B.

Related

Can't compile with Clang on MacOS, only builds

I'm attemping to follow this article to be able to code in c++ on MacOS, but my code isn't running? Or the output isn't going into the terminal.
I've gotten to the "Run helloworld.cpp" step in the article and have chosen "C/C++: clang++ build and debug active file." When I run the code, it brings me to the debug console and says main is asking for permission to access files in my Desktop folder.
Here is my tasks.json:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
"-fdiagnostics-color=always",
"-std=c++17",
"-stdlib=libc++",
"-g",
"-pedantic-errors",
"-Wall",
"-Weffc++",
"-Wextra",
"-Wsign-conversion",
"-Werror",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
Build finished successfully.

Is it possible to build task based on the specific file extension?

Specifically, I'd like one keyboard shortcut to build an executable file with the correct compilation command and flags, whether the source file is a .c or a .cpp.
For example, my current tasks file, which compiles .cpp files, is as follows:
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++-9 build active file",
"command": "/usr/bin/g++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
]
}
I noticed that if I change "commands" to "/usr/bin/gcc", it's able to compile .c files.
So what I'm looking to have my tasks file do is:
Extract the extension (.c or .cpp) of the file on which I'm building.
Set a variable that will be given to "command".
Conditionally change that variable to "/usr/bin/gcc" or "/usr/bin/g++", based on the extracted extension.
Is all that possible? Do you have better suggestion to achieve that kind of conditional building?
Thanks!
You can use the extension Command Variable.
Use the command: extension.commandvariable.file.fileAsKey
The order of the extensions is important.
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "${input:pickCompiler}",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
...
...
}
],
"inputs": [
{
"id": "pickCompiler",
"type": "command",
"command": "extension.commandvariable.file.fileAsKey",
"args": {
".cpp": "/usr/bin/g++",
".c": "/usr/bin/gcc"
}
}
]
}

How to set bigobj option when compiling C++ code in Visual Studio Code?

My Problem:
I am writing a chess engine in C++. Part of writing a chess engine is dealing with very large numbers (conceivably up to 2^63). I have a file that is running unit tests for my project, and when I try to run the build task to compile it to an executable, I am getting the following error:
C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/bin/as.exe: C:\Users\chopi\AppData\Local\Temp\ccEvO3si.o: too many sections (32782)
C:\Users\chopi\AppData\Local\Temp\ccCF1XuS.s: Assembler messages:
C:\Users\chopi\AppData\Local\Temp\ccCF1XuS.s: Fatal error: can't write 293 bytes to section .text of C:\Users\chopi\AppData\Local\Temp\ccEvO3si.o: 'File too big'
C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/bin/as.exe: C:\Users\chopi\AppData\Local\Temp\ccEvO3si.o: too many sections (32782)
C:\Users\chopi\AppData\Local\Temp\ccCF1XuS.s: Fatal error: can't close C:\Users\chopi\AppData\Local\Temp\ccEvO3si.o: File too big
The terminal process "C:\windows\System32\WindowsPowerShell\v1.0\powershell.exe -Command & 'C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin\g++.exe' -g c:\Users\chopi\Desktop\chess-engine\maestro-tests\main.cpp -o c:\Users\chopi\Desktop\chess-engine\maestro-tests\main.exe" terminated with exit code: 1.
Basically, there are too many sections. So I go looking for an answer and find many places explaining how to configure bigobj for Visual Studio, but not for Visual Studio Code.
What I've tried:
It should be as simple as passing either /bigobj, -bigobj, or both -Wa and -mbig-obj as options when compiling. So I've tried a number of things that should, but aren't, compiling my code with big objects enabled. This is how my tasks.json looks:
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "C/C++: g++.exe build active file",
"command": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
but I've also tried the likes of:
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "C/C++: g++.exe build active file",
"command": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe",
"args": [
"-g",
"-bigobj",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
and
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "C/C++: g++.exe build active file",
"command": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe",
"args": [
"-g",
"-Wa",
"-mbig-obj",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
...among others.
These configurations are yielding errors of g++.exe: error: unrecognized command line option '-bigobj' and
g++.exe: error: unrecognized command line option '-Wa'; did you mean '-W'?
g++.exe: error: unrecognized command line option '-mbig-obj'
, respectively.
What I'm wondering:
So, is there a way I can resolve this "too many sections" error in Visual Studio Code? Or will I have to bite the bullet and configure everything for Visual Studio?
As user4581301 suggested, I had to not separate -Wa and -mbig-obj as two, independent options. Instead, I had to keep them as one option: -Wa,-mbig-obj. This ran into an error, but, per this answer, adding --% as the first argument, coupled with the aforementioned suggestion about the options, got my code finally compiling to an executable. Here is what the tasks.json looks like after the changes:
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "C/C++: g++.exe build active file",
"command": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe",
"args": [
"--%",
"-g",
"-Wa,-mbig-obj",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}

VSCode - C++ undefined reference to 'CLASS::FUNCTION'

I'm using VSCode to create a C++ project and keep getting this error when trying to build && debug. When running from console if I use 'g++ -o main 'main.cpp' 'file_1.cpp' 'file_2.cpp'' it works and compiles correctly.
I have read that this is something to do with the linking of files? Does anyone know how to fix this in VSCode? I have a default launch.json configuration file that builds the active file if this helps.
Here is the contents of my tasks.json file:
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "shell: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "/usr/bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
},
{
"type": "shell",
"label": "g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "/usr/bin"
}
}
]
}
EDIT: I'm using Debian 10 'Buster'
Many thanks
You need to modify tasks.json for compile all .cpp .
In args change ${file} for ${workspaceFolder}/*.cpp
To run the build task press Ctrl+Shift+B, this generate a execution file in the current directory. Execute this file in the terminal for run the program (./fileName)

Clang++ working on CLion fine but not VSCode

I was tryna transition from seperate IDEs to a single one where I can use with everything (Python, C++ and Web). I chose VSCode, since it had all of the necessary stuff in it. I finished setting up Conda and Python, but when I got to C++ I had a problem compiling my task.json file. The error was that wchar.h couldn't be found. It compiles and works fine on XCode, and CLion, but the Clang just doesn't work on VSCode. Any ideas on how to fix this?
Thanks
HJ
Here is the error code for reference
In file included from /Users/kimh2/Desktop/Coding Stuff/C++/HelloWorld/main.cpp:1:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iostream:38:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/ios:215:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iosfwd:96:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/wchar.h:119:15: fatal error:
'wchar.h' file not found
#include_next <wchar.h>
^~~~~~~~~
1 error generated.
The terminal process terminated with exit code: 1
The task.json file:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "clang++ build active file",
"command": "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin"
},
"problemMatcher": [
"$gcc"
],
"group": "build"
},
{
"type": "shell",
"label": "clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "/usr/bin"
},
"problemMatcher": [
"$gcc"
],
"group": "build"
},
{
"type": "shell",
"label": "clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "/usr/bin"
},
"problemMatcher": [
"$gcc"
],
"group": "build"
},
{
"type": "shell",
"label": "clang++ build active file",
"command": "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}