Following up from here, I understand that g++ uses c++03 syntax by default, however how do I go about changing this to c++11 on VSC?
My best guess is that I change my task.json file, but how exactly?
Tasks.json file:
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "/usr/bin/clang++",
"args": [
// Ask msbuild to generate full paths for file names.
"/property:GenerateFullPaths=true",
"/t:build",
// Do not generate summary otherwise it leads to duplicate errors in Problems panel
"/consoleloggerparameters:NoSummary"
],
"group": "build",
"presentation": {
// Reveal the output only if unrecognized errors occur.
"reveal": "silent"
},
// Use the standard MS compiler pattern to detect errors, warnings and infos
"problemMatcher": "$msCompile"
}
]
}
I'm also using VSC on Mac.
Related
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.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
Whenever I try to run my code if I have more than one .cpp file, I get the following message: The preLaunchTask 'C/C++: g++.exe build active file' terminated with code -1.
I can debug anyway, show errors or abort. If I press show errors, nothing happens, it doesn't take me anywhere. If i press debug anyway, I get another message saying: lauch: program 'c:\Users\me\HelloWorld\program.exe' does not exist. I can either cancel, or press open launch.json. If I press the latter, it takes me to the launch.json file as expected, to the line with "name": "g++.exe - Build and debug active file",. I use the Mingw-w64 compiler.
launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "g++.exe - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++.exe build active file"
}
]
}
tasks.json
{
"tasks": [
{
"type": "cppbuild",
"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",
"${workspaceFolder}\\*.cpp",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
To build a program that consists of multiple files you need to use sort of a build system. Two most popular ones are make and cmake. You need to get more familiar with them. make comes with mingw. cmake needs to be downloaded and installed separately, and finally also requires either make, ninja or full version of Visual Studio.
Alternatively, you can try to use Visual Studio 2019, where you just add source files to project and VS takes care of compiling everything on its own. However note that VS2019 does not support Mingw/gcc, and uses Microsoft C++ compiler instead. Depending on your task it may or may not work for you.
I'm having difficulties installing the OpenAL audio library in C++. I guess the headers are in the correct folder and the problem is in the lib files, because VS Code doesn't show any error when I include the library (#include <AL/al.h> and #include <AL/alc.h>), but when I try to compile my code from the terminal I get the following error:
C:\Users\ALEXAN~1\AppData\Local\Temp\ccNZ4t3C.o:Proves.cpp:(.text+0x1c): undefined reference to `__imp_alcOpenDevice'
C:\Users\ALEXAN~1\AppData\Local\Temp\ccNZ4t3C.o:Proves.cpp:(.text+0x30): undefined reference to `__imp_alcCloseDevice'
collect2.exe: error: ld returned 1 exit status
Where and how I've installed the library:
I've downloaded and unziped the Windows binaries. Then I've run the cpp -v command to find the C++ include directories, which showed 3 directories. I moved the AL folder (that contains the headers) to the C:\ProgramData\chocolatey\lib\mingw\tools\install\mingw64\x86_64-w64-mingw32\include directory and the Win32 lib files (called libOpenAL32.dll.a, OpenAL32.def and OpenAL32.lib) to the C:\ProgramData\chocolatey\lib\mingw\tools\install\mingw64\x86_64-w64-mingw32\lib directory. I'm not sure if I have to put there the 3 of them or just the one called libOpenAL32.dll.a.
I know there are similar posts on stackoverflow and I've read some of them, but it's the first time I install a library in C++ and it's difficult for me to understand them. If someone could provide a clear explanation on how to complete the installation I'd be very grateful.
I've found a solution to my problem thanks to #drescherjm and to this answer by #MemzIcon. I've modified a bit the code in the answer so I can use it for any file in the project by changing ${workspaceFolder} by ${fileDirname}.
Moreover, I've created a C:\Libraries directory with two folders: Include, where I'll store my external headers, and Libs, where I'll store my external library files. This way, the libraries I install are not mixed with the other C++ libraries.
This was the first tasks.json I created:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "Compiler",
"type": "shell",
"command": "g++",
"args": [
"-c",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.o",
"-IC:\\Libraries\\Include"
]
},
{
"label": "Linker",
"type": "shell",
"command": "g++",
"args": [
"${fileDirname}\\${fileBasenameNoExtension}.o",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"-LC:\\Libraries\\Libs",
"-llibOpenAL32"
]
},
{
"label": "Build OpenAL",
"dependsOn": [
"Compiler",
"Linker"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
However, it didn't work properly, because it executed the second command called "Linker" when the <file>.o hadn't been created yet. In consequence, when I pressed the Ctrl+Shift+B shortcut, the "Linker" command throwed an error because it didn't find the object file. To solve this, I created just ONE command instead of 3. Inside of this command I used the ; symbol to separate the Compiler and the Linker parts. Finally, this is the aspect of my tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"label": "Build OpenAL",
"type": "shell",
"command": "g++",
"args": [
"-c",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.o",
"-IC:\\Libraries\\Include",
";",
"g++",
"${fileDirname}\\${fileBasenameNoExtension}.o",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"-LC:\\Libraries\\Libs",
"-llibOpenAL32",
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
I have the following task in VSCode to build C++ programs.
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build my app",
"type": "shell",
"command": "g++",
"args": [
"-std=gnu++0x", "-Wall", "-Werror", "-g", "tutorial.cpp"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
However each time I run this the output opens in an external terminal. I would like to run my builds and see the output in the internal terminal.
Is this possible and if so what do I need to change?
The integrated terminal panel behavior is controlled through a presentation property of a task. Add the following:
"presentation": {
"reveal": "always",
"panel": "new"
}
It offers the following options:
reveal
focus
echo
panel
I am a new c++ learner and using visual studio code as my IDE. As an example, a cpp file has the hello world program written in it. In VSC, I compile the cpp file with a task named Build( >Tasks: Run Build task), and run it using the task named Run( >Tasks: Run task). (tasks.json is given below)
But when I Run, a new terminal named "Task - Run" starts, shows the output and immediately exits.(Its very hard to see what was the output.)
Is there any command that I can put into the tasks.json file so that the program doesn't disappear after it ends? Or is there any other workaround?
//my tasks.json file
{
"version": "2.0.0",
"tasks": [
{
"taskName": "Build",
"type": "shell",
"command": "g++",
"args": [
"main.cpp"
],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": []
},
{
"taskName": "Run",
"type": "shell",
"command": "./a.out",
"problemMatcher": []
}
]
}
More of a hack than a fix but I have the same issue and I noticed that changing the version from "2.0.0" to "0.1.0" would keep my output window open with all the errors present.
Though I'm quite sure that this is not a long term solution.