I'm trying to run cmake with ctrl+shift+B like so:
{
"version": "2.0.0",
"tasks": [
{
"label": "cmake",
"type": "shell",
"options": {
"cwd": "${workspaceRoot}/build"
},
"command": "cmake ${workspaceRoot} -G \"MinGW Makefiles\"",
(...)
},
{
"label": "make",
"type": "shell",
"command": "mingw32-make.exe",
"options": {
"cwd": "${workspaceRoot}/build"
},
(...),
"dependsOn":["cmake"]
},
{
"label": "build",
"type": "shell",
"options": {
"cwd": "${workspaceRoot}/build"
},
"group": {
"kind": "build",
"isDefault": true
},
"dependsOn": ["make"]
}
]
}
But no matter what I do It runs on ${workspaceRoot} instead of the ${workspaceRoot}/build one:
Executing task in folder cpppractice: cmake C:\workspace\cpp\cpppractice -G "MinGW Makefiles"
Is there anyhting wrong with this approach? As far as I understand the cwd variable in the options item should work.
{
"label": "cmake",
"type": "shell",
"options": {
"cwd": "${workspaceRoot}/build"
},
"command": "cmake \"MinGW Makefiles\" ${workspaceRoot}",
},
This works, It seems that the "Executing task in folder cpppractice:" is not accurate and it is executing it in the correct place, as for why it did not work previously I think it's parsing the args incorrectly? I can't confirm, but here is the output:
Executing task in folder cpppractice: cmake "MinGW Makefiles"
C:\workspace\cpp\cpppractice <
-- Configuring done
-- Generating done
-- Build files have been written to: C:/workspace/cpp/cpppractice/build
Where previously it was compalining about not being able Use the generator "MinGW" which means that it separating the argument "MinGW Makefiles".
After some tinkering I found out that this is also an answer:
{
"label": "cmake",
"type": "shell",
"options": {
"cwd": "${workspaceRoot}/build"
},
"command": "cmake",
"args": [
"-G",
"'MinGW Makefiles'",
"./.."
],
...
},
I actually find the second approach a little bit cleaner but both work the same, So in order to pass an argument as a string you have to use single quotes like so:
...
"command":"echo",
"args": [
"'Za Warudo!'",
],
...
You pass arguments to cmake wrongly. The whole string cmake ${workspaceRoot} -G "MinGW Makefiles" is treated as a command name. Arguments must be listed in the args array.
{
"version": "2.0.0",
"tasks": [
{
"label": "cmake",
"type": "shell",
"options": {
"cwd": "${workspaceRoot}/build"
},
"command": "cmake",
"args": [
"${workspaceRoot}",
"-G",
"\"MinGW Makefiles\""
]
},
...
]
}
Related
I am trying to compile all files in a certain directory, but g++ is giving an error.
It says "invalid argument". I am using Windows 11. Here is my directory structure
Here is my tasks.json file -
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "g++",
"args": [
"-fdiagnostics-color=always",
"-c",
"${fileDirname}\\*.cpp",
//"-g",
//"-Wall",
//"-m64",
//"-I",
//"include",
//"-I",
//"C:\\SDL2-w64\\include",
//"-o",
//"bin\\debug\\${fileBasenameNoExtension}.exe",
//"-l",
//"C:\\SDL2-w64\\lib",
//"-lmingw32",
//"-lSDL2main",
//"-lSDL2",
//"-lSDL_image",
//"&&",
//"start",
//"bin\\release\\main"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": "build",
"detail": "compiler: C:\\MinGW\\bin\\g++.exe"
}
]
}
For any further details comment it. ANY help would be appreciated.
I have a CMake CPP project which has a dependency on the SDK environment. Before running the CMake I need to source the environment setup file.
build$source qemux86/environment-setup-i586-poky-linux
build$cmake ..
build$make
I created the tasks.json file which having the above commands.
{
"version": "2.0.0",
"tasks": [
{
"label": "cmake build",
"type": "shell",
"command": "source qemux86/environment-setup-i586-poky-linux;cmake ..",
"options": {
"cwd": "${workspaceFolder}/build"
}
},
{
"label": "make build",
"options": {
"cwd": "${workspaceFolder}/build"
},
"command": "make",
"args": [
"-j3"
]
},
{
"label": "Build",
"dependsOn": [
"cmake build",
"make build"
],
"dependsOrder": "sequence",
"problemMatcher": [],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
It's working fine for me, but is there any way to run the source command separately, currently, I am running both source, CMake command in a single task
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"
}
}
]
}
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
}
}
]
}