How to link GLX? - c++

I'm trying to find the compiler flag for linking GLX on a Linux based system. So far, I have (in qt Creator):
unix:LIBS += -lglx -lX11 -lGLEW -lGLU -lGL -lXext -L/usr/X11R6/lib
But, that still gives me glxChooseVisual was not declared in this scope, thus preventing my code from compiling.

When defining a glX function, the 'X' must be capitalized. That was my fault for not seeing that. Consider this problem solved.

Related

Compiler flags for C++ using OpenGL, GLUT, GLEW

When I am compiling simple examples it works with the following compiler instructions, but I don't understand them in detail:
clang -o name name.cpp -L/usr/local/lib -lglfw -lGLEW -framework OpenGL
I understand the -o flag for the executable file and also the -L flag which clarifies where the compiler should search for libraries to use.
But when it comes to the -lglfw or -lGLEW flags I have no idea what they do. Same for the -framework flag, but there I have an idea what it does.
Also what is the difference between OpenGL, GLEW and GLUT?

Is there a way to automatically link all libraries an OpenGL program needs, without explicitly writing their flags on compile?

For an entry level program, I'm currently compiling with
g++ manip.c -o manip -lglfw3 -lX11 -lXxf86vm -lXrandr -lpthread -ldl -lXinerama -lXcursor -lGLEW
It was a very time-consuming process of trial and error figuring out which flags were necessary, googling the 'undefined references to', copy pasting every flag I came across and then deleting flags one by one until I had removed those I didn't need.
For a beginner who is learning the ropes, compiling example programs in tutorials (that don't have instructions on how to compile), is there a way to automatically link everything the program might need? Perhaps a way to move the libraries so they don't have to be explicitly linked?
If not, is there a way to simplify the flag adding process? (I can envisage the command becoming incredibly long from all the flags as the program grows in complexity.)
I'm writing this answer for someone at the level I'm at.
You apparently can write makefiles that will automatically detect what libraries should be linked. The syntax for doing so is way out of my depths, but I'll edit this in future if I advance to that.
To simplify the compiling option, you could write a makefile that links every single library you might need. The example I've included below has the libraries I've found out I require so far and may need to be extended significantly as I develop the OpenGL program further.
Call the file 'Makefile'. Run it using the command make.
So
vim Makefile
Inside the file, I have this:
manip: manip.c
g++ manip.c -o manip -lglfw3 -lX11 -lXxf86vm -lXrandr -lpthread -ldl -lXinerama -lXcursor -lGLEW -lglfw3 -lGL -lm -lXi
The term before the : on the first line will be the output executable. The term after the : on the first line is the C/C++ file the executable depends on. The next line is what the Makefile will execute if it detects the source code (after : on the first line) has changed.
If you have a more extensive set of files that depend on each other, you can add them below with the same syntax (google this if relevant to you).
So I now edit the manip.c file, run the command make, and manip is generated for me.

How might excess links/libraries affect the executable output during compilation?

For instance, if I were to link the object "example.o" with
-L/usr/X11R6/lib -L/usr/local/lib -lGL -lGLU -lm -lglut -lGLEW -lglfw3 -lX11 -lXxf86vm -lXrandr -lpthread -lXi -ldl -lXcursor -lXinerama
and the output executable compiled just as well (and perceivably functioned just as well) as when linked only with
-lGL -lm -lglfw3
how then might the excessive linkages of the former compilation affect an executable for the end user (if at all)? Load/run times? For larger programs (understanding that "example.o" is rather small)?
This question may be for my own edification, admittedly.
If your compiler driver is passing the "--as-needed" option to the linker by default, then this will make no difference in the resulting binary because the linker will drop the unneeded library dependencies.
The Debian wiki as an extensive article on this: Debian DSO Linking.

undefined reference to `iluInit' ONLY

This is very strange or very simple. I am using Devil library and in the very simple program I have to initialize it by:
ilInit();
iluInit();
And here is the problem. IlInit() works fine, but iluInit() is not! It can not find the reference.
Of course I include headers:
#include <IL/il.h>
#include <IL/ilu.h>
And compile my file by:
g++ -std=gnu++11 -O3 myIL.cpp -lglfw3 -lGL -lGLEW -lIL -lGLU -lX11 -lXxf86vm -lpthread -lXrandr -lXi -o myIL
(generally I want to use Devil lib for OpenGL project but I don't think it is important right now).
I have also all headers in /usr/include/IL and *.a, *.so etc. files in /usr/lib/x86_64-linux-gnu.
Btw it also be great if someone could explain me what the difference between IlInit(), iluInit() and ilutInit(). I cannot find the simple explanation.
I don't know anything about devils, but I suspect you forgot to link against the relevant library -- wouldn't that be libILU.so, i.e. require the compiler/loader option -lILU?

GLEW Linker Errors (undefined reference to `__glewBindVertexArray')

I've recently made the decision to re-write some OpenGL code for a game using im working on using non depreciated techniques. Instead of drawing primitives with glBegin() and glEnd(), i'm trying to stick to vertex array objects and such. I'm trying to get code to compile from http://www.opengl-tutorial.org/beginners-tutorials/tutorial-2-the-first-triangle/ . I've done alot of linking before but for some reason this isn't working. I'm trying to link GLEW to my project with CodeBlocks as my IDE and MinGW GCC as my compiler. How do I go about fixing this? Yes, i did link "glew32.lib"
This usually happens if you link GLEW statically, but don't inform the header about this to happen. For this you must define the preprocessor token "GLEW_STATIC". This is best done as a compiler option. In case of GCC, add -DGLEW_STATIC to your compiler command line.
Try this:
pkg-config --libs --static glew
in the terminal. Then, copy the libs it gives you and paste after your gcc/g++ statement:
g++ <your-file-name>.cpp -o <output-file-name> -lGL -lGLU -lglfw3 -lrt -lm -ldl -lXrandr -lXinerama -lXcursor -lXext -lXrender -lXfixes -lX11 -lpthread -lxcb -lXau -lXdmcp -lGLEW -lGLU -lGL -lm -ldl -ldrm -lXdamage -lX11-xcb -lxcb-glx -lxcb-dri2 -lxcb-dri3 -lxcb-present -lxcb-sync -lxshmfence -lXxf86vm -lXfixes -lXext -lX11 -lpthread -lxcb -lXau -lXdmcp
(some are repeated above because I used glfw too)
This is supposed to solve your problem, because usually these libraries are not declared.
If you use Linux, FLTK ui library with OpenGL, see .../bin/fltk-config file for LDLIBS. It should contain also "-lGLEW" or you can add this option to the LDLIBS parameter when compile. Of course "libglew-dev" should be installed.