I frequently create small toy programs in vscode. Every time I do, I need to modify the tasks.json file to compile using C++20. I would like to use C++20 by default.
An example solution: when I open a directory in vscode for the first time, the tasks.json file should automatically include the following
"args": [
"-fdiagnostics-color=always",
"-g",
"-std=c++20",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
I am using Ubuntu.
Related
Can I ask you that can I change the default output file location in VS Code when I am coding in C++ and using MingW debugger... when I debug the program... It will create the output file in the main folder... but can this output file be created in the subfolder(The P01 folder in the picture)... Thank you!
Short answer: Yes, you can.
Open the contents of your .vscode/tasks.json file. Just focus on the args:
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/a.out",
],
When you hit F5 (by default setting in VS Code) to start the debugging mode, tasks.json is executed to compile the program. Here, you can change the value of the 4th line with your desired location.
Note that the mentioned line content (of tasks.json) must be equivalent to the content of "program" of .vscode/launch.json. Otherwise, the program will be compiled successfully, but it won't be launched because the location will be mismatched.
I am trying to create a build system for building C++ files in Sublime Text 3. I have installed MinGW and have correctly set the environment path.
I have created a build system as:
{
"cmd": ["C:\\MinGW\\bin\\g++", "${file}", "-o", "${file_path}\\${file_base_name}"],
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c, source.c++",
"shell": "true",
"variants":
[
{
"name": "Run",
"cmd": ["start", "cmd.exe", "#cmd", "/k", "${file_path}\\${file_base_name}"]
}
]
}
However, since the directory which contains the C++ file has a whitespace in it (C:\Users\Bryan Adams PC\Documents\C++), when I try to build the file, it says 'C:\Users\Bryan' is not recognized as an internal or external command,
operable program or batch file.
What changes should I make to the build in order to be able to use this directory?
I've tried adding double quotes around the filepaths as \". However it does not work. I have no idea about JSON.
Well all I've seen for build systems for C++ is the following:
"build_systems":
[
{
"name: "g++ test",
"cmd": ["g++", "${file}"],
"shell": true
}
{
"name": "echo test",
"cmd": ["echo", "${file}"],
"shell": true
}
]
Just as a test, this only works for the currently opened file, which might not even be a source file nor part of the sublime project. So this is good for a test program that only has one source but it isn't that useful otherwise.
Question 1:
Is there any ${project_files} variable with regex to accept only .cpp files ?
Question 2:
I would want to pass these files to qmake, such that it generates a .pro project file. Such that if I add a file to a sublime project it and build the file will be added to the project file. How would I do this without shell script or otherwise such that it's cross platform ?
I want to be able to save my files in E:\Documents\C++ and then be able to run and compile them. My MinGW location is E:\MinGW\bin. Everything is saved on a USB flash drive. I am using SublimeText to run and compile these files. Everything works fine if i save the C++ files in the E:\MinGW\bin folder. I just want to be able to change where to save the files and build and run them. Also I am using this as my sublime-build file
{
"cmd": ["g++", "${file}", "-o", "${file_path}/${file_base_name}"],
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c, source.c++",
"variants":
[
{
"name": "Run",
"cmd": ["${file_path}/${file_base_name}.exe"]
}
]
}
Your path should contain E:\MinGW\bin (or you should give full path for g++)
You want to change your home directory and your path for mingw.
This page shows how to set your home directory, and where to set your PATH,
http://www.mingw.org/wiki/HOWTO_Set_the_HOME_variable_for_cmd_exe
Here is a page that tells you how to build a cpp file,
http://www.mingw.org/wiki/MinGW_for_First_Time_Users_HOWTO
If you want to use the same home location for MINGW and windows,
http://mingw.5.n7.nabble.com/making-home-directory-on-msys-agree-with-Windows-td22176.html
Or, you can set your home directory to your flash drive.
Good day!
Can anyone share their experience how to attach MinGW-compiler to Sublime?
I found a config in the internet, but when I run compiled program popping bugs with missing files from "../MinGW/bin/".
Config:
{
"cmd": ["mingw32-g++.exe", "-o", "$file_base_name", "$file_name"],
"path": "c:\\Program Files\\MinGW\\bin\\"
}
Thanks!
UPD
I found answer for my question! I had to add one parameter in cmd. It's "-static".
So, it's my MinGW.sublime-build, which works fine:
{
"path": "c:\\Program Files\\MinGW\\bin\\",
"cmd": ["mingw32-g++.exe", "-static", "-o", "$file_base_name", "$file"]
}
Make sure to include the bin file in the "Path" variable on your system.
Open the start menu and type "variable" or "environment variable" (or google it) to find how to do it. You'll get in a Window with a lot of variables, find the Path (and not PATH) variable and add the path to the bin folder of MinGW.
And btw, as suggested, you should change file_base_name by file, and put file_base_name where you put file_base.
Here's the command I personally use:
"cmd": ["C:\\MinGW\\bin\\mingw32-g++.exe", "-Wall", "-time", "$file", "-o", "$file_base_name"]
You should be using $file instead of $file_name. $file_name expands to only the name whereas $file expands to the full path.
The changed config would be
{
"cmd": ["mingw32-g++.exe", "-o", "$file_base_name", "$file"],
"path": "c:\\Program Files\\MinGW\\bin\\"
}