SDL2_image not found - c++

I am trying to compile the following code which has the headers:
#include <SDL2/SDL.h>
#include <SDL2_image/SDL_image.h>
However after running the following makefile:
g++ -std=c++11 src/main.cpp -lSDL2 -lSDL2_image
I get the following error:
fatal error: SDL2_image/SDL_image.h: No such file or directory
#include <SDL2_image/SDL_image.h>
Any suggestions? Not entirely sure about my installation of SDL_image. I am running this on Ubuntu.

This problem can be solved through installing libsdl2-image-dev package:
apt install libsdl2-image-dev

Run apt-file search SDL_image.h
The result will tell you the location of the include file.
For instance, /usr/include/SDL2/SDL_image.h was returned.
So, when you want to include SDL_image.h, write everything after the include/ in between < >.
Thus, includes should look like the following:
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
See the question's comments for the original discussion regarding this solution.

From SDL documentation, it says that add 'lSDL_image' to the end of the compile line.
cc -o myprogram mysource.o `sdl-config --libs` -lSDL_image
or
gcc -o myprogram mysource.c `sdl-config --libs` -lSDL_image
Here is the reference -> https://www.libsdl.org/projects/docs/SDL_image/SDL_image.html
Section 2.2 Compiling.
So for SDL2, you just need to change 'lSDL_image' to 'lSDL2_image'.

For Windows + SDL2-2.0.8 + SDL_image-2.0.4 + Codeblocks you've got the add both Runtime Binaries and Development Libraries to the compiler and linker. Or else, you'll get the error SDL2_image not found, even with having the dll in your program's directory, this occurs. Hopefully others find this helpful; I had to figure it out myself. Example: If your resources are separate, you'll be adding the two plus your standard SDL2 paths to your compiler and linker. Warning: SDL2_image.h has it's headers assuming that the headers are in the same folder as the SDL2 framework. If you get errors about the image header, include the sub-folder SDL2 from SDL framework in the path and then you should be including SDL2 in the program as: include <SDL.h> rather than include <SDL2/SDL.h>.

Related

Compiling examples from GLFW?

I am trying to compile the file simple.c in glfw-3.2.1/examples on Ubuntu 18.04. I am using the following compilation command:
gcc -o simple simple.c glad.c -lglfw3 -lGL -lm -lXrandr -lXi -lX11 -lXxf86vm -lpthread -ldl -lXinerama -lXcursor
I have copied and pasted glad.c and glad.h into this examples folder, as well as the include folder that came with glad.zip
However, I when I try to compile the code I get the following:
glad.c:25:10: fatal error: glad/glad.h: No such file or directory
#include <glad/glad.h>
I don't understand why this is, since I am including glad.c in the compilation command.
I am following this tutorial to set up glad https://learnopengl.com/Getting-started/Creating-a-window. Unfortunaly, this opengl tutorial is geared towards MS Windows. Is there an easier way to set up glad on Ubuntu (sudo apt install ...)?
What am I missing here?
Thanks
Including glad.c in the compilation command will not bring the header in, it will compile glad.c and bring in the object from the generated file in (so it would be somewhat close to -lglad if you had installed some glad library)
Unfortunately there does not seem to be any ubuntu package for glad, the next simplest thing would be to simply compile the examples along with glfw (just run cmake and make in the glfw folder), but we can fix that anyway.
Understanding the error
glad.c:25:10: fatal error: glad/glad.h: No such file or directory
#include <glad/glad.h>
says "the file glad.c wants a header located in glad/glad.h, but I cannot find it"
This can either be a problem with include paths (gcc is not looking in the directories you intended it to look at), or the file really is not here.
Looking at glad.c and simple.c, they use this syntax:
#include <glad/glad.c
Includes can be of two kinds, either through double-quotes in which case they are called "local includes" and gcc will look for the headers in the current directory, or with angle brackets and they are usually "system includes"; you need to tell gcc where to look for them with the -I option
Fixing it
There are multiple ways to fix that.
We can use a command line/environment the c files expect.
First, respect the zip hierarchy, the post you linked to says there should be two include directories, so you need to put the headers where they were in the zip file (glad.h in the glad directory) ;
then tell gcc to look for include files in the current directory with -I. (. is the current directory)
The command line will then look like something like gcc -o simple simple.c glad.c -I. -lglfw3 -l...
or
change simple.c and glad.c to include "glad.h" instead of <glad/glad.h> ; the files will then look for the file where you had it automatically.
Having tried to compile simple.c the same way you did now, you will also need a linmath.h header; I am not sure if it comes with glad but glad and linmath.h are in the deps directory of glfw in the git tree, I would assume they also are in the tar.

Error: 'GL/glfw3.h: No such file or directory' when compiling C++ programs using OpenGL on Linux

I receive the error message
GL/glfw3.h: No such file or directory
when I try to compile the example program given in the tutorial here, section 'Opening a window'. I have installed all the libraries they referred to in the 'Building on Linux' section. (My distribution is Ubuntu 16.04.)
I have also successfully run
apt-get install libglfw-dev
as I found suggested somewhere as an answer to this issue.
I think the glfw3 library has possibly been installed in a place that the compiler does not know how to automatically access.
How do I find out if this is the case, where should I put it so it can be accessed (/usr/share?), and what exactly do I put there? I've copied a file called libglfw3.a that I found in /usr/local/lib to /usr/share, but the error reoccurred.
I've tried compiling it using
g++ first.c -o first
and
g++ first.c -lglut -lGL -lGLEW -lglfw -o first. That error message alone occurred both times (which makes me think the other libraries don't even need to be linked?)
I'm a pretty new user. The solution is probably obvious.
Easy fix (the solution was indeed obvious).
#include <GL/glfw3.h> should be #include <GLFW/glfw3.h>

SDL2 headers not being found when compiling os x

I can't seem to compile this program. I have other people in my class that are having no problem compiling this code. I'm using the same command to try to compile the program and installed the frameworks in the same directory as them. /Library/Frameworks. I also installed eclipse and followed the zamma.co.uk tutorial to setup sdl2 and that didn't work either. Here is the command i'm running when compiling
g++ -std=c++11 -o Gravity main.cpp Game.cpp Particle.cpp Point.cpp -I/Library/Frameworks/SDL2.framework/Headers -framework SDL2 -framework Cocoa
Note: I have tried both
#include <SDL.h>
and
#include <SDL2/SDL.h>
and neither work
The ld linker error you mention in your comments suggests you may need to pass the -F option or -L in case your features/library search path is not finding your SDL2 installation.
Your problem sounds similar to:
linker command failed, sdl
How do you include files in C++ from the /Library/Framework folder in MAC

Installed freetype through homebrew, can't seem to include headers correctly

On my xcode project, I added the dylib of freetype to the project Link Binary phase.
I ensure /usr/local/include and /usr/local/lib are in search paths in the build settings.
I then include:
#include <freetype2/ft2build.h>
#include FT_FREETYPE_H
But i get an error that freetype.h was not found. Any ideas? I tried including <freetype2/freetype.h> directly, but that leads to more compile problems with include paths in other freetype files.
Looking at the demo programs in "freetype2-demos", I see:
#include <ft2build.h>
#include FT_FREETYPE_H
Also, I think you need your compiler command-line to include -I (path to freetype includes).
For example...
g++ -I (...)/freetype2 myfile.cpp
Here are the instructions. The suggestion there is to compile with something like...
g++ $(freetype-config --cflags) myfile.cpp
...which, if you system is configured correctly, will incorporate the -I option that I previously mentioned.

Problems compiling gtkmm

OS: Fedora 14
Compiler: g++ (GCC) 4.5.1 20100924 (Red Hat 4.5.1-4)
I installed gtkmm24-devel from repository via yum. To make sure the install went as planned I decided to try one of the examples on the page.
#include <gtkmm.h>
int main(int argc, char *argv[]) {
Gtk::Main kit(argc, argv);
Gtk::Window window;
Gtk::Main::run(window);
return 0;
}
I ran the example, and, hey! It said it couldn't find gtkmm.h, no problem, I just forgot to link the library. I added /usr/include/gtkmm-2.4 to my library search through Eclipse. No bueno, g++ still can't find it!
fatal error: gtkmm.h: No such file or directory
I then try to include gtkmm by using #include <gtkmm-2.4/gtkmm.h> and recompile, another error! :(
/usr/include/gtkmm-2.4/gtkmm.h:87:20: fatal error: glibmm.h: No such file or directory
Thanks for reading.
Short answer
Use the output of 'pkg-config gtkmm-2.4 --cflags' for include paths and 'pkg-config gtkmm-2.4 --libs' for libraries to link.
Long answer
It said it couldn't find gtkmm.h, no problem, I just forgot to link the library.
Building a C/C++ program is done in two separate steps. First the source files are compiled, outputting object files; and then the object files are linked together. The error you are getting comes from the compiling step.
On Linux, most libraries come with pkgconfig files to make it easier for other programs to use the libraries. gtkmm also comes with its own pkgconfig files.
You are trying to manually specify /usr/include/gtkmm-2.4 for include path; this is wrong. Instead, use the output of pkgconfig to figure out where the header files are located. To get all the include directories needed for gtkmm, use the following command:
pkg-config gtkmm-2.4 --cflags
For linking, use the following pkgconfig command to get the libraries you need to link with:
pkg-config gtkmm-2.4 --libs
You can test it on the command line by invoking g++ directly.
g++ myfirstprogram.cpp -o myfirstprogram `pkg-config gtkmm-2.4 --cflags --libs`
For more information, see the gtkmm docs: http://library.gnome.org/devel/gtkmm-tutorial/unstable/sec-basics-simple-example.html.en
These steps usually help resolving this problem:
Search your computer for glibmm.h
If found - add its directory to the include path list
If not found - Google for glibmm.h and find out which library it is contained in. You will find out in this case it's (surprise!) glibmm. Install it using your package manager.
The problem, as noted in comments, is a compiler error and the compiler is arguing about a missing (header) file. The steps I described above either find the location of the missing file or help you to install a library that the header file belongs to.