In Visual studio code, I can compile and debug one C++ file easily. But when it comes to multiple C++ file with some header file, it does not work.
Here is the default tasks.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": "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
}
}
]
}
What changes do I have to make?
Related
I am trying to set up a default VS Code project that I can copy and paste for C++ projects. I have installed Clang/LLVM and I have got a task in tasks.json that can build for Windows just fine. My problem is when I try to create task that would build for Linux by changing the --target variable to target for Linux on the same physical platform I end up getting an error that it is unable to find <iostream>. I am not sure if this is a tasks.json thing that I am getting wrong or a clang issue that I am unaware of. (I am very new to cross platforming)
My tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "Build Clang for Windows x64 (Debug)",
"command": "clang++",
"args": [
"-std=c++17",
"--target=x86_64-pc-win32-gnu",
"-g",
"${workspaceFolder}/Src/*.cpp",
"-o",
"${workspaceFolder}/Bin/Debug/Windows/x64/out.exe"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
},
{
"type": "shell",
"label": "Build Clang for Linux x64 (Debug)",
"command": "clang++",
"args": [
"-std=c++17",
"-stdlib=libc++",
"--target=x86_64-pc-linux-gnu",
"-g",
"${workspaceFolder}/Src/*.cpp",
"-o",
"${workspaceFolder}/Bin/Debug/Linux/x64/out"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
]
}
]
}
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"
}
}
]
}
So I have a CMakeLists.txt that works well and the solution runs in debug.
The tasks associated with VS Code (build active file, etc) call GCC ignoring the CMakeLists.txt. This is undesired because CMake sets required flags like the C++ standard version.
Anybody know how to pipe CMake into tasks.json?
For the default tasks look like:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++-9 build active file",
"command": "/usr/bin/g++-9",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "/usr/bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Generated task by Debugger"
},
{
"type": "cppbuild",
"label": "C/C++: cpp build active file",
"command": "/usr/bin/cpp",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "/usr/bin"
},
"problemMatcher": [
"$gcc"
],
"group": "build",
"detail": "compiler: /usr/bin/cpp"
}
],
"version": "2.0.0"
}
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
}
}
]
}
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
}
}
]
}