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

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>

Related

C++ Executable cannot find library at runtime, even though it's in /usr/lib (On Linux)

I'm creating a game in c++ with the Panda3D framework on Ubuntu. All of the Panda3D shared libraries are in /usr/lib/panda3d and all of the headers are in /usr/include/panda3d. I'm compiling with SCons, but I've tried it with gcc and it's the same, so here are the commands:
g++ -o src/main.o -c -fPIC -O2 -std=gnu++11 -I/usr/include/python2.7 -I/usr/include/panda3d -Iinclude src/main.cpp
g++ -o Test src/main.o -L/usr/lib/panda3d -lp3framework -lpanda -lpandafx -lpandaexpress -lpandabullet -lp3dtoolconfig -lp3dtool -lp3direct -lpthread
And here is the error I get when I run the executable:
./Test: error while loading shared libraries: libp3framework.so.1.11: cannot open shared object file: No such file or directory
There isn't much in the code, just initializing a Panda3D window, so I doubt that's the culprit.
As I said before, the libraries are in /usr/lib/panda3d, I've checked about a million times now and it's driving me crazy. I can't think of a single reason why I would get this error. Any help is appreciated :)
Edit:
I was looking through my files and there's a panda3d.conf file in /etc/ld.so.conf.d with one line: /usr/local/lib/x86_64-linux-gnu/panda3d. Does this have anything to do with it?
Edit #2:
I used the path in the above edit as the library path and got the same results, unfortunately.
I can't think of a single reason why I would get this error.
The reason is very simple: the dynamic loader hasn't been told to look in /usr/lib/panda3d for shared libraries, and so doesn't.
You can run your program with:
LD_DEBUG=files,libs ./Test
and observe which directories the loader is searching.
panda3d.conf ... with one line: /usr/local/lib/x86_64-linux-gnu/panda3d
That is the wrong directory (or at least not the one where your libraries are).
One way to fix this is to correct the above path to /usr/lib/panda3d and run sudo ldconfig.
Another way is to add -Wl,-rpath=/usr/lib/panda3d to your link line.

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.

SDL2_image not found

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>.

Cannot find .dll using MinGW and MSYS

OS: Windows 7
I have the source, as well as all the library files I'm using in one directory, on my desktop. I'm running the shell by using msys.bat, which was created when I installed MinGW. I've tried to run the following (and many others):
g++ -I. -L. -o opengltest.exe opengltest.cpp -lglew32 -lglew32s -lglew32.dll
I recieve the following error:
c:/mingw/bin/../lib/gcc/mingw32/4.6.2/../../../../mingw32/bin/ld.exe: cannot find -lglew32.dll
collect2: ld returned 1 exit status
This also happens when I use the -L switch and the entire C:/Users/... path, but I get the same error. Again, glew32.dll is in the same directory that the .cpp is in, which is the same directory I'm working in within the shell. I've tried multiple solutions from multiple posts, and it still seems like I'm missing something. I thought using the -L. was a straightforward way to tell MinGW to look in the working directory, but apparently it doesn't work that way.
Do not link against both the dynamic and static linking version of glew in the same application. This makes no sense. Also do no link against glew32.dll, this makes equally little sense.
Most importantly, do not use the DLL version of glew with g++ at all - it will not work (see one of my previous answers to understand why). Instead, #define GLEW_STATIC (better if you use -DGLEW_STATIC as a compiler switch) and only link to glew32s.

How to use OpenSSL with GCC?

I'm trying to use openssl in a gcc program but it isn't working.
g++ server.cpp /usr/lib/libssl.a -o server
gives an error message, as does anything with the -l option. What must I type on the command line to link with openssl? The file /usr/lib/libssl.a exists, but nevertheless I still get the linker error no such function MD5() exists.
Without knowing the exact errors you are seeing, it is difficult to provide an exact solution. Here is my best attempt.
From the information you provided, it sounds as though the linker is failing because it cannot find a reference to the md5 function in libssl.a. I believe this function is actually in libcrypto so you may need to specify this library as well.
g++ server.cpp -L/usr/lib -lssl -lcrypto -o server
You or others may find this article developerWorks article helpful.
Secure programming with the OpenSSL API
https://developer.ibm.com/technologies/linux/tutorials/l-openssl
It describes most things you need to know to get off the ground with OpenSSL and C/C++. If you find you are following most of the same steps, it might help you see what needs doing.
Good luck.
update
revised link: https://developer.ibm.com/technologies/linux/tutorials/l-openssl
Which has been shuffled around
original link: http://www.ibm.com/developerworks/linux/library/l-openssl.html
Which now goes to a digest page including the article.
Note: keeping both links because they be used to find new discoveries.
In Eclipse IDE select Your Project property --> c/c++ Build --> Settings gcc c linker(from tools settings)--> add to Library Search Path (-L)
/usr/lib -lssl -lcrypto
The location of the library is not fixed. In my case (Ubuntu 18.04), the .a files are located in /usr/lib/x86_64-linux-gnu/. So here are the complete steps:
1) install the library,
sudo apt install libss-dev
2) check the installed files,
dpkg-query -L libssl-dev
3) change the gcc flags -L(library directory) -l(library name), e.g.,
gcc XXX.c XXXXX.c -L/usr/lib/x86_64-linux-gnu/ -lcrypto -lssl
On top of the accepted answers, I could not make compile the OpenSSL example for AES-CCM:
https://github.com/openssl/openssl/blob/master/demos/evp/aesccm.c
To make it work I needed to add two more things:
The Dinamic Linking Library : -ldl
The PThread library to use POSIX threading support: -pthread (Adding directly the library with -lpthread is not recommended )