Setting up OpenGL NetBeans project with GLFW on Ubuntu - c++

I am trying to setup OpenGL developing environment on Ubuntu.I installed all the libs including GLFW as I don't want to use GLUT.GLEW lib has been installed too.I am trying to set it all in NetBeans.I have never used it before and currently I am getting :
undefined reference to `glfwInit' error while running this simple code:
#include <stdio.h>
#include <cstdlib>
#include <iostream>
#include <GL/glew.h>
#include <GL/glfw.h>
#include "glm.hpp"
using namespace std;
using namespace glm;
int main(int argc, char** argv) {
/* init GLFW */
if(!glfwInit()){
fprintf(stderr,"failed to init GLFW");
}
return 0;
}
I am sure it is linker related problem.So I have added libglfw.a to Linker->Libraries and then got even more errors like these:
make[2]: Entering directory /home/sasmaster/NetBeansProjects/OpenGLDemo'
mkdir -p dist/Debug/GNU-Linux-x86 g++ -o dist/Debug/GNU-Linux-x86/opengldemo build/Debug/GNU-Linux-x86/main.o /usr/lib/libglfw.a
/usr/lib/libglfw.a(window.o): In functionglfwOpenWindow':
/usr/lib/libglfw.a(x11_init.o): In function _glfwPlatformInit':
/usr/lib/libglfw.a(x11_init.o): In function_glfwPlatformInit':
/usr/lib/libglfw.a(x11_init.o): In function _glfwPlatformInit':
/usr/lib/libglfw.a(x11_init.o): In function_glfwPlatformInit':
/usr/lib/libglfw.a(x11_init.o): In function _glfwPlatformTerminate':
/usr/lib/libglfw.a(x11_init.o): In function_glfwPlatformTerminate':
/usr/lib/libglfw.a(x11_window.o): In function translateKey':
/usr/lib/libglfw.a(x11_window.o): In functiontranslateKey':
/usr/lib/libglfw.a(x11_window.o): In function translateKey':
/usr/lib/libglfw.a(x11_window.o): In functiontranslateChar':
...........
........................
What else should I link or install?
Update:
Here it is said one should add LIBRARIES='-pthread -lglfw -lGL -lX11 -lGLU -lXxf86vm'
But where should I add those in NetBeans properties? I tried put into the MakeFile and "linker options" too and nothing helped.
Update1
Compiling the main.cpp manually using this command:
**g++ main.cpp -o Game -lglfw -lGL -lGLU -lX11 -lpthread -lXxf86vm -lm**
Compiles fine.How do I link all these libs via NetBeans???

I figured out the solution.For those interested:
Openg Project properties.Then "Linker" ->"Libraries".
In the Libraries dialog:
Click "Add Option" -> "Other Option".
In the textfield insert these params:
-lGLEW -lglfw -lGL -lGLU -lX11 -lpthread -lXxf86vm -lm
I guess lGLU is needed only if you use the old OpenGL version.

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.

Problems including GLFW header in c++ program

I need to include GLFW header in my c++ program. The installation was fine and the GLFW folder exists in my usr\include folder and g++ does look for header files in that folder. Despite that it throws an error telling me that the GLFW directory doesnt exist.
I am using sublime text as my editor and IDE and my system is Ubuntu-20.04
FOllowing is the code, the terminal command i used to compile and the error message i encountered:
#include <GLFW\glfw3native.h>
#include <iostream>
int main(int argc, char const *argv[])
{
std::cout << "All DOne!!" << std::endl;
return 0;
}
g++ -g -o bin/debug/main src/*.cpp -x64 -std=c++17 -Wall -I -include -lglfw3 -lGL -lm -lXrandr -lXi -lX11 -lXxf86vm -lpthread && ./bin/debug.main
src/main.cpp:1:10: fatal error: GLFW\glfw3.h: No such file or directory
1 | #include <GLFW\glfw3.h>
| ^~~~~~~~~~~~~~
compilation terminated.
I cannot tell where the problem lies. please help.
and my system is Ubuntu-20.04
On Linux, the path delimiter is /, not \, so
#include <GLFW/glfw3native.h>
Note that while windows primarily uses backslash, it also accepts the forwad slash, so always using / is also the best option for cross-platform programming.

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.

g++ -lglfw3 "Undefined reference"

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!

C++ with GLUT static compile on linux

I am trying to compile simple program to standalone executable so i can send it to my friend. But i am facing a problem with GLUT dependencies. Command i tried is:
g++ triangle.cpp -static -lglut -lGLU -lGL -lm
And i got this error:
/usr/bin/ld: cannot find -lGL
collect2: error: ld returned 1 exit status
When i try to use Mingw64 i get this:
i686-w64-mingw32-g++ triangle.cpp -static -lglut -lGLU -lGL -lm -o test.exe
triangle.cpp:9:21: fatal error: GL/glut.h: No such file or directory
#include <GL/glut.h>
^
compilation terminated.
My includes
#include <vector>
#include <cstdio>
#include <GL/glut.h>
#include <math.h>
#include <string>
#include <sstream>
/usr/bin/ld: cannot find -lGL
collect2: error: ld returned 1 exit status
It seems that you do not have the GLUT libraries installed.
For GNU linux with aptitude, try
apt-get update
apt-cache search freeglut
to get a list of all libraries containing freeglut.
I think (not totally sure) that you need freeglut3-dev installed.