Hello i have been trying to learn c++ SFML by using visual studo code.
After watching a tutorial how to install sfml in c++ i had everything set.
But, the problem is when i try to compile it gives me this error:
"main.cpp:2:10: fatal error: SFML/Graphics.hpp: No such file or directory".
I have been throught many guides but none of them appeared to be working.
Here is my code:
#include <SFML/Graphics.hpp>
#include <iostream>
int main() {
sf::RenderWindow window(sf::VideoMode(1280,720),"Nareszcie");
while(window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type==sf::Event::Closed)
{
window.close();
}
window.clear();
}
}
return 0;
}
Hope someone will help me in solving this problem.
tasks.json:
{
"tasks": [
{
"type": "shell",
"label": "g++.exe build active file",
"command": "C:/mingw32/bin/g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "C:/mingw32/bin"
}
},
{
"type": "shell",
"label": "cpp.exe build active file",
"command": "C:\\mingw32\\bin\\cpp.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "C:\\mingw32\\bin"
}
}
],
"version": "2.0.0"
}
c_cpp_prperties.json:
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"C:/SFML-2.5.1/include/**",
"C:/SFML-2.5.1"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.18362.0",
"compilerPath": "C:/mingw32/bin/g++.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "${default}"
}
],
"version": 4
}
You will need to add the include directory to your build instructions too.
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"-I",
"C:/SFML-2.5.1/include/"
],
However, you will also need to link your executable to the SFML libraries.
"args": [
"-g",
"${file}",
"C:/path/to/sfml/libsfml-graphic.a" // something like that
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"-I",
"C:/SFML-2.5.1/include/",
],
My recommendation would be to use a proper build system such as CMake, meson or others, which will do this kind of things automatically. Here's a CMake example:
cmake_minimum_required(VERSION 3.14)
project(your-project CXX)
# creates your executable with two cpp file in it
add_executable(my-exe main.cpp otherfile.cpp)
# find sfml. Assume the command line argument -DCMAKE_PREFIX_PATH="C:/SFML-2.5.1"
find_package(SFML 2.5.1 COMPONENTS graphic REQUIRED)
# configure your project correctly. Take care of include directories and linking
target_link_libraries(my-exe PUBLIC sfml-graphic)
VSCode also has a nice plugin for these build systems.
Related
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"
This question already has answers here:
How to avoid errors in Vscode for putting header files in a separate directory than src
(2 answers)
#include errors detected in vscode
(22 answers)
Closed 4 months ago.
I downloaded ncurses through msys64 then tried to include it in my source file as #include <ncurses.h>, then it didn't work so I tried changing the c_cpp_properties.json file
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"C:\\msys64\\mingw64\\include",
"C:\\MinGW\\include"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.19041.0",
"compilerPath": "C:\\MinGW\\bin\\g++.exe",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "windows-gcc-x64"
}
],
"version": 4
}
the error persisted, what am I doing wrong
not sure what am I doing wrong, help is appreciated
i solved this problem in my vscode but i use linux, maybe this can be useful for you:
I inserted in the file "tasks.json", in the category args (cpp compiler argument) the option "** - I **", where I could define where my library is:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ compila il file attivo",
"command": "/usr/bin/g++",
"args": [
"-fdiagnostics-color=always",
"-I", //option of the compiler for set the library folder
"/usr/local/include/png++-0.2.10/", //patch to the library folder
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Attività generata dal debugger."
}
],
"version": "2.0.0"
}
this will probably help you
I am trying to include the SOCI library in my C++ library. The compilation fails with:
fatal error: soci/soci.h: No such file or directory
3 | #include "soci/soci.h"
I have downloaded and built the library in the path:
/users/niko/.env/libraries
My c_cpp_properties.json looks like:
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"users/niko/.env/libraries/include/**"
],
"defines": [],
"compilerPath": "/opt/devtoolset//gcc",
"cStandard": "gnu17",
"cppStandard": "c++20",
"intelliSenseMode": "linux-gcc-x64",
"compilerArgs": [
"-Wall",
"-Wextra",
"-Wconversion",
"-Wpedantic",
"-I /users/niko/.env/libraries/include/**"
]
}
],
"version": 4
}
And correspondingly, the tasks.json looks like:
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: cpp build active file",
"command": "/opt/devtoolset//cpp",
"args": [
"-fdiagnostics-color=always",
"-I",
"users/niko/.env/libraries/include/*",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": "build",
"detail": "compiler: /opt/devtoolset//cpp"
}
]
}
IntelliSense correctly finds it from this path, but the compilation fails. I have tried various things on SO, but nothing seems to work.
This question already has answers here:
VSCode c++ task.json include path and libraries
(4 answers)
Closed 1 year ago.
I'm trying to use Eigen in my C++ program but this error appears (paths are simplified).
Starting build...
"C:\Program Files\(...)\g++.exe" -g D:\(...)\main.cpp -o D:\(...)\main.exe
D:\(...)\main.cpp:2:10: fatal error: Eigen/Dense: No such file or directory
#include <Eigen/Dense>
^~~~~~~~~~~~~
I included Eigen directory in .vscode/c_cpp_properties.json.
The Eigen folder with Dense file - and others - inside is listed in my Explorer sidebar, under the project I want to use it in.
This is my c_cpp_properties.json:
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${default}",
"D:\\programming\\_lib\\cpp\\eigen-3.3.9\\"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.18362.0",
"compilerPath": "C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/g++.exe",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "linux-gcc-x64"
}
],
"version": 4
}
It seems that Eigen include is not used in the compiler command. Is that right? If so do I include it manually via "Compiler arguments" or "Compile commands" setting or do something else?
Following #ThomasSablik suggestion I used -I argument to add the Eigen path in .vscode/tasks.json file inside my project folder. It worked. Contents of the file:
{
"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",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"-I",
"D:\\programming\\_lib\\cpp\\eigen-3.3.9\\",
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
With VScode, how can this error be fixed?
#pragma once in main file [-Wpragma-once-outside-header]
Update:
Showing in VScode:
Update Again:
Here are my current VScode settings in c_cpp_properties.json
{
"configurations": [
{
"name": "Mac",
"includePath": ["${workspaceFolder}/**"],
"defines": [],
"macFrameworkPath": [
"/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/System/Library/Frameworks"
],
"compilerPath": "/usr/bin/clang",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}
Given that there's no answer, and given that I also had some hard hours trying to fix this, here it goes.
In Visual Studio Code the compile settings are built by default in tasks.json (Terminal > Configure Default Build Task > g++.exe ()). And in VS Code 2020 is this:
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "g++.exe build active file",
"command": "C:\\Program Files (x86)\\mingw-w64\\i686-8.1.0-posix-dwarf-rt_v6-rev0\\mingw32\\bin\\g++.exe",
"args": [
"-g",
"${workspaceFolder}\\*.cpp",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "C:\\Program Files (x86)\\mingw-w64\\i686-8.1.0-posix-dwarf-rt_v6-rev0\\mingw32\\bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
(I am using mingw-64 to support gcc compiler so maybe "command" and "cwd" have different paths depending on the compiler you are using.)
The key part is this: "${file}", which is the name of the active file (active tab) in your Editor.
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
If you are working with several files, one header(.h or .hpp) and one main(.cpp) files at least, VS Code will take that active file (.h or.hpp) as it were the main file (.cpp). So you need to change it with this: "${workspaceFolder}\*.cpp".
"args": [
"-g",
"${workspaceFolder}\\*.cpp",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
I fixed it by moving the .h/.hpp files to include folder.
Maybe there's a setting somewhere not to compile files in include.