Compiling examples from GLFW? - opengl

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.

Related

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

libxml/parser.h: in c++ ubuntu

Even though I have installed libxml++2.6-2 libxml++2.6-doc etc in my ubuntu 12.04 version again I am getting the below error
fatal error: libxml/parser.h: No such file or directory
I am using make for building the project
Kindly suggest any other libxml libraries which I need to install
libxml/parser.h is a part o libxml library, not libxml++
For any given library, you need development packages (the ones with names ending in -dev) in order to build applications using that library.
You need to pass additional flags to your compiler: xml2-config --cflags and to linker xml2-config --libs.
I don't have access to an Ubuntu system now, but: Maybe you need to install the libxml developer package? Maybe you only have the library but not the include file(s)?
Check in /usr/include, /usr/local/include, ... for the directory libxml and the file parser.h.
If you find the file, you may need to adapt your makefile so that the parent-directory is in the list of include paths, e.g.:
INC = -I/usr/local/include
g++ $(INC) ...
If you did not find the file: Check the available libxml packages for a developer package and install that.
Before Posting the answer THANKS to the people who have answered, but those answers were not worked for me
I have just copied libxml folder from the directory usr/lib/libxml2 and pasted in usr/lib directory and compiled my code it is not giving any error. It is working fine now.
Please read #el.pescado answer before reading this. I wanted to comment on that answer but felt the need to format my code better.
gcc -c <files to compile> `xml2-config --cflags` -o <object files>
gcc <object files> -L<libxml2 lib location> `xml2-config --libs` -o <binary file>
Assuming we have a file names xmltest.c that have code that included libxml2 header like #include <libxml/parser.h>, standard location of libxml2 shared library i.e. /usr/lib64/libxml2, the above code will evaluate like this:
gcc -c xmltest.c -I/usr/include/libxml2 -o xmltest.o
gcc xmltest.o -L/usr/lib64/libxml2 -lxml2 -lz -lm -o xmltest
A better idea is to put together a Makefile that does this automatically.

Cannot find sdl2main

I'm trying to compile code using SDL, I can't really post much of the code here but I can do my best to answer questions about it. The problem occurs when compiling the view code and trying to link the SDL libraries.
g++ -o test test.c -lSDL2main -lSDL2
gives me an error /usr/bin/ld: cannot find -lSDL2main
I know my SDL install is okay because if I leave out the link to SDL2main it compiles fine and runs fine. The problem is there is other code that needs SDL2main. I've poured through my file system and I can't find it and I've searched online pretty exhaustively. I was just hoping someone could help me either resolve the dependency or fix my sdl install if its broken.
If you are using some distro there's probably some tool to search files, even in packages not installed.
For example (I'm using Debian/unstable) apt-file search SDL2main
libsdl2-dev: /usr/lib/i386-linux-gnu/libSDL2main.a
libsdl2-2.0-0:i386 2.0.2+dfsg1-4
libsdl2-dev 2.0.2+dfsg1-4
Once you've verified that the lib exist if it doesn't link there still something to check and try
Check the lib is installed in one of the search path (see How to print the ld(linker) search path)
Or explicit the path
g++ -o test test.c -L/usr/lib/i386-linux-gnu -lSDL2main -lSDL2
Being a static library with some gcc/g++/ld version maybe you need to link the archive as an object, without -l
g++ -o test test.c /usr/lib/i386-linux-gnu/libSDL2main.a -lSDL2

How to use sqlite3.c and sqlite3.h in eclipse C/C++ project

I am trying to use amalgamated version of sqlite3.c and sqlite3.h in my C/C++ project. It has code in both C and C++. I am using eclipse IDE in UBUNTU 12.04.
Now my problem is that after having include sqlite3.h in my file where I need sqlite3.c functions and having added ld and pthread libraries in linker, i am still getting errors and cant build the project.
This is weird and should not happen. Please take a look at following screenshots that explain my problem.
************************************************************************************
************************************************************************************
What am I missing?
Please Help!
Thanks
Make sure that the pthread library is linked before the dl library.
In other words, your compiler command (as generated by Eclipse) should be:
gcc shell.c sqlite3.c -lpthread -ldl
and not
gcc shell.c sqlite3.c -ldl -lpthread
See here for a correct command line:
http://www.sqlite.org/howtocompile.html