The proper way to compile SFML on vscode with G++ - c++

I want to know which is the proper way to use and configure VSCode build tasks, using G++ and some libraries (in this case, SFML).
I'm using linux mint, when i type the build command directly on the terminal it works fine and compile the source.
I used 'sudo apt-get install libsfml-dev' to install sfml.
SFML is located at: '/usr/include/SFML/'.
But when using the same build command (the same args) inside a vscode task.json, the compiler cannot include SFML/Graphics.hpp.
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "Build",
"command": "/usr/bin/g++",
"args": [
"-g", "${workspaceFolder}/source/Source.cpp",
"-o", "${workspaceFolder}/release/AppLx",
"-lsfml-graphics", "-lsfml-window", "-lsfml-system", "-lm",
"-I/usr/include/"
],
"options": {
"cwd": "${workspaceFolder}/release/"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: /usr/bin/g++"
}
]
}
This is the build output:
(EDIT)
While trying to add the include to the intellisense, i saw that even the 'c_cpp_properties.json' can't locate the SFML, but as showed on the 2d image, the SFML folder exists.
VS version: 1.60.2 (Official package on Software Manager).

Related

Visual Studio Code C++ Build finished with error(s)

I'm trying to run a simple hello world application. Whenever I try to build using g++ it gives me the following error. I can't generate the .exe file or anything. I re-installed mingw using msys2. Walked through tutorials etc.. same problem occurs. The paths provided below are supposed to be correct made sure of it
* Executing task: C/C++: g++.exe build active file
Starting build...
C:\msys64\mingw64\bin\g++.exe -fdiagnostics-color=always -g3 -Wall H:\VSCode\CPP\helloworld.cpp -o H:\VSCode\CPP\helloworld.exe
The system cannot find the path specified.
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.
I walked through tutorial to build and debug c++ from the beginning to the end still the same problem. tasks.json code is below. I sticked with default "args" but same problem as well. I copied the tutorials args same problem.
{
"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",
"-g3",
"-Wall",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
// "${fileDirname}" also gives the same problem.
"cwd": "C:\\msys64\\mingw64\\bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
},
"detail": "compiler: C:\\msys64\\mingw64\\bin\\g++.exe"
},
{
"type": "shell",
"label": "Run C/C++: g++.exe",
"command": "C:\\msys64\\mingw64\\bin\\g++.exe -g3 -Wall \"${file}\" -o \"${fileDirname}\\${fileBasenameNoExtension}.exe\"",
"options": {
"cwd": "C:\\msys64\\mingw64\\bin"
},
"problemMatcher": [
"$gcc"
],
"detail": "compiler: C:\\msys64\\mingw64\\bin\\g++.exe"
}
]
}
edit:
helloworld.cpp
#include <iostream>
using namespace std;
int main(){
cout<<"Hello World!"<<endl;
return 0;
}
When I run it using Run Code using CodeRunner extension it runs normally, but Run/Debug C++ Code gives the error provided.
Whenever I try to see the errors, it shows nothing.
Another Edit:
when running C:\msys64\mingw64\bin\g++.exe -fdiagnostics-color=always -g3 -Wall H:\VSCode\CPP\helloworld.cpp -o H:\VSCode\CPP\helloworld.exe in cmd it build successfully and generate the .exe file.

VS Code G++ intellisense settings not working and compiler not running to completion

So I'm trying to use vs code as my ide on linux for the GNU c/c++ toolset.
Thus far not with a great amount of success.
The first part is intellisense.
I am using the Microsoft c/c++ extension for this.
I followed what common sense told me and then I also followed what the extenstion told me to do which ended up being the same thing.
See below. Even after reloading the window intellisense is not working.
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"/Data/Documents/CensusProject/gdallib/src/compiled/include/**"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "gnu17",
"cppStandard": "gnu++17",
"intelliSenseMode": "linux-gcc-x64"
}
],
"version": 4
}
The next part is building.
To build from the commandline I have to do the following:
PKG_CONFIG_PATH=/Data/Documents/CensusProject/gdallib/src/compiled/lib/pkgconfig
LIBRARY_PATH=/Data/Documents/CensusProject/gdallib/src/compiled/lib
LD_LIBRARY_PATH=/Data/Documents/CensusProject/gdallib/src/compiled/lib/
C_INCLUDE_PATH=/Data/Documents/CensusProject/gdallib/src/compiled/include/
CPLUS_INCLUDE_PATH=/Data/Documents/CensusProject/gdallib/src/compiled/include/
export PKG_CONFIG_PATH
export LIBRARY_PATH
export LD_LIBRARY_PATH
export C_INCLUDE_PATH
export CPLUS_INCLUDE_PATH
So I placed these in tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}",
"env":{
"PKG_CONFIG_PATH":"/Data/Documents/CensusProject/gdallib/src/compiled/lib/pkgconfig",
"LIBRARY_PATH":"/Data/Documents/CensusProject/gdallib/src/compiled/lib",
"LD_LIBRARY_PATH":"/Data/Documents/CensusProject/gdallib/src/compiled/lib/",
"C_INCLUDE_PATH":"/Data/Documents/CensusProject/gdallib/src/compiled/include/",
"CPLUS_INCLUDE_PATH":"/Data/Documents/CensusProject/gdallib/src/compiled/include/"
}
},
"problemMatcher": [
"$gcc"
],
"group": "build",
"detail": "compiler: /usr/bin/g++"
}
]
}
Running the task lead to
Starting build...
/usr/bin/g++ -g /Data/Documents/CensusProject/gdallib/src/compiled/GdalTest/test.cpp -o /Data/Documents/CensusProject/gdallib/src/compiled/GdalTest/test
collect2: fatal error: cannot find 'ld'
compilation terminated.
Build finished with error(s).
The terminal process terminated with exit code: -1.
which is a bit of an improvement, as before the compiler errored about not finding the header files etc... its sort of an improvement.
in the shell this works fine.
Is anyone else familiar with these problems ?
Thanks in advance.
In your case, the directory where the "ld" program was located had not been defined in the PATH environment variable. Firstly you should add it.
Example:-
export
PATH=$PATH:/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin

gdb C++ debugger not working properly on Windows; Visual Studio Code Debugger

Overview
I have been trying to get C++ debugging to work in VS Code for quite some time now with no success.
I followed the official guide from Microsoft on setting up debugging in VS Code, but still ran into problems.
For context:
VS Code config files
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": false,
"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
{
"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": "${workspaceFolder}"
},
"problemMatcher": ["$gcc"],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
The issue
The problem branches out into two parts.
1. Firstly, I normally use git bash as my default shell in Windows, and would like to keep it that way when debugging C++. However, something that seems to be specific to it is an issue with paths. It seems that running the task here leads to the shell ignoring slashes between directories (likely because of the disparate file system interpretations). This can be seen in the following example:
> Executing task: 'C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin\g++.exe' -g C:\Users\Redact\Desktop\code\Competitive_Programming\Evaluation.cpp -o C:\Users\Redact\Desktop\code\Competitive_Programming\Evaluation.exe <
g++.exe: error: C:UsersRedactDesktopcodeCompetitive_ProgrammingEvaluation.cpp: No such file or directory
g++.exe: fatal error: no input files
compilation terminated.
The terminal process "C:\Program Files\Git\bin\bash.exe '-c', ''C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin\g++.exe' -g C:\Users\Redact\Desktop\code\Competitive_Programming\Evaluation.cpp -o C:\Users\Redact\Desktop\code\Competitive_Programming\Evaluation.exe'" terminated with exit code: 1.
2. When running the debugger with either PowerShell or cmd as the shell, pre-build works as intended and a debugging session is opened, but nothing else happens. The code doesn't stop in the main function (as the guide stated it should) nor are any breakpoints hit—indicating it's not running at all:
(Click Here to see moving gif if it appears still here)
Any help is very much appreciated.

Visual Studio Code WSL can't compile files in /mnt/c/OneDrive

I usually store all my programming files in One Drive so that I can access them anywhere I go. But now I need to be able to compile my c++ programs on Linux machines. So I tried using visual studio code in WSL mode but when I try to compile and debug my code I am told that it can't find any *.cpp fies in the specified location, yet the files are clearly there. If I run the compile command on my own in the ubuntu window it works just fine. When I moved the files from /mnt to my home directory everything worked fine. Why would my compile command work just fine when I use it from the ubuntu window, but now work when VS Code tries to do it? Any ideas?
Here is my task.json file and launch.json file:
task.json
{
"tasks": [
{
"type": "shell",
"label": "g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-g",
"${cwd}/*.cpp",
"-o",
"${cwd}/main"
],
"options": {
"cwd": "/usr/bin"
}
}
],
"version": "2.0.0"
}
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "g++ build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${cwd}/main",
"args": ["test1.txt"],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "g++ build active file",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}

Visual Studio Code with Bash Terminal (WSL) in C++ builds only .out files - not .exe

I'm using Visual Studio Code on Windows 10 with the Linux Subsystem. (Ubuntu)
I've created a small c++ file and when I'm building it with the bash terminal - only a .out file is created. Which is fine, but I want to debug it as well and in that case - I can only open .exe files.
When I switch to powershell from bash - the build extension is an .exe
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "echo",
"type": "shell",
"command": "g++",
"args": [
"-g", "main.cpp"
],
"group": { "kind": "build", "isDefault": true }
}
]
}
launch.json for debugging
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/a.exe", // .out doesn't work here
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe",
"preLaunchTask": "echo",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
main.cpp
#include <iostream>
int main() {
std::cout << "hello a" << std::endl;
}
I'm really not sure what to do - as I'm not able to debug .out files and I'd like to choose the build extension myself.
Found the solution:
sudo apt-get install mingw-w64
And then inside tasks.json
"command": "i686-w64-mingw32-g++"
That compiles a 32 bit exe - but the 64 bit version with x86_64-w64-mingw32-g++ somehow doesn't work. Creates an invalid exe.
In order to run/debug cross-platform, your have to use cross-compilers such as MinGW. You can install MinGW-w64 in both Windows, Linux (or WSL).
VScode-tools with gdb.exe from MinGW can debug *.out as well as *.exe files (Tested).
To compile 64-bit version you need to include some static depedency library for C/C++ which are libgcc and libstdc++ while compile. so the command and args in task.json should be:
"command": "x86_64-w64-mingw32-g++",
"args": [
"-g", //Need for debug and compatibility
"-static-libgcc", //flag for the libgcc
"-static-libstdc++", //flag for the libgc++
"helloworld.cpp", //C++ source code
"-o", //flag for output
"a2.out" //Output filename and extension (can be .exe)
],
You can learn more about C/C++ Standard Library Dependencies here