How do i link the SFML libraries in Visual Studio Code? - sfml

I've been trying for hours and I can't seem to do it I've downloaded extensions and asked for help around but everything is just confusing me at this point.
I want to include the SFML libs in my project and I'm trying to use the the Visual Studio Code editor for it but it just won't comply for some reason.
A picture of what it currently looks like.
http://imgur.com/qJPlJua
I've been trying this for hours yesterday also but it just doesn't want to work.

I know the topic is a couple years old now but since I was searching for a way to link the sfml lib in vs code and I first ended up here, I thought I would share this git repo I found, which works pretty well for me so far:
https://github.com/andrew-r-king/sfml-vscode-boilerplate
I'm not using SFML 2.5.1 though, so I had to bring a small change in the c_cpp_properties.json file (I am on Ubuntu 18.04 and installed sfml through package manager)
here my c_cpp_properties.json file:
{
"configurations": [
{
"name": "Linux",
"intelliSenseMode": "gcc-x64",
"includePath": [
"${workspaceFolder}/src",
"/usr/local/include/**",
"/usr/include/**"
],
"defines": [],
"cStandard": "c11",
"cppStandard": "c++17",
"forcedInclude": [
"${workspaceFolder}/src/PCH.hpp"
]
}
],
"version": 4
}

I know this question is about two years old, but after fiddling with my own tasks to solve this problem, and came up with something. This shouldn't be the best way to do it, but this should be good for anyone that finds this answer in the future.
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "Compile",
"type": "shell",
"group": "build",
"command": "g++",
"args": [
"${file}",
"-o",
"${fileBasenameNoExtension}.exe",
"-IC:\\SFML-2.5.1\\include",
"-LC:\\SFML-2.5.1\\lib",
"-lsfml-graphics",
"-lsfml-window",
"-lsfml-system",
],
"problemMatcher": [
"$gcc"
]
}
],
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "shared"
//"showReuseMessage": true
}
}
This should work the same as the above answer. Hit CTRL+SHIFT+B to bring up the Task prompt, or look up Run task in the Command Palette (CTRL+SHIFT+P). Remember to have the .dlls of each library used in the root of the project.
Hope this helps.

I searched and I have found the solution.
In the tasks.json file, define two tasks :
"tasks": [
{
"taskName": "Compilation",
"isBuildCommand": true,
"args": ["-c", "${workspaceRoot}\\main.cpp", "-IC:\\SFML-2.4.0\\include"]
},
{
"taskName": "Liaison du fichier compilé aux bibliothèques SFML",
"args": ["${workspaceRoot}\\main.o", "-o", "sfml-app.exe", "-LC:\\SFML-2.4.0\\lib", "-lsfml-graphics", "-lsfml-window", "-lsfml-system"]
}
],
and add "suppressTaskName": true,
So it's like on Linux.
You compile with CTRL + SHIFT + B. To create the .exe file : CTRL+SHIFT+P --> then "run task" then click on the "Liaison du fichier compilé aux
bibliothèques SFML" task.
the entire file is as (for me):
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "0.1.0",
"command": "g++",
"isShellCommand": true,
"suppressTaskName": true,
"tasks": [
{
"taskName": "Compilation",
"isBuildCommand": true,
"args": ["-c", "${workspaceRoot}\\main.cpp", "-IC:\\SFML-2.4.0\\include"]
},
{
"taskName": "Liaison du fichier compilé aux bibliothèques SFML",
"args": ["${workspaceRoot}\\main.o", "-o", "sfml-app.exe", "-LC:\\SFML-2.4.0\\lib", "-lsfml-graphics", "-lsfml-window", "-lsfml-system"]
}
],
"showOutput": "always"
}

well there is nothing more to say, except all it's written on official web-site:
https://code.visualstudio.com/docs/cpp/config-linux
the only stuff I needed to do is to add additional library links for the compiler, which can be done in tasks.json part:
...
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
"-lsfml-graphics",
"-lsfml-system",
"-lsfml-window"
],
...

Related

opencv.hpp file not found vscode Mac M1

I have been trying to set up opencv on my M1 chip Mac in Vscode, I used home-brew and followed all of the necessary steps to install it on my Mac. I created a vscode project and included it in the c_cpp_properties.json and task.json files. When I wrote up a simple test program I built the program using g++ but the build failed and threw an error stating:
fatal error: 'opencv2/opencv.hpp' file not found
I tried a bunch of different ways of including the libraries but there is still a squiggled line in front of #include <opencv2/opencv.hpp> in my program which wasn't there prior to building the program. Intellisense provides me with suggestions in the program and everything. I went through the few existing questions on stack overflow and none of them had a working solution for me.
I tried a bunch of different solutions that I found online and in and around the forums but nothing seems to be working
These are my c_cpp_properties.json and task.json files:
Task.json:
"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}",
"-I",
"/Users/user/Desktop/OpenCV/opencv/include/opencv2",
"/usr/local/include/opencv4"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": "build",
"detail": "compiler: /usr/bin/g++"
}
]
}
c_cpp_properties.json:
{
"configurations": [
{
"name": "Mac",
"includePath": [
"${workspaceFolder}/**",
"/Users/user/Desktop/OpenCV/opencv/include/opencv2",
"/usr/local/include/opencv4"
],
"defines": [],
"macFrameworkPath": [
"/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks"
],
"compilerPath": "/usr/bin/clang",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "macos-clang-arm64",
"configurationProvider": "ms-vscode.cmake-tools"
}
],
"version": 4
}
the code for my sample code is as follows:
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
///////////////// Images //////////////////////
int main() {
string path = "/Users/brettmylek/Desktop/OpenCV/Img/TestImg.jpg";
Mat img = imread(path);
imshow("Image", img);
waitKey(0);
return 0;
}
it is simply there to open an image to test whether or not the install was successful so I could go on developing my actual project.
I'll start with c_cpp_properties.json more precisely the includePath element. This element is only and only for IntelliSense. It has no other use whatsoever. This should answer why IntelliSense is working.
If you then look into tasks.json one thing that caught my eye is this:
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
--> "-I",
--> "/Users/user/Desktop/OpenCV/opencv/include/opencv2",
--> "/usr/local/include/opencv4"
],
This most likely doesn't work the way you think it does. What you want instead is:
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
"-I/Users/user/Desktop/OpenCV/opencv/include/opencv2",
"-I/usr/local/include/opencv4"
],
EDIT: You might have to link against built opencv libraries (last time I checked it's not a header-only library)
EDIT2: Also are you sure about this path: /Users/user/Desktop/OpenCV/opencv/include/opencv2"
Last time I checked there is no /Users on Linux
EDIT3: I've checked their github. If the error persists it's most likely a wrong include path i.e. instead of
"-I/Users/user/Desktop/OpenCV/opencv/include/opencv2"
Maybe it should be (based on their github) this:
"-I/Users/user/Desktop/OpenCV/opencv/include"

Intellisense does not show function descriptions in Visual Studio Code for C++ using the clang++ compiler on macOS

So I'm just starting to learn C++ and I decided to use Visual Studio Code as my development environment and use the clang++ compiler on macOS.
I followed the official Using Clang in Visual Studio Code guide and ended up with the following configuration files:
tasks.json (compiler build settings)
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "[mthree] 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
}
}
]
}
launch.json (debugger settings)
{
"version": "0.2.0",
"configurations": [
{
"name": "[mthree] clang++ - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb",
"preLaunchTask": "[mthree] clang++ build active file"
}
]
}
c_cpp_properties.json (compiler path and IntelliSense settings)
{
"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
}
Now my problem has to do with Intellisense -- while the code completion/suggestion works fine, I just don't see any of the function descriptions.
Here is a simple example:
No description for the append() function
If I go to the definition of the string append function, it takes me to /Library/Developer/CommandLineTools/usr/include/c++/v1/string. And yes, this file happens indeed to not have any descriptive documentation in it. Here's what it says at the top:
// -*- C++ -*-
//===--------------------------- string -----------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
Therefore, does anyone know what I should do in order for Intellisense to show the complete documentation (i.e tell me what the functions do in 'plain English')?
Thanks!
I had the same issue. I found this post. I ended up installing gcc in Homebrew, but didn't set "compilerPath": "/opt/homebrew/Cellar/gcc/11.2.0/bin/g++-11" and got the function description showing up properly.

How can I include .dylib files in ".json" configuration files? - VS Code on Mac iOS - Clang++ compiler - Language: c++

I unsuccessfully was looking 2 days on google to find a clear paragraph to describe how to include dynamic libraries (files with .dylib extension on Mac iOS) in order to be compiled by clang++ when someone is setting up the task.json and/or c_cpp_properties.json files - (prior to press F5 in order to launch the task and execute the source code)
Particularly I would like to include next two .dylib files:
/usr/local/Cellar/glfw/3.3.3/lib/libglfw.3.3.dylib;
/usr/local/Cellar/glew/2.2.0_1/lib/libGLEW.2.2.0.dylib;
Have to mention that I successfully succeeded to make the clang++ to compile in the OpenGL main.cpp file both glew.h and glfw3.h headers as per the following snippet:
// GLEW
#define GLEW_STATIC
#include <GL/glew.h>
// GLFW
#include <GLFW/glfw3.h>
...
This task was accomplished writing the next c_cpp_properties.json file:
{
"configurations": [
{
"name": "Mac",
"includePath": [
"${workspaceFolder}/**",
"${workspaceFolder}/include"
],
"defines": [],
"macFrameworkPath": [
"/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks"
],
"compilerPath": "/usr/bin/clang++",
"cStandard": "gnu17",
"cppStandard": "gnu++14",
"intelliSenseMode": "macos-gcc-x64"
}
],
"version": 4
}
where the magic is done by "macFrameworkPath" instruction.
But yet is not enough.
I still have this error when the clang++ compiles:
clang: error: linker command failed with exit code 1 (use -v to see invocation).
And this - as I understood after googling the solution - happens because is necessary to include those two dynamic libraries which I mentioned earlier.
But as I said in the beginning I didn't find how to do it.
Maybe someone can come up with a clear and appropriate solution.
My best guess is to add a new argument in task.json script, which by the way looks like that:
{
// 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",
"${file}",
"-I/Users/Armonicus/MyProjects/C++VSCodeProjects/projects/helloworld01/include",
"-o",
"${fileDirname}/src/${fileBasenameNoExtension}",
"-Wno-deprecated",
"-Wno-pragma-once-outside-header"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": ["$gcc"],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
Best regards to all readers.
It seems you are indeed need to pass the libs into arguments of clang++.
Try to do this with -l flag, pointing to target library.
Add
"-l /usr/local/Cellar/glfw/3.3.3/lib/libglfw.3.3.dylib"
and
"-l /usr/local/Cellar/glew/2.2.0_1/lib/libGLEW.2.2.0.dylib" to the args list of strings.
The solution to this particular problem is to add in task.json the next command arguments:
task.json
{
// 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",
"${file}",
"-I/Users/Armonicus/MyProjects/C++VSCodeProjects/projects/helloworld01/include",
"/usr/local/Cellar/glfw/3.3.3/lib/libglfw.3.3.dylib",
"/usr/local/Cellar/glew/2.2.0_1/lib/libGLEW.2.2.0.dylib",
"-o",
"${fileDirname}/src/${fileBasenameNoExtension}",
"-Wno-deprecated",
"-Wno-pragma-once-outside-header"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": ["$gcc"],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
As everyone can see, simply writing the full path of the file as additional argument will be accepted by compiler

Setting up VS code for c++ on mac

I am trying to follow this guide exactly
https://code.visualstudio.com/docs/cpp/config-clang-mac
Everything works fine until I reach the step "Debug helloworld.cpp", I am able to use the "tasks.json" file to build the .cpp file and run it in the command line. However as soon as I add the "launch.json" file outlined in "Debug helloworld.cpp" everything stops working and I get the following errors as soon as I try to debug.
expected ';' at end of declaration
range-based for loop is a C++11 extension [-Wc++11-extensions]
I have looked at many other stack overflow posts claiming that clang++ is defaulting to c++03 whereas I need to be using c++11, however the guide above uses
-std=c++17 as an argument.
The guide itself even provides the following related help at the bottom.
If you see build errors mentioning "C++11 extensions", you may not have updated your task.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 task.json file with the code block provided in the Build helloworld.cpp section.
Which I definitely did do as you can see in the tasks.json file I linked below, so I don't see what the problem could be.
Any help at all would be appreciated.
tasks.json file...
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "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": "build"
},
{
"type": "cppbuild",
"label": "C/C++: clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
]
}
launch.json file...
{
// 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": "clang++ - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb",
"preLaunchTask": "C/C++: clang++ build active file"
}
]
}
c_cpp_properties.json file...
{
"configurations": [
{
"name": "Mac",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"macFrameworkPath": [
"/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks"
],
"compilerPath": "/usr/bin/clang",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}
Your launch.json calls the "C/C++: clang++ build active file" task. You want it to call your "clang++ build active file" task.
You don't need the -stdlib=libc++ on a Mac.
That is because your VSCode Debug Extension, does little matter with configuration files.
Do not use Microsoft C/C++(Identifier: ms-vscode.cpptools, for C/C++
IntelliSense, debugging, and code browsing)!
Just use CodeLLDB to debug C++ on mac. Just use CodeLLDBv1.4.5. Do not use the latest version of CodeLLDB(CodeLLDBv1.6.x has caused much more buggers since Nov 5, 2020, and never fix. that bugger version 1.6.10 has taken me seven days to config debug C++
with VSCode on mac, made me in trouble, made me depressed even masturbate 4 times in a week.).
Setup C++11 debug with VSCode on mac: Details of successful configuration refer to here.

vscode clang++: no such file or directory: 'main.cpp'

Okay, so this is terribly frustrating.
I am simply trying to using clang++ in my VSCode editor to compile a very simple main.cpp program, located in the 'main' module of my project.
The project hierarchy is as follows:
${workspaceFolder}/
cpp/
main/
main.cpp
There are many other folders and files that I have purposefully omitted for the sake of simplicity.
Here is my terminal output from my tasks.json build task:
> Executing task: clang++ -Wall -std=c++17 -stdlib=libc++ -o main.out main.cpp -I/Users/ajm/Projects/restaurant/cpp/main/ --debug <
clang: error: no such file or directory: 'main.cpp'
clang: error: no input files
The terminal process terminated with exit code: 1
main.cpp is clearly in the folder that I am trying to include with -I (see the following screenshot):
What the heck am I missing here??? I am very tempted to switch my C++ IDE to either Visual Studio or Xcode, as besides this issue, VSCode's C++ autocomplete is buggy and terribly slow.
Note that I am new to using clang++. I have read through 5-10 other SO posts of others going through similar issues, none of which have helped. I also haven't found anything helpful on Clang quickstart guides on LLVM or Microsoft's website.
If anyone needs more info to answer the question, just let me know.
Someone, please help! Thanks in advance.
EDIT:
I figured I would also include my tasks.json, c_cpp_properties.json, and launch.json files.
tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"label": "Build with Clang",
"type": "shell",
"command": "clang++",
"args": [
"-Wall",
"-std=c++17",
"-stdlib=libc++",
"-o",
"main.out",
"main.cpp",
"-I${workspaceFolder}/cpp/main/",
"--debug"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
c_cpp_properties.json:
{
"configurations": [
{
"name": "Mac",
"includePath": [
"${workspaceFolder}/**",
"/usr/local/boost_1_71_0"
],
"defines": [],
"macFrameworkPath": [
"/System/Library/Frameworks",
"/Library/Frameworks"
],
"compilerPath": "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "${default}",
"browse": {
"path": []
}
}
],
"version": 4
}
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": "(lldb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "enter program name, for example ${workspaceFolder}/main.out",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb",
"logging": {
"trace": true,
"traceResponse": true,
"engineLogging": true
}
}
]
}