Shortening Run Statement for C++ - visual-studio-code

I recently started using C++ with Microsoft VSCode for competitive programming. Whenever I run the program I have to type
g++ filename.cpp -o filename
then I would type filename in terminal to run the .exe file.
In the context of competitive programming, it can be really time consuming to retype this command. Is there a way to shorten this command?
Also, when I make a change to the program is there a better way of recompiling it instead of making another .exe file?
Any help would be appreciated!

If you are so obsessed with typing filename you can always use the run button in IDE, or just use g++ filename.cpp and it will create a.out file. If you need more, then one version of program, then it is much better to save different cpp files, instead of binaries.

Use coderunner addon for VSCode
In config file, add:
"code-runner.executorMap": {
"c": "cd $dirWithoutTrailingSlash && gcc $fileName -o build/$fileNameWithoutExt && $dirWithoutTrailingSlash/$fileNameWithoutExt",
"cpp": "cd $dirWithoutTrailingSlash && g++ $fileName -o build/$fileNameWithoutExt && $dirWithoutTrailingSlash/$fileNameWithoutExt"
},
"code-runner.saveFileBeforeRun": true,
"code-runner.runInTerminal": true
Click right mouse and run code

Related

How to compile a C program that includes `math.h` using vscode code-runner extension?

Is there a way to compile a C program that includes math.h using the vscode code-runner extension? I know that the program can be compiled by appending -lm option at the end of the gcc command if I compile it in the terminal. The thing is I do not want to compile the program from the terminal.
As a workaround, I tried to change "code-runner.executorMapByFileExtension" as follows.
"code-runner.executorMapByFileExtension" : {
".c": "cd $dir && gcc $fileName -o $fileNameWithoutExt -lm && $dir$fileNameWithoutExt"
}
Note -lm option appended. But I could not get it done because the command to compile the program is not overwritten?
Any ideas to solve this problem are appreciated.
Edit: Forgot to say that I am on linux.
I haven't been able to figure out a way to load the math.h file in Visual Studio Code, either. It seems that there is no choice but to build from the terminal so that you can pass the -lm switch.

Cannot run 'make' in VScode Terminal

For a school assignment, I am trying to compile a C file using a provided Makefile in Vscode. The makefile contains the following:
CFLAGS += -std=gnu11 -g
EXES = greet
all: $(EXES)
clean:
rm -f $(EXES)
greet: greet.c
# don't treat all and clean as file targets
.PHONY: all clean
When I run make in the VScode terminal, it gives me:
bash: make: command not found
Why is this happening? The assignment says this:
The accompanying Makefile will build the program greet. Thus, you can compile the
program by running make. The make program will print out each command that it uses to
compile the program. Note that if you run make twice in a row, the second time it won’t do
anything, because it knows your source file hasn’t changed.
Run the program using the following command:
./greet
I don't know if this has anything to do with my tasks.json file in VScode?
I also ran across this VSCode extension: https://naereen.github.io/Makefiles-support-for-VSCode/
It says Vscode now has something built-in: https://github.com/microsoft/vscode/tree/master/extensions/make
I don't know how to install this.

Code-runner configuration for running multiple cpp classes in vscode

I have a cpp project with multiple classes and headers. I was trying to make it compile and run using tasks and lunch.json but I gave up. I realized that a while ago I had a problem with Python interperter and went to code-runner configuration to change the default interperter when working with Python. But there has to be a way to make code-runner work even in cpp when having multiple classes and header.
This is what I found in the configuration:
"code-runner.executorMap": {
"cpp": "cd $dir && g++ -std=c++14 $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
},
I see that only one file gets compiled. What should I add to the code above to make vscode compile all classes?
I change that line to
"code-runner.executorMap": {
"cpp": "cd $dir && g++ -std=c++14 *.cpp -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
},
Now it works like a charm.

Cygwin EMacs GDB cannot debug NASM-64 source

I have a 64-bit NASM exe written as a console app. I want to debug it using gdb in Emacs under Cygwin. I compiled and assembled using the following command strings:
nasm -f elf64 Prime_Number_EXE.asm
gcc -g -l cygwin Prime_Number_EXE.o -o Prime_Number_EXE.exe
After compiling and linking, I go to cygwin and start emacs. I open the nasm source file. Then I start gdb and switch to gdb-many-windows mode. But the source file showing in the source window is not my nasm code; it's a special C program that Cygwin inserts that says
/* libcmain.c
This file is part of Cygwin.
/* Allow apps which don't have a main to work, as long as they define WinMain */
So it doesn't show the nasm source code, and when I step through it, it will not stop at any breakpoints in the nasm source code.
How can I solve this?
Thanks for any help.
elf64 is NOT the structure of windows/cygwin program that is COFF,
You can not run elf64 on windows.
You are looking at the STUB gcc added to all the program.
For what you are trying to do you need a Unix/Linux system or
change to COFF structure.

problem in configuring clang static analzer

i follow the following steps to install and configure clang static analyser.but still i could not run scan-build command in project directory can anyone can give correct tutorial to set path and also run scan-build command.terminal shows "scan-build command not found" the steps i followed:
Installation: Navigate to http://clang.llvm.org/StaticAnalysis.html Download the linked checker tarbell (it says tar.bz2, but it's really tar.bz2.tar). Extract that and copy that to a directory on your device. I chose ~/Developer/clang Open terminal and type sudo nano /etc/paths Enter the directory in which you keep your clang stuffs. Press 'Ctrl + X' to Exit, and press 'Y' to save.
You're now done with installation. Quit and restart terminal.
To use this, First make sure you go into Xcode and "Clean All" before you do anything. When that's all set, open terminal and navigate to the directory of the app you want to build. Enter the following command. Make sure to replace the name od the sdk with the one you currently want to build with. scan-build -k -V xcodebuild -configuration Debug -sdk iphonesimulator3.0
I've never added paths that way. But regardless you should not need to.
If you added clang to ~/Developer/clang, then just change the command you are using to run it to:
~/Developer/clang/scan-build -k -V xcodebuild -configuration Debug -sdk iphonesimulator3.0
Ran into this problem myself. It seems that scan-build is actually a perl script which changes some env variables so that clang compiler gets run before the work is passed to the real project compiler. This way clang can perform static analysis.
Try running like this:
perl <CLANG_PATH>/llvm/tools/clang/tools/scan-build/scan-build -k -o $HOME/clang-result make
Before that make sure you have the clang executable in the PATH variable:
echo $PATH
To add it:
export PATH=$PATH:<CLANG_BUILD_BIN_PATH>
eg: export PATH=$PATH:$HOME/clang/build/Release+Asserts/bin/