Compiling first OpenGL Redbook program (triangles.cpp) [duplicate] - c++

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 8 years ago.
I'm starting to learn OpenGL with Redbook version 4.3 and I need some linking help (I think). I am running Linux Mint and a Radeon HD 5000/6000/7350/8350 Series video card. I'm trying to compile and link the first program (triangles.cpp). I installed freeglut3, freeglut3-dev, libxi-dev, glew-utils, and libglew-dev. I found this linking command in an old version of OpenGL Superbible and I'm guessing I need to add -lGLEW.
g++ triangles.cpp -lX11 -lXi -lglut -lGL -lGLU -lGLEW
I get the following error:
/tmp/ccXSL2nx.o: In function `init()':
triangles.cpp:(.text+0x11d): undefined reference to `LoadShaders'
collect2: error: ld returned 1 exit status
I copied over vgl.h and LoadShaders.h and LoadShaders.cpp from the Redbook's source code download. What else am I missing?

Try this:
g++ triangles.cpp LoadShaders.cpp -lX11 -lXi -lGL -lGLU -lGLEW -lglut -o triangles
This will compile and link both triangles.cpp and LoadShaders.cpp into a single output file triangles.
Note, too, that you might not need "-lX11 -lXi". To test this try:
g++ triangles.cpp LoadShaders.cpp -lGL -lGLU -lGLEW -lglut -o triangles
Also note that the order of libraries is important.

Related

Problems setting up GLFW on Arch Linux

I use Linux, more specifically, Arch Linux. I wanted to work with OpenGL, so I did the following steps:
First, I downloaded the glfw-x11 with the pacman package manager.
I also used cmake to add glfw to /usr/local/include, everything works fine.
I wrote a Makefile to run my program with the command:
gcc -o demo main.c -lglfw3 -lm -lXrandr -lXi -lXxf86vm -lpthread
Also note that I removed -lGl because gcc, the compiler, screamed at me.
Here is my project directory: glad main.c Makefile. Note that glad is a directory. And it works fine.
I am running on Linux so I can't install Visual Studio and I am really don't think I need CodeBlocks to create an OpenGL project.
The error the compiler gives me when I run this code (in main.c):
#include <stdio.h>
#include "glad/glad.h"
#include <GLFW/glfw3.h>
int main() {
glfwInit();
printf("Hello, OpenGL!");
// Code goes here..
glfwTerminate();
return 0;
}
The compiler gave me:
/usr/bin/ld: /usr/local/lib/libglfw3.a(x11_window.c.o): undefined reference to symbol 'XPending'
/usr/bin/ld: /usr/lib/libX11.so.6: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
Edit
lGL just worked, but still I get the same error.
Another Edit
Adding -ldl, lXinerama and lXcursor worked for me.
Final Edit
I fixed it, first, you create a main.cpp (or whatever you wanna call it). Make sure you have libgl, glu and glfw-x11 installed. Then create a Makefile with the flags -lGL -lGLU -lglfw3 -lm -lXrandr -lXi -lX11 -lXxf86vm -lpthread -ldl -lXinerama -lXcursor. Then download glad. Extract glad.zip (Make sure glad is in the project folder). Move src/glad.c into the project directory. Include glad with "include/glad/glad.h" or whatever directory you choose. Also make sure GLFW is in /usr/local/bin.

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 to correct use GLFW3 with code::blocks on ubuntu? [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 7 years ago.
I download GLFW source code. Next correct compile with sudo make install, after that library is installed in /usr/local , now when I create project in code blocks I can include GLFW/glfw3.h, but when I try to complie i have errors
How to correct add this library ?
Every time I must write this command to terminal: " g++ -std=c++0x main.cpp -lglfw3 -lGL -lX11 -lXi -lXrandr -lXxf86vm -lXinerama -lXcursor -lrt -lm -pthread" , is any posiibility to automatize linking and compilation with Code::Blocks ?
Just click :
Project -> Build Options
And then just add liked libs. Of course widouth - sign. And you must remember that there are Debug Release and both. So you must enter names of those libs in good target.

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.

undefined reference to symbol 'gluLookAt' [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 7 years ago.
I'm using Fedora 16. I've installed freeglut and freeglut-devel packages. I tried to rum a simple opengl program, but i'm getting the following error
gcc cube.c -o cube -lglut
/usr/bin/ld: /tmp/ccSFol4w.o: undefined reference to symbol 'gluLookAt'
/usr/bin/ld: note: 'gluLookAt' is defined in DSO /usr/lib/libGLU.so.1 so try adding it to the linker command line
/usr/lib/libGLU.so.1: could not read symbols: Invalid operation
collect2: ld returned 1 exit status
Compile : g++ sampleCode.cpp -lglut -lGL -lGLU
run : ./a.out
is your answer.
You have to link some gl libraries.
g++ cube.c -o cube -I /usr/lib/libglut.so.3 /usr/lib/libGL.so.1 /usr/lib/libGLU.so.1 -lGL
I think you should consult some introduction text on compilers, linkers and libraries, i.e. how the pieces come together when building a program. In essence the linker is telling you, that there are some loose ends and it cannot finish linking the program due to them. Adding a library happens by the -l switch with library name (GLU in your case), not by giving it a full path to the library file.
do what it says
gcc cube.c -o cube -lglut -lGLU
I got the exact same problem in Ubuntu 12.04 LTS when I wrote:
g++ square.cpp -lglut
But then I found on the web that some people also added -lGL and lGLU so I did it and it compiles now:
g++ square.cpp -lglut -lGL -GLU