Making debug and release configs for c++ in VS Code - c++

I've been looking everywhere for an answer to this question. I'm holding off using an IDE until I can confidently use g++, makefiles, and JSON configs. My issue is that in VS Code, setting up SDL2, I'm not sure how to make a release config (by that I mean code cleaning). Here's what I have in my tasks.json file for debugging.
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "SDL2 Debug",
"command": "C:\\msys64\\mingw64\\bin\\g++.exe",
"args": [
"-g",
"src\\*.cpp",
"-o",
"bin\\debug\\main.exe",
"-IC:/SDL2/include",
"-LC:/SDL2/lib",
"-lmingw32",
"-lSDL2main",
"-lSDL2",
"-mwindows"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
}
A lot of this I just copied off the internet. Which is fine because it works, but not fine because I don't understand JSON or how VS Code uses it. And I can't find any references to explain what all this fancy stuff means. And the main problem is that this just creates a debug build, I'm not even sure if the arguments are correct or if it's messy. I want to create a release config alongside this to clean the code once the project is finished. All I'm looking for is a push in the right direction. Not expecting an outright solution to my problem (although that would be great), I just want to understand what I'm supposed to do here. Any references to read would be awesome, I don't know where to look at this point in time.

Related

Problems getting OpenGL to work in VSCode on Mac

I am trying to complie a basic openGL program in VSCode on mac. I am using glad and GLFW, and I have the Glad files in the same folder as the test.cpp file I am trying to run. However, the include statement throws an error no matter how I type it. This is the program I am writing
#include <iostream>
#include "glad.h"
However, it throws the errors:
screenshot of errors
VSCode cannot find the header files the program needs to run, and the compiler throws errors as a result. I should add, that autocomplete for these libraries is working, however, the file cannot be found by the editor.
Here is the c_cpp_properties.json code
{
"configurations": [
{
"name": "Mac",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"macFrameworkPath": [
"/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks"
],
"compilerPath": "/usr/bin/clang",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "macos-clang-x64"
}
],
"version": 4
}
Perhaps the problem could be realted to how I am trying to compile the file? Here is my tasks.json file
{
"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"
}
Any guidance on how to set this project up correctly, any advice would be greatly appreciated. For reference, here is the tutorial I am trying to follow: https://learnopengl.com/Getting-started/Hello-Window.
Thank you so much!
I'll start by pointing out a few things that aren't really important.
VSCode is not a compiler, nor an IDE. VScode is a text editor. As a text editor it may have some extensions that help you with developing (thus turning it into a make-shift IDE).
What is happening here is that the compiler (gcc) doesn't know where to look for include files. It is even hinting you that you need to update the includePath in a way that you point it to the correct folder with the header files. In the CLI this is done via the -I flag i.e. g++ main.cpp -Ipath/to/your/include_folder/.
If you look into the Getting Started section they even go through these steps and go as far as to suggest the usage of CMake (which is a build tool that helps you generate the makefile with which you build your binaries).
By looking at the tasks.json I can conclude that the extension which you are using in VSCode uses this file to pass down the correct arguments to g++. So the quickest solution for you right now is to specify another element in args that points to the header files i.e.:
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"-Ipath/to/your/include_folder/with_the_headers", <----
"${fileDirname}/${fileBasenameNoExtension}",
],
Hope it helps!

launch: program "directory of the program " does not exit error

I have a header file, a CPP file that defines member functions of the header file and a caller main file that sets and gets some values. When I run the code in ubuntu 20.04 vs code, I get the error "launch:program does not exist error" all the time.
When it comes to running CPP programs without headers involved, it seems that the program builds and runs but after opening header files inside the project folder, I started to get the same type of error. I create the launch json file and try to build it basically. Should I configure json files for the headers as well. They are in the same folder so I thought this would not be required.
I have run the codes in visual studio already and there it works. As you can see from the terminal window, I get undefined reference error. I haven't uploaded the codes because the class and the member functions and all the data variables seem to match with each other and doesn't give any error. I would appreciate it if anyone would help me with this code, I switched to linux and trying to get used to vs code.
this is the json file I created.
{
"version": "2.0.0",
"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": "compiler: /usr/bin/g++"
}
]
}
I followed the exact steps explained in the vs code's Getting started c++ on Linux.

Undefined reference to vtable when functions are defined in VS Code

I've seen a lot of questions about this, but can't seem to see what I'm missing. I am fairly new at C++. I am using Visual Studio Code with G++ and MINGW32 10.3.0. When I attempt to run test.cpp (below) I receive two errors:
...test.cpp:7: undefined reference to 'QData::getDataPacket(void*)
...undefined reference to 'vtable for QData'
// qdata.h
#ifndef QDATA_H_
#define QDATA_H_
//Define generic queue data
class QData {
private:
int data = 17;
public:
void virtual getDataPacket(void* dataptr);
void virtual setDataPacket(void* dataptr);
};
#endif
// qdata.cpp
#include "qdata.h"
void QData::getDataPacket(void* dataptr) {
*(int*)dataptr = data;
}
void QData::setDataPacket(void* dataptr) {
data = *(int*)dataptr;
}
// test.cpp
#include <iostream>
#include "qdata.h"
int main() {
QData qqq;
int a;
qqq.getDataPacket(&a);
std::cout << a << std::endl;
return 0;
}
I know the code works because it was originally all in one file and compiled fine. From my research this is maybe a linking issue? Most questions related to this refer to needing to define your virtual functions, but I have already done that.
If I use the following command in the terminal, binary.exe runs correctly (the output is 17):
g++ -o binary test.cpp qdata.cpp
Is there a way to get this to compile and run correctly without manually typing in a list of cpp files?
Edit: Since there seems to be some confusion, typically in VSCode you can compile and debug in one go by pushing F5. This is the part where I get the errors above. I am hoping someone can help me understand why that is failing and how to fix it so I can continue testing/debugging in VSCode.
Edit: I'm still voting that this question is unique since I was simply following the trace of the compile error in VS Code. I had actually found this article before, and it did not solve my problem. It is also extremely dense and as a beginner was difficult to understand how it might explain my problem. I will add a visual studio code tag to help people find this question. But every other reference to the vtable error I found was to do with the vtable itself and not following the troubleshooting path to a solution in VS Code.
#JaMiT shared a link that helped me get to the answer. As it turns out, the code is fine but the issue is with how VisualStudio Code is configured in the tasks.json file. If you want to compile multiple cpp files with g++ (assuming you used the Using GCC with MINGW getting started guide) you need to modify the "args". Here is my tasks.json that specifies which cpp files to compile:
tasks.json
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "C:\\msys64\\mingw64\\bin\\g++.exe",
"args": [
"-g",
"test.cpp",
"qdata.cpp",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
If you simply want to compile all cpp files in the active directory, then you can modify it as follows:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "C:\\msys64\\mingw64\\bin\\g++.exe",
"args": [
"-g",
"*.cpp",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
Notice that "${file}" has been removed from the "args" in the default tasks.json file.

how do I configure VS code to build in Release mode (clang + macOS)

I am new to VS code. I have configured it using Clang on macOS using the provided [VS code documentation] (https://code.visualstudio.com/docs/cpp/config-clang-mac)
It works, I can build and debug. Great!
My question is: how do I configure VS Code to build in Release mode? I don't seem to be able to find any info in the documentation or a tutorial on how to do it
In case it helps, this is the way tasks.json looks right now
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
"-std=c++17",
"-stdlib=libc++",
"-g",
"-v",
"${file}",
"${workspaceFolder}/source/*.cpp",
"${workspaceFolder}/FFTreal/*.cpp",
"-I.",
"-L",
"${workspaceFolder}/BassLibrary",
"-lbass",
"-o",
"${workspaceFolder}/final.out",
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": ["$gcc"],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
Any idea? Thanks!
Sorry, for the last answer...
So first of all, I need to claim that I'm not really familiar to Clang Family, but I did some research and found that there are two special groups out in tasks.json which VSCode provides: Build and Test. So, I guess that we need to build it in release mode manually where again I don't have any confidence on me...
I found that you can build your Clang scripts using CMake. CMake is simply a builder tool to help you reassemble your whole code, including libraries, dependencies, modules etc.
So, I'm not sure if that'll be possible but you can change your tasks.json configuration from Clang++ to CMake, maybe?
First of all, for a quick answer, I edited your tasks.json snippet to this:
{
"tasks": [
{
"label": "cmake init",
"type": "shell",
"options": {
"cwd": "${workspaceRoot}"
},
"command": "/path/to/cmake",
"args": [
"-S",
"${cwd}",
"-B",
"${cwd}/build"
],
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
},
{
"label": "cmake release builder",
"type": "shell",
"options": {
"cwd": "${workspaceRoot}"
},
"command": "/path/to/cmake",
"args": [
"--build",
"${workspaceRoot}/build",
"--config",
"Release"
],
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
What it does is, basically CMake scans your project folder and create configuration files via the cmake init task. After that, when you run cmake release builder, the task will assume that you have a build folder - which you can change easily - and creates a compact executable file, in build/Release.
But also, in order to use CMake, you need to create a CMakeLists.txt file whose baseName is case-sensitive. So, I created a really simple CMake configuration file which you can add your source files manually, unfortunately, like this:
cmake_minimum_required(VERSION 3.13) # CMake version check
project(simple_example) # Create project "simple_example"
set(CMAKE_CXX_STANDARD 14) # Enable c++14 standard
set(SOURCE_FILES test.cpp add.cpp) # Append source files
# Add executable target with source files listed in SOURCE_FILES variable
add_executable(simple_example ${SOURCE_FILES})
Finally, to make this process REAL, you need to go Terminal > Run Build Task in VSCode.
I'm not really sure that you can use Clang compiler to build a whole application, like an .exe file in Windows. During the research, I focused a lot on the difference between compiler and builder which I recommend to look it up!
I hope that I used English in a comprehensible way and not made confused.

How to get debugging c++ to work in VSCode on a mac?

Can someone explain how to get building and debugging to work in VSCode on a Mac?
Let's assume we successfully installed cpp tools:
-Including creating a proper task file that works on a mac.
-The required changes to launch.json
-Any other step required.
(Don't get me wrong, I'm not lazy, I have been trying for more then 2 hours now and it seems that a proper answer for this question can help a lot of people.)
Once you have the C/C++ extension downloaded, you can use the configurations to generate a project.json in debug window of VsCode. If you do not currently have a project.json under the project's .vscode folder, hit F5 and a dropdown list should show up. There you can select C++ (GDB/LLDB), and this should generate a project.json for you to use.
If you want to just hit F5 so it automatically compiles and debugs your program, you will need to add a tasks.json. This can be done by hitting F1 and selecting Tasks: Configure Task Runner and select Other. Replace "echo" with "gcc" (or clang) and replace the args with your .cpp files and don't forget to add -g.
You can find more information in their documentation: https://code.visualstudio.com/docs/languages/cpp
I don't count the time I lost looking for an answer to this question!
I found the vscode-lldb extension and it works fine, all other solutions I found don't work for me.
You still have to create configuration files, here are mine to debug my unit tests:
I'm using googletest and extension c++14 in this example
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "build & debug tests",
"type": "shell",
"command": "g++",
"args": [
"-g",
"-std=c++14",
"-I/src",
"-lgtest",
"tests/MainTest.cpp",
"-o",
"bin/testMain"
],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": [
"$gcc"
]
}
]
}
launch.json
{
"version": "0.2.0",
"configurations":
[
{
"name": "Debug Tests C/C++",
"type": "lldb",
"request": "launch",
"program": "${workspaceFolder}/bin/testMain",
"args": [],
"cwd": "${workspaceFolder}/tests",
"preLaunchTask": "build & debug tests"
}
]
}