VS code cannot compile mutiple C++ files(WSL) - c++

I just started to setup my VS Code in WSL. I followed by the instruction from GCC on Windows subsystem for Linux. Most of the work is done, there is only one issue. For this part:
Modifying tasks.json
You can modify your tasks.json to build multiple C++ files by using an argument like ${workspaceFolder}/*.cpp instead of ${file}. You can also modify the output filename by replacing ${fileDirname}/${fileBasenameNoExtension} with a hard-coded filename (for example 'helloworld.out').
I want to compile multiple cpp files, but the method provides from above ${workspaceFolder}/*.cpp instead of ${file} doesn't work for me. After I replaced it, the g++ seems like recognize the *.cpp as a file so it can't find it.
This is what the terminal shows.
g++: error: /.../.../*.cpp: No such file or directory
g++: fatal error: no input files
compilation terminated.
The terminal process terminated with exit code: 1

I find out the problem is on the name of my folder. There is a space in my folder name, so change the name of the folder solved this problem. It is really not a good habit to use space. But if you continue to want to use a name that contains space, do "\"${workspaceFolder}\"/.cpp" instead of "${workspaceFolder}/.cpp".

Try this instead: "${workspaceFolder}/**.cpp"
From the website you posted the example tasks.json would be this
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "g++ build active file",
"command": "/usr/bin/g++",
"args": ["-g", "${workspaceFolder}/**.cpp", "-o", "${fileDirname}/${fileBasenameNoExtension}"],
"options": {
"cwd": "/usr/bin"
},
"problemMatcher": ["$gcc"],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
I can't tell you the reasoning behind the double asterix * but it takes all the files with .cpp ending in the current folder.

Related

Spotlight Search, Mail Search, C++ compiling not working on macOS after PATH Modification or Ventura Update

I recently made a couple of changes on my Mac.
Intending to start working with C++ I installed cmake and modified my $PATH variable.
I updated to the newest version of Ventura.
Now, spotlight search and apple mail are not working working anymore. Additionally (and more annoying), I cannot debug, and compile cpp files as before in VS-Code.
But receive the following error message:
Starting build...
/opt/homebrew/bin/g++-11 -fdiagnostics-color=always -g "/Users/jakobnitschke/Library/Mobile Documents/com\~apple\~CloudDocs/PhD/Learning_cplusplus/Jumping_into_cpp/sample_code/ch1/hello.cpp" -o "/Users/jakobnitschke/Library/Mobile Documents/com\~apple\~CloudDocs/PhD/Learning_cplusplus/Jumping_into_cpp/sample_code/ch1/hello"
/bin/sh: /opt/homebrew/bin/g++-11: No such file or directory
Build finished with error(s).
* The terminal process failed to launch (exit code: -1).
* Terminal will be reused by tasks, press any key to close it.
My current $PATH:
/Users/jakobnitschke/opt/anaconda3/bin
/Users/jakobnitschke/opt/anaconda3/condabin
/opt/homebrew/bin
/Library/Frameworks/Python.framework/Versions/3.10/bin
/opt/local/bin
/opt/local/sbin
/usr/local/bin
/System/Cryptexes/App/usr/bin
/usr/bin
/bin
/usr/sbin
/sbin
/Library/TeX/texbin
/opt/X11/bin
/Library/Apple/usr/bin
Because I noticed a problem first through the errors in Spotlight and Apple Mail, I tried to reindex the spotlight search and followed several procedures on the apple forum but none solved my issue.
One of the first things was to properly update the Command Line Developer tools but this did not solve the issues.
Since, I looked into ways of resetting my $PATH and found ways to do it but I don't know which should be my final $PATH to set since I am scarred to mess up other parts of my programming environment with Python and R.
You should (within your project folder) look for a file named tasks.json this file configures the tasks for your Visual Studio Code. This is an example file I took from a different question I answered some time ago:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
If you look closely there is a line with an element named "command":
You will most-likely have something like "command": "/opt/homebrew/bin/g++-11" there.
Change it to "command": "/opt/homebrew/bin/g++-12"
EDIT: You can also create a symbolic-link for g++ in your /usr/bin folder that points to your homebrew installation of g++. This should save you a lot of issues when working with VSCode.

Error during build of cpp file in VSC: "Errors exist after running preLaunchTask 'C/C++:cpp.exe build active file"

I am unable to run my code and execute the build of the .exe file in the cpp language in Visual Studio Code.
I have downloaded and installed GCC, MinGW software, MinGW-w64 GCC, etc. according to the instructions here and have successfully verified that gcc g++ and gdb are all installed (by checking 'gcc --version' etc in the commad prompt). My intent is to use this compiler to compile my code but it seems I cannot find the correct compiler under the options listed here to simply 'build and debug the active file'.
I also (and perhaps consequentially) have run into a problem with launch.json not being able to build the executable file.
Can someone please help me with this? This is incredibly frustrating and I simply want to be able to run my code. I downloaded MinGW along with gcc is a good compiler for this very purpose.
Image of error message 1: here, to which I click 'debug anyway' and get the next error:
Image of error message 2: here.
Image of launch.json with the configuration that I thought would be appropriate (I found this from another source online): here.
Thank you!!
I recommend deleting all the configurations under the .vscode directory, as they can be regenerated again. After deletion, follow the steps:
Go to your program file, and press F5 to launch debug.
Select g++.exe build and debug active file or a convenient option.
A tasks.json under the .vscode will be generated with the appropriate instructions and the program will be executed instantly.
For example, in my case, it was this:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "C:\\TDM-GCC-64\\bin\\g++.exe",
"args": [ // ----- given by me in C/C++ extension settings
"-fdiagnostics-color=always",
"-Wall",
"-O3",
"-pedantic",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": ["$gcc"],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}

Build multiple C++ files not working - Linux Mint

I am following a C++ tutorial, and the guy shows how to set up tasks.json to run multiple C++ files at the same time, link with timestamp:
https://youtu.be/8jLOx1hD3_o?t=4584
Now I installed the newest versions of all of the packages:
Change my tasks.json to be same as his:
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++-11 build active file",
"command": "/usr/bin/g++-11",
"args": [
"-g",
"-std=c++20",
"${workspaceFolder}/*.cpp",
"-o",
"${fileDirname}/rooster"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": ["$gcc"],
"group": "build",
"detail": "compiler: /usr/bin/g++-11"
}
]
}
Try to compile and it does not work, error:
Starting build...
/usr/bin/g++-11 -g -std=c++20 "/home/elhan/ius/intro to programming/cpp/*.cpp" -o "/home/elhan/ius/intro to programming/cpp/rooster"
cc1plus: fatal error: /home/elhan/ius/intro to programming/cpp/*.cpp: No such file or directory
compilation terminated.
Build finished with error(s).
What might be the problem?
Working dir:
One of the arguments to your build command is
${workspaceFolder}/*.cpp
When variable substitution occurs, VS Code recognizes that there are spaces in your directory name, so the entire argument is enclosed in quotes to protect it from the shell. In the line after Starting build..., you can see the same thing happening with the argument specifying the output destination (which also has spaces after substitution), while the other arguments (with no spaces in them) are passed to the shell without quotes.
"/home/elhan/ius/intro to programming/cpp/*.cpp"
The quotes do indeed protect the argument from being processed by the shell, which is why it is interpreted as a single argument. At the same time, the quotes protect the argument from being processed by the shell, which is why the wildcard * is not expanded. And that's the problem.
If you pay attention to details, the video uses underscores instead of spaces to separate words in directory names. Follow suit, and rename your intro to programming directory to intro_to_programming.

C++ VS Code not recognizing syntax, unable to run code

I am using a specific syntax needed for a course, but when I use this C++ syntax in VS Code, it doesn't work and raises errors.
Here is an example of the syntax that is not working:
error: expected ';' at end of declaration
int i {0};
^
;
When I change it to int i = 0; the error disappears.
It specifically doesn't recognize the {} syntax for setting default variable values. I am using a ssh login for this course and the syntax works well in the ssh, but won't work in VS Code.
I attempted to change my VS Code C++ version to C++17 by doing the top answer in this thread, but it still doesn't recognize the syntax.
Am I using incorrect syntax, or is there a way to fix this?
Adding on to the comment above, this is what solved my issue:
Go to this link: https://code.visualstudio.com/docs/cpp/config-clang-mac
Go to the section Clang on macOS and scroll down to Troubleshooting. Follow the steps in this paragraph:
"If you see build errors mentioning "C++11 extensions", you may not have updated your tasks.json build task to use the clang++ argument --std=c++17. By default, clang++ uses the C++98 standard, which doesn't support the initialization used in helloworld.cpp. Make sure to replace the entire contents of your tasks.json file with the code block provided in the Run helloworld.cpp section."
You will need to copy-paste this exact code and replace the code in the tasks.json folder:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "C/C++: clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
"-std=c++17",
"-stdlib=libc++",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": ["$gcc"],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
]
}
To find the tasks.json folder, go to your program-file, do Command + Shift + . to find hidden files > .vscode > tasks.json > replace all the code in the file with the one I provided. It updates the C++ compiler to use a more updated version of C++ which recognizes the syntax.

Run C++ in Visual Studio Code without opening it with `code .` from CMD

I set up VSCode to compile C++, but in order to be able to run it, I first have to open CMD, navigate to the location of the .cpp file I want to open and then run code . (This opens VSCode and then I can compile the file with Ctrl+Shift+B.) This is tedious and it would be wonderful if I had a script that enabled me to run C++ without having to do the above procedure every time.
Thank you for your help. :)
EDIT
This is my tasks.json file:
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "C:\\msys64\\mingw64\\bin\\g++.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "C:\\msys64\\mingw64\\bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: C:\\msys64\\mingw64\\bin\\g++.exe"
}
]
}
For your particular case with a single .cpp file and g++ installed, you should be able to compile from the command line using a command similar to below:
g++ -fdiagnostics-color=always -g <.cpp file name> -o <output binary name>
Make sure to replace <.cpp file name> with your actual filename and <output binary name> with whatever you want to name your executable.
It appears you are first starting to learn how to develop in C++. This solution above should work for now.
When you start to write bigger programs and decide to split your source code into multiple files, you will eventually want to learn how to use a build system that can help automate the compilation of multiple source files. A popular build system is GNU Make, this would probably be a good tool to learn. You can write makefiles which instruct how to build the code, and then VSCode can be configured to use make to read the makefiles and build the code.