g++ -lglfw3 "Undefined reference" - c++

I'm trying to compile and link my c++ GLFW3 program with g++,
this is a simple test program i wrote:
#include <GLFW/glfw3.h>
#include <iostream>
int main()
{
if (!glfwInit())
std::cout << "glfwInit(); // ERROR" << std::endl;
return 0;
}
I'm using this command on ubuntu 14.04: g++ src/main.cpp -lglfw3,
and it gives me the following extremely long list of errors: http://pastebin.com/p58k3x41

Compiling with g++ src/main.cpp -lglfw3 -pthread -lXrandr -lXxf86vm -lXi -lXinerama -lX11 -ldl -lXcursor works for me without any errors, i don't fully understand why it needs all these extra libraries, but it works so hurray!

I recently faced the same errors, on my machine the working command line was:
gcc -o myprog myprog.c -lglfw3 -lGL -lX11 -lXxf86vm -lpthread -lXrandr -lXi -lXinerama -lXcursor -lm
^^^^
Consider also that depends on how you compiled glfw3 library (shared or static see in the docs) you will obtain a glfw3.so free binary!

Related

How to embed glut library into c++ project?

I have a c++ project but when distribute this, need install libglut.so.3.:
./bin: error while loading shared libraries: libglut.so.3: cannot open shared object file: No such file or directory
I want the user not to have to install this dependency. How to embed the library into project?
I try compile as static library in c++ project using g++. My make file contains:
#g++ \
-o bin \
-std=c++11 \
main.cpp \
-lm -lGL -lGLU -lglut \
;
I try define glut as static library:
-lm -lGL -lGLU -Wl,-Bstatic -lglut -Wl,-Bdynamic
But the compiler says:
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/libglut.a(libglut_la-freeglut_state.o): undefined reference to symbol 'XGetWindowAttributes'
/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/libX11.so: error adding symbols: DSO missing from command line
I try download freeglut-3.0.0 and compile as static library:
Change CMakeLists.txt file for static compile:
OPTION(FREEGLUT_BUILD_SHARED_LIBS "Build FreeGLUT shared library." OFF)
OPTION(FREEGLUT_BUILD_STATIC_LIBS "Build FreeGLUT static library." ON)
And in the CMakeCache.txt:
//Build FreeGLUT shared library.
FREEGLUT_BUILD_SHARED_LIBS:BOOL=OFF
//Build FreeGLUT static library.
FREEGLUT_BUILD_STATIC_LIBS:BOOL=ON
And compile it:
$ cmake .
$ make
[ 62%] Built target freeglut_static
...
[100%] Built target shapes_static
And verify:
$ ll lib/libglut.a
-rw-r--r-- 1 me me 690860 nov 17 19:11 lib/libglut.a
$ file lib/libglut.a
lib/libglut.a: current ar archive
Now, change the makefile:
-lm -lGL -lGLU freeglut-3.0.0/lib/libglut.a
And compile:
$ make
usr/bin/ld: freeglut-3.0.0/lib/libglut.a(fg_state_x11.c.o): undefined reference to symbol 'XGetWindowAttributes'
//usr/lib/x86_64-linux-gnu/libX11.so.6: error adding symbols: DSO missing from command line
But, same error message.
In similar problem: fglut/libfglut.a(freeglut_state.o): undefined reference to symbol 'XGetWindowAttributes' , i try add x11 libraries into project:
-lm -lGL -lGLU -lX11 freeglut-3.0.0/lib/libglut.a
Or:
-lm -lGL -lGLU -lX11 -Wl,-Bstatic -lglut -Wl,-Bdynamic
But have a same error message. What happened?
I have successfully linked my a GLUT program using this command
g++ -o ogl-test main.cpp freeglut-3.0.0/lib/libglut.a -lGL -lGLU -lX11 -lXxf86vm -lXext -lXrandr -lXt -lXi
Hopefully this will also work for you. Make sure you change the path to glut library.

How to set up in NetBeans 8.1 to successfully compile an OpenGL code which uses GLFW

I am trying to compile a sample code in the 9th edition of "OpenGL Programming Guide" using NetBeans v8.1 on Ubuntu 14.04 64bit x86. The sample code is triangles.cpp, the first sample code of the book, downloadable from here. I have added information in the "Linker" tab of Project Properties dialog as follows:
You can see that "Additional Library Directories", "Libraries" and "Additional Options" fields are filled with needed and correct information (at least I think so).
However, when I build the project by clicking the "Clean and Build Project" button in the IDE, I got tons of errors:
g++ -c -g -I/home/me/ComputerGraphics/include -I/home/me/glfw-3.2.1/include -MMD -MP -MF "build/Debug/GNU-Linux/01-triangles.o.d" -o build/Debug/GNU-Linux/01-triangles.o 01-triangles.cpp
mkdir -p dist/Debug/GNU-Linux
g++ -o dist/Debug/GNU-Linux/opengl1 build/Debug/GNU-Linux/01-triangles.o -L/home/me/glfw-3.2.1/bin/lib -Wl,-rpath,/home/me/glfw-3.2.1/bin/lib -lglfw3 -pthread -ldl -lGLU -lGL -lrt -lXrandr -lXxf86vm -lXi -lXinerama -lX11
build/Debug/GNU-Linux/01-triangles.o: In function `init()':
/home/me/ComputerGraphics/OpenGL1/01-triangles.cpp:27: undefined reference to `gl3wGenVertexArrays'
/home/me/ComputerGraphics/OpenGL1/01-triangles.cpp:28: undefined reference to `gl3wBindVertexArray'
/home/me/ComputerGraphics/OpenGL1/01-triangles.cpp:35: undefined reference to `gl3wCreateBuffers'
/home/me/ComputerGraphics/OpenGL1/01-triangles.cpp:36: undefined reference to `gl3wBindBuffer'
/home/me/ComputerGraphics/OpenGL1/01-triangles.cpp:37: undefined reference to `gl3wBufferStorage'
/home/me/ComputerGraphics/OpenGL1/01-triangles.cpp:46: undefined reference to `LoadShaders'
......
I had previously thought it is related to vulkan but now I have added -ldl and the errors persist. So, how can I successfully compile the triangles.cpp code on linux within NetBeans? Thank a lot.
It turns out I need one more library GL3W in addition to GLFW. Follow these steps (based on the settings I have set already in NetBeans):
(1) Go to https://github.com/shakesoda/gl3w to install GL3W, or use existing files shipped with the book.
(2) Set the linker flag to:
-pthread -ldl -lGLU -lGL -lrt -lXrandr -lXxf86vm -lXi -lXinerama -lX11 -lXcursor
Note: the -ldl and -lXcursor flags are not mentioned in previous threads but indeed needed.
(3) Add gl3w.c and LoadShaders.cpp to the Source Files
(4) Add #include <cstdio> at the beginning of LoadShaders.cpp
(5) Compile and done!

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 symbol 'glFrontFace'

I have spent all day trying to resolve this issue and now am looking for a bit of help.
My global Linker settings:
-lGL -lGLU -lpthread -lXrandr -lXxf86vm -lXi -lXinerama -lX11 -l/usr/lib/libglfw.sso
I have two projects, one is a library used by the other.
When I compile Project A, it compiles into a library without issue. When i compile Project B, while also linking to the library created by Project A, I get the error in the title.
the compiler command is(called from Project B)(libEngine.a is the result of Project A):
g++ -L/usr/lib -o bin/Debug/Game obj/Debug/main.o obj/Debug/src/MyScene.o -lGL -lGLU -lpthread -lXrandr -lXxf86vm -lXi -lXinerama -lX11 ../Engine/bin/Debug/libEngine.a /usr/lib/libglfw.so
Any help would be appreciated.
Is libGL.so file or link present in /usr/lib? If yes. then check the pressence/architecture of the (lib) file pointed by the link. I hope this will solve the issue.
This was aswered by Gyapti jain, there was a missing link to the actual location of the library. the missing link belonged in /usr/lib, the library was in /usr/library/nvidia-331

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.