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.
Related
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!
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?
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.
I'm trying building Box2d v2.3.1 on my Ubuntu (13.10) machine. This is what I'm doing:
$ premake4 gmake
$ cd Build/gmake/
$ make
But the testbed is not building correctly. I get lots of undefined reference errors for glfw and glew symbols, like this:
obj/Debug/Testbed/Main.o: In function `main':
/home/mostafa/.adobe/box2d-2.3.1/Box2D/Build/gmake/../../Testbed/Framework/Main.cpp:458: undefined reference to `glfwCreateWindow'
I have the development packages for both glfw and glew installed. I also checked the Testbed.make makefile and, since I saw no reference to glfw, added -lglfw to the two places where LIBS variable was defined. But I still get the same error.
I managed to fix this at last, after lots of searching and tweaking. This is what I did:
Make sure you have the very latest version of premake. I had to install premake 4.4 (beta version).
Compile and install the latest version of glfw (3.0.4 at the moment) from source. The version in Ubuntu's repositories does not work.
Make sure you have glew and xorg development packages. I installed these from Ubuntu's repository: sudo apt-get install libglew-dev xorg-dev
After running premake4 gmake in Box2D directory, go to Build/gmake and edit Testbed.make. Change the line LIBS += $(LDDEPS) -lX11 -lGL -lGLU -lglut into this LIBS += $(LDDEPS) -lX11 -lGL -lGLU -lglut -lGLEW -lglfw3 -lX11 -lXxf86vm -lpthread -lXrandr -lXi.
Now run make.
I'm trying to cross-compile a small test opengl/glew program and I get linker errors from undefined references.
$ /usr/bin/i486-mingw32-g++ -I/usr/i486-mingw32/include -L/usr/i486-mingw32/lib/ -lglfw -lglew32 -lopengl32 main.cc
/tmp/cct8OpVh.o:main.cc:(.text+0x50): undefined reference to `glfwInit'
/tmp/cct8OpVh.o:main.cc:(.text+0xa6): undefined reference to `glfwOpenWindowHint'
...
The same code does work when compiling for linux:
$ g++ -I/usr/include -L/usr/lib/ -lglfw -lGLEW -lGL main.cc
One thing that caught my eye is that every exported symbol from cross-compiled libraries has an extra underscore prefix:
$ nm /usr/lib/libglfw.a | grep glfwInit$
00000000 T glfwInit
$ /usr/i486-mingw32/bin/nm /usr/i486-mingw32/lib/libglfw.a | grep glfwInit$
00000000 T _glfwInit
This seems to be a common thing since even libstdc++.a shares this property, but why is my cross-compiler linker then looking for non-underscore symbols?
Running arch with following packages (local means AUR):
community/mingw32-binutils 2.23.1-3
community/mingw32-gcc 4.7.2-1
local/mingw32-glew 1.9.0-1
local/mingw32-glfw 2.7.7-1
community/mingw32-pthreads 2.9.1-1
community/mingw32-runtime 3.20-4
community/mingw32-w32api 3.17-1
EDIT
After playing out with both pkg-config and watching glfw recompile and test itself, I came up with the following magic that seems to work, at least I'm compiling:
/usr/bin/i486-mingw32-g++ -I/usr/i486-mingw32/include -L/usr/i486-mingw32/lib -mwindows main.cc -lglew32 /usr/i486-mingw32/lib/libglfw.a /usr/i486-mingw32/lib/libopengl32.a -static-libgcc
There are few questions though:
What is the difference between linking with -l and without?
Why do I need to use -l with glew and cannot with glfw
I was able to solve my problem and, in case someone ever runs into similar situation hope this helps you.
There are two versions of glew32 -library in my system, glew32.a and glew32.dll.a.
glew32.a does not allow for using --static, glew32.dll.a does.
The two commands which compile succesfully, only first of which I've run are:
/usr/bin/i486-mingw32-g++ -I/usr/i486-mingw32/include -L/usr/i486-mingw32/lib main.cc -lglew32.dll -lglfw -lopengl32 --static
/usr/bin/i486-mingw32-g++ -I/usr/i486-mingw32/include -L/usr/i486-mingw32/lib main.cc -lglew32 -lglfw -lopengl32
Looking at my old compiling attempts, the problem was wrong order of libraries and that main.cc was after the libraries.
There is a program, called pkg-config that helps you to configure your compiler. See the manual pages for usage information, but, for this case, its output is:
-mwin32 -I/usr/i486-mingw32/include -L/usr/i486-mingw32/lib -lglfw -lglu32 -lopengl32 -lm -s -mwindows -e _mainCRTStartup
Try to compile with this, I guess it will work.