Build multiple C++ files not working - Linux Mint - c++

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.

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.

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.

Why is "C/C++:" being inserted into the label of my tasks.json file in Visual Studio Code?

I am using Visual Studio Code with MinGW-w64. This involves creating two files (launch.json and tasks.json) that allow me to build and debug my C++ code. I select my tasks.json file by going to Terminal --> Configure Default Build Task... and then selecting "C/C++: g++.exe build active file."
Normally, the tasks.json file appears as follows:
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "g++.exe build active file",
"command": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin"
},
"problemMatcher": [
"$gcc"
],
"group": "build"
}
]
}
However, in the last few days, it has appeared as follows:
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "C/C++: g++.exe build active file",
"command": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin"
},
"problemMatcher": [
"$gcc"
],
"group": "build"
}
]
}
When the file is in this format, I'm not able to debug my code. Instead, I get the message "Could not find the task 'g++.exe build active file'.
The only apparent difference is that C/C++is now preceding g++.exe build active file in the "label" line. Once I delete this label, I'm able to get the code to compile and/or debug.
This leads me to ask two questions: first, why is C/C++ being inserted in the label? It doesn't appear in the Microsoft documentation for setting up MinGW with Visual Studio Code: https://code.visualstudio.com/docs/cpp/config-mingw
Secondly, how am I able to permanently remove C/C++ from my label so that I don't have to manually delete it each time? I have tried choosing Configure Task next to C/C++:g++.exe build active file in the Configure Default Build Tasks in the dropdown menu; removing C/C++, and then saving the tasks.json file, but this doesn't appear to cause the build task to change permanently.
Thank you as always for your help.
It took me a while to read the source code of the cpptools extension, and then I found some problems. Under different conditions, the extension has different response
If there is ".vscode" folder in the folder root directory, then when adding the debug configuration, it will only guide the creation of launch.json.The user needs to be guided to configure "tasks.json" again.But the name of the task created at this time is not consistent with value of "preLaunchTask". This is a problem with this extension. I think the reason is that the ".vscode" folder was not found when initializing the debug configuration, some conditions are missing when the extension's code is executed
If there is a empty ".vscode" folder in the root directory of the folder, "launch.json" and "tasks.json" will be created at the same time when initializing the debug configuration.The two files are exactly matched and do not need any modification
If the user configures the task as first.When initializing the debug configuration, the extension will recreate a task called "g++.exe build active file" which is not start with "C/C++".This of course works, but you may need to delete the task created at the beginning whichhas not been used
This design is reasonable because "C/C++: g++.exe build active file" is actually a task template built into cpptools (there are two others). The built-in templates can be referenced by "preLaunchTask", but the user cannot see them in "tasks.json"
If you delete the file "tasks.json" and change the value of "preLaunchTask" to "C:C++: g++.exe build active file".It also works,but this is not a standard usage
I think this is a mistake of them, it started to appear after a recent version

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

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.