VS Code Code Runner C++ with wrong command in terminal - c++

I'm using Code Runner extension in VS Code to run C++ code and it's using the wrong terminal command to do so. I'm using git-bash in VS Code and Windows 10.
This is the command in terminal:
Douglas#LAPTOP-6BSNLLDB MINGW64 /c/path (master)
$ cd "c:\path\" && g++ HelloWorld.cpp -o HelloWorld && "c:\path\"HelloWorld
bash: cd: c:\path" && g++ HelloWorld.cpp -o HelloWorld && c:path"HelloWorld: No such file or directory
As you can see, it's using cd and g++ correctly, but the command to actually run the .exe file is wrong.
Shouldn't it be "c:\path\HellowWorld.exe"?
How can I change this? It works correctly with python.
If I can't, how do I run C++ code within VS Code?

Seems like you are using git-bash instead of PowerShell.
you only need to use
"code-runner.terminalRoot": "/",
VSCode: Code Runner extension is unable to execute the code on git bash terminal?
Change setting to
"code-runner.executorMap":{
"cpp": "cd $dirWithoutTrailingSlash && g++ $fileName -o $fileNameWithoutExt && ./$fileNameWithoutExt",
}
also c's: ( I use tcc)
"code-runner.executorMap":{
"c": "cd $dirWithoutTrailingSlash && tcc -run $fileNameWithoutExt.c",
}

$ cd /d "c:\path\" && g++ HelloWorld.cpp -o HelloWorld && HelloWorld
Or..
$ cd /d "c:\path\" && g++ HelloWorld.cpp -o HelloWorld.exe && HelloWorld

What wuyudi said before isn't false, but I think instead of
"code-runner.termnalRoot":"/",
you should use
"code-runner.terminalRoot":"/mnt/"
it's working fine for me so I hope it will work for the others too.

Related

llvm-link: error : expected top-level entity

I'm trying to compile the libpng 1.2.56 file and use a target.cc(same as fuzzer-test-suite to unite all these file in libpng1.2.56 into one LLVM IR bitcode file, named combined.bc.
Here is my compile process, refering build.sh:
[ ! -e libpng-1.2.56.tar.gz ] && wget https://downloads.sourceforge.net/project/libpng/libpng12/older-releases/1.2.56/libpng-1.2.56.tar.gz
[ ! -e libpng-1.2.56 ] && tar xf libpng-1.2.56.tar.gz
build_lib() {
rm -rf BUILD
cp -rf libpng-1.2.56 BUILD
(cd BUILD && ./configure --disable-shared && make -j $JOBS )
}
build_lib || exit 1
mkdir bitcode
clang++-10 -g -flto -std=c++11 ./target.cc -c -o bitcode/target.o -I .
cd bitcode
ar x ../.libs/libpng12.a
llvm-link-10 *.o -o combined.bc
And error is
enter image description here
The bitcode folder:
enter image description here
For modifications like the above, I succeeded on the project json, but not on libpng.
I am a beginner in LLVM and would like to get answers from you all. Thanks a lot!

How to Modify Code Runner's Default Command Line to Include g++ -std=c++11?

I recently tried running some code on VS Code but, as I'm using C++11 Standard code, I have to manually change the execution code in terminal to g++ -std=c++11 to actually run the code without errors.
I'm using Code Runner v0.11.8 by Jun Han
How can I set this option as the default?
On this page there is a Configuration section in which you have example how to specify compilation for c language. Write your own for C++ with flags you need and I think that it will be enough.
Follow below steps:
Open VsCode settings using CTRL+, or from left bottom side
click edit settings.json
add the following code snippet in the end:
{
"code-runner.executorMap": {
"cpp": "cd $dir && g++ -std=c++11 $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt"
}
}
& it's done.
NOTE: Make sure VsCode terminal profile is Command prompt.
My setup:
I use this execution command, it also remove .exe file after execution of the program:
"code-runner.executorMap": {
// -lm flag for linking c file with libm (will enable use of math.h in VsCode)
"c": "cd $dir && gcc $fileName -o $fileNameWithoutExt.exe -lm && $dir$fileNameWithoutExt.exe && del $dir$fileNameWithoutExt.exe ",
"cpp":"cd $dir && g++ -std=c++17 $fileName -o $fileNameWithoutExt.exe && $fileNameWithoutExt.exe && del $dir$fileNameWithoutExt.exe",
},
You can use any version like -std=c++14 or -std=c++17.
Hope it helps

missing function: _ZN4CoreC1Ev when running a emscripten js compiled file

So here is my Makefile file:
webgl:
emcc -s USE_SDL=2 -s USE_SDL_IMAGE=2 -s SDL2_IMAGE_FORMATS='["bmp","png", "jpeg", "jpg"]' -s USE_SDL_TTF=2 -c main.cpp -o build/index.o
emcc -O1 build/index.o -s LLD_REPORT_UNDEFINED -s ERROR_ON_UNDEFINED_SYMBOLS=0 -o build/index.js
Yes, I have been using SDL2 for my emscripten project and compiling it has the big error so I have added a -s LLD_REPORT_UNDEFINED -s ERROR_ON_UNDEFINED_SYMBOLS=0 to compile immediately without errors. After my attempt to build, it works but I can't run because of an error when I typed node index.js and pressing the Enter key.
missing function: _ZN4CoreC1Ev
Aborted(-1)
I searched up _ZN4CoreC1Ev on Google for solution but Your search - _ZN4CoreC1Ev - did not match any documents. it just said. Any solutions?

How to compile custom cpp files on Google Colab

I'm trying to replicate the result of this github repo using Google Colab since I don't want to install all the requirements on my local machine and to take advantage of the GPU on Google Colab
However, one of the things I need to do (as indicated in the repo's README) is to first compile a cpp makefile. The instruction of the makefile is included below. Obvious I can't follow this instruction since I don't know Google Colab's directories of ncvv, cudalib and tensorflow library
cd latent_3d_points/external
with your editor modify the first three lines of the makefile to point to
your nvcc, cudalib and tensorflow library.
make
Is there a way for me to compile the files included in the makefile (because those functions are needed to run the model) either using the makefile directly or compile each cpp file individually? I included the content of the makefile below to avoid having you to click around in the repo looking for it
nvcc = /usr/local/cuda-8.0/bin/nvcc
cudalib = /usr/local/cuda-8.0/lib64
tensorflow = /orions4-zfs/projects/optas/Virt_Env/tf_1.3/lib/python2.7/site-packages/tensorflow/include
all: tf_approxmatch_so.so tf_approxmatch_g.cu.o tf_nndistance_so.so tf_nndistance_g.cu.o
tf_approxmatch_so.so: tf_approxmatch_g.cu.o tf_approxmatch.cpp
g++ -std=c++11 tf_approxmatch.cpp tf_approxmatch_g.cu.o -o tf_approxmatch_so.so -shared -fPIC -I $(tensorflow) -lcudart -L $(cudalib) -O2 -D_GLIBCXX_USE_CXX11_ABI=0
tf_approxmatch_g.cu.o: tf_approxmatch_g.cu
$(nvcc) -D_GLIBCXX_USE_CXX11_ABI=0 -std=c++11 -c -o tf_approxmatch_g.cu.o tf_approxmatch_g.cu -I $(tensorflow) -DGOOGLE_CUDA=1 -x cu -Xcompiler -fPIC -O2
tf_nndistance_so.so: tf_nndistance_g.cu.o tf_nndistance.cpp
g++ -std=c++11 tf_nndistance.cpp tf_nndistance_g.cu.o -o tf_nndistance_so.so -shared -fPIC -I $(tensorflow) -lcudart -L $(cudalib) -O2 -D_GLIBCXX_USE_CXX11_ABI=0
tf_nndistance_g.cu.o: tf_nndistance_g.cu
$(nvcc) -D_GLIBCXX_USE_CXX11_ABI=0 -std=c++11 -c -o tf_nndistance_g.cu.o tf_nndistance_g.cu -I $(tensorflow) -DGOOGLE_CUDA=1 -x cu -Xcompiler -fPIC -O2
clean:
rm tf_approxmatch_so.so
rm tf_nndistance_so.so
rm *.cu.o
You can use the bash like on your pc by adding %%bash in the colab's cells.
Example:
Cell one: write cpp file
%%writefile welcome.cpp
#include <iostream>
int main()
{
std::cout << "Welcome To AI with Ashok's Blog\n";
return 0;
}
Cell two: compile and run
%%bash
g++ welcome.cpp -o welcome
./welcome
You can also open the cpp file in colab's build-in text editor in order to enjoy correct highlights. It opens when you open a text file from the "Files" tab on the left and can be save with "ctr+s" shortcut.
You can install the required version of Cuda in google colab. For eg.
For Cuda 9.2 you can try
!apt-get --purge remove cuda nvidia* libnvidia-*
!dpkg -l | grep cuda- | awk '{print $2}' | xargs -n1 dpkg --purge
!apt-get remove cuda-*
!apt autoremove
!apt-get update
!wget https://developer.nvidia.com/compute/cuda/9.2/Prod/local_installers/cuda-repo-ubuntu1604-9-2-local_9.2.88-1_amd64 -O cuda-repo-ubuntu1604-9-2-local_9.2.88-1_amd64.deb
!dpkg -i cuda-repo-ubuntu1604-9-2-local_9.2.88-1_amd64.deb
!apt-key add /var/cuda-repo-9-2-local/7fa2af80.pub
!apt-get update
!apt-get install cuda-9.2
Similarly, you can find a way to install Cuda 8.2.
For gcc
!apt-get install -qq gcc-5 g++-5 -y
!ln -s /usr/bin/gcc-5
!ln -s /usr/bin/g++-5
!sudo apt-get update
!sudo apt-get upgrade
Then you can compile it or make it by running make, if your installation has a custom make file.
!make

How to run an executable file (.exe) in Windows Powershell with spaces in it?

Recently I wanted to try out Visual Studio Code for C++ programming. Since I am a fairly new to C++ and programming in general I didn't want to do the tedious setup process for running programs. So I installed the extension "Code Runner" which runs the cpp file in the terminal. It was all going great until I wanted to run cpp files with spaces in them. After some searching I found out that the extension uses some kind of batch file to change the directory of the terminal,compile it and run the output file. When there were spaces in the name the 2 parts of the name were being interpreted as separate commands.This is the code-runner.executorMap setting in the VS code settings:
"code-runner.executorMap": {
"javascript": "node",
"php": "C:\\php\\php.exe",
"python": "python",
"perl": "perl",
"ruby": "C:\\Ruby23-x64\\bin\\ruby.exe",
"go": "go run",
"html": "\"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe\"",
"java": "cd $dir && javac $fileName && java $fileNameWithoutExt",
"c": "cd $dir && gcc $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt"
"cpp": "cd $dir && g++ $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
"objective-c": "cd $dir && gcc -framework Cocoa $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
The output for this was:
PS D:\> cd "d:\" ; if ($?) { g++ Test 1.cpp -o Test 1 } ; if ($?) { .\Test 1 }
g++.exe: error: Test: No such file or directory
g++.exe: error: 1.cpp: No such file or directory
g++.exe: error: 1: No such file or directory
I managed to fix some of the problem by surrounding the name and directory with double quotes.
"c": "cd $dir && gcc \"$fileName\" -o \"$fileNameWithoutExt\" && \"./$fileNameWithoutExt\"",
"cpp": "cd $dir && g++ \"$fileName\" -o \"$fileNameWithoutExt\" && \"./$fileNameWithoutExt\"",
"objective-c": "cd $dir && gcc -framework Cocoa \"$fileName\" -o \"$fileNameWithoutExt\" && \"./$fileNameWithoutExt\"",}
The output was:
PS D:\> cd "d:\" ; if ($?) { g++ "Test 1.cpp" -o "Test 1" } ; if ($?) { "./Test 1" }
./Test
The "cd" command and the "g++" command seemed to work but the part which actually executes the program to be malfunctioning. It interpreted the double quoted file name as a string and just displayed it as it is instead of running it. I searched on the internet on a solution to running .exe files with spaces in the name but couldn't find any. I'm sorry if the topic is unrelated or its due to my incompetence as I'm very new to programming.