An extension running on Visual Studio Code to Compile & Run single c/c++ files easily. This is a fork of danielpinto8zz6/c-cpp-compile-run that allows for on-demand compile checking of C/C++ files.
Compile & Run C/C++ opened file directly from the command palette or by pressing 'f6' or 'f7'
Additionally, you can compile the file without running it by pressing 'cmd-6' (for mac) or 'ctrl-6' (windows & linux). This gives you a linter like ALE or syntastic on vim, except only for C/C++ files. Linking is not done - only compiling.
- If you are on linux you must install gcc (see instructions)
- If you are on window you must install tdm-gcc (see instructions). Alternatively, you can also install MSYS2 with mingw(32|64), but please read the instructions to setup the path correctly.
- If you are on mac os you must install clang (see instructions)
Make sure you have .c or .cpp file open. Press "ctrl-6" or "cmd-6" to compile the file only and delete any autogenerated object files. Press "F6", this will compile and run the file using default arguments in settings. Or press "F7", this will use the arguments you specify for the program. If you want to register gcc/g++ path manually, you can set it under settings. You can also set to save file before compiling.
Key | Description |
---|---|
c-cpp-compile-run-x.c-compiler | The C compiler path (e.g: /usr/bin/gcc or C:\TDM-GCC-64\bin\gcc.exe) |
c-cpp-compile-run-x.cpp-compiler | The Cpp compiler path (e.g: /usr/bin/g++ C:\TDM-GCC-64\bin\gcc.exe) |
c-cpp-compile-run-x.save-before-compile | Whether should save the file before compiling |
c-cpp-compile-run-x.env-vars | The environment variables to pass to the compiler |
c-cpp-compile-run-x.c-flags | The C flags: e.g. -Wall. default: -Wall -Wextra |
c-cpp-compile-run-x.cpp-flags | The Cpp flags: e.g. -Wall. default: -Wall -Wextra |
c-cpp-compile-run-x.working-dir | The working directory for the compiler. Defaults to the file path |
c-cpp-compile-run-x.run-args | The run arguments |
c-cpp-compile-run-x.run-in-external-terminal | Whether should run in an external terminal |
c-cpp-compile-run-x.should-show-notifications | Whether should show notifications |
c-cpp-compile-run-x.output-location | Custom output location for the compiled file |
For the working directory and flags, the following variables have special meanings:
$HOME
resolves to your home folder$ROOT
resolves to the root folder/
; on Windows this is equivalent toC:\
$PROJECT_ROOT
resolves to your project root folder - if there is no project then it's equivalent to$ROOT
. Multiple project roots are not supported yet - for now it just uses the first root.$$
resolves to a literal$
.
Important: Only local folders are supported at the moment.
Linux | Windows | Mac | Description |
---|---|---|---|
f6 | f6 | cmd+r | Compiles and runs the file |
crtl+6 | ctrl+6 | cmd+6 | Check for compile errors only |
f8 | f8 | cmd+y | Compiles and run the file in external console |
f7 | f7 | cmd+t | Compiles and run the file specifying custom arguments and flags |
- On Linux and possibly macOS, gcc and g++ might fail to find "cc1" and "cc1plus" respectively. You might see an error message such as below:
g++: fatal error: cannot execute 'cc1plus': execvp: No such file or directory
compilation terminated.
The solution is to explicitly specify the PATH in your environment variable to the "gcc" folder. or add this to your settings.json:
"c-cpp-compile-run-x.env-vars": {
"PATH": "/bin:/usr/bin:/usr/lib/gcc/x86_64-linux-gnu/9/"
}
Please substitute x86_64-linux-gnu/9
with the folder you see in your system. The folder structure takes the format /usr/lib(or lib64)/gcc/PLATFORM/GCC_VERSION/
.
Refer to CHANGELOG