Can't install OpenGL on MacOS10.14 - c++

I've tried many ways to install OpenGL, using homebrew to install glfw and glew:
$ brew install glew glfw3
Or compile through source code:
$ cd glfw && cmake . && make && sudo make install
$ cd glew && make && sudo make install
Xcode is used to create the project. Opengl.framework, glut.framework, libglfw3. A and libglew. A are all added to the project. The header file path is / usr / local / include, and the library path is / usr / local / lib. Main.cpp source code:
#include <iostream>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
void Render(void)
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLES);
{
glColor3f(1.0,0.0,0.0);
glVertex2f(0, .5);
glColor3f(0.0,1.0,0.0);
glVertex2f(-.5,-.5);
glColor3f(0.0, 0.0, 1.0);
glVertex2f(.5, -.5);
}
glEnd();
}
int main(void) {
GLFWwindow* win;
if(!glfwInit()){
return -1;
}
win = glfwCreateWindow(640, 480, "OpenGL Base Project", NULL, NULL);
if(!win)
{
glfwTerminate();
exit(EXIT_FAILURE);
}
if(!glewInit())
{
return -1;
}
glfwMakeContextCurrent(win);
while(!glfwWindowShouldClose(win)){
Render();
glfwSwapBuffers(win);
glfwPollEvents();
}
glfwTerminate();
exit(EXIT_SUCCESS);
return 0;
}
Error in compilation:
Undefined symbols for architecture x86_64:
... (many can't find errors)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Can anyone help me?

Related

GLEW and GLFW undefined references

I am trying to set up openGl with glfw and glew. This is the source code:
#define GLFW_INCLUDE_GLU
#ifdef _WIN32
#define GLFW_DLL // -> windows
#include <GL/glew.h>
#elif __linux__
#include <GL/glew.h>
#endif
#include <GLFW/glfw3.h>
#include <iostream>
#include <stdexcept>
GLFWwindow* window = NULL;
GLFWmonitor* monitor = NULL;
int window_width = 800;
int window_height = 600;
static void window_hints() {
const GLFWvidmode* mode = glfwGetVideoMode(monitor);
glfwWindowHint(GLFW_RED_BITS, mode->redBits);
glfwWindowHint(GLFW_GREEN_BITS, mode->greenBits);
glfwWindowHint(GLFW_BLUE_BITS, mode->blueBits);
glfwWindowHint(GLFW_REFRESH_RATE, mode->refreshRate);
glfwWindowHint(GLFW_TRANSPARENT_FRAMEBUFFER, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE);
}
static void set_window_callbacks() {
glfwSetWindowCloseCallback(window, [] (GLFWwindow *window) {
std::cout << "closing window!";
});
glfwSetKeyCallback(window, [] (GLFWwindow *window, int key,int scancode, int action, int mods) {
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
glfwSetWindowShouldClose(window, GLFW_TRUE);
}
});
glfwSetWindowSizeCallback(window, [] (GLFWwindow *window, int width, int height) {
glViewport(0, 0, width, height);
window_width = width;
window_height = height;
glLoadIdentity();
GLdouble aspect = (GLdouble)window_width / window_height;
glOrtho(-1, 1, -1 / aspect, 1 / aspect, 0.01, 10000);
glTranslatef(0, 0, -10);
});
}
void GLAPIENTRY
MessageCallback( GLenum source,
GLenum type,
GLuint id,
GLenum severity,
GLsizei length,
const GLchar* message,
const void* userParam )
{
fprintf( stderr, "GL CALLBACK: %s type = 0x%x, severity = 0x%x, message = %s\n",
( type == GL_DEBUG_TYPE_ERROR ? "** GL ERROR **" : "" ),
type, severity, message );
}
int main(int argc, char *argv[])
{
glfwSetErrorCallback([] (int code, const char * err_msg) {
std::cerr << "GLFW Error " << code << ": \n\t" << err_msg << std::endl;
});
if(!glfwInit())
throw std::runtime_error("glfwInit failed");
monitor = glfwGetPrimaryMonitor();
window_hints();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
window = glfwCreateWindow(window_width, window_height, "test window", NULL, NULL);
if(!window)
throw std::runtime_error("glfwOpenWindow failed.");
set_window_callbacks();
// GLFW settings
glfwMakeContextCurrent(window);
glewInit();
glfwSwapInterval(1);
std::cout << glGetString(GL_VERSION) << std::endl;
glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
glDebugMessageCallback(MessageCallback, 0);
glMatrixMode(GL_PROJECTION);
glViewport(0, 0, window_width, window_height);
glLoadIdentity();
GLdouble aspect = (GLdouble)window_width / window_height;
glOrtho(-1, 1, -1 / aspect, 1 / aspect, 0.01, 10000);
glTranslatef(0, 0, -10);
while(!glfwWindowShouldClose(window))
{
// process pending events
glfwPollEvents();
glClearColor(0, 0, 0, 1); // black
glClear(GL_COLOR_BUFFER_BIT);
//glRotatef(1, 0, 0, 0.1);
glBegin(GL_TRIANGLES);
glColor3f(1.0, 0.0, 0.0);
glVertex2f(-0.5, -0.5);
glColor3f(0.0, 1.0, 0.0);
glVertex2f( 0.5, -0.5);
glColor3f(0.0, 0.0, 1.0);
glVertex2f( 0.0, 0.5);
glEnd();
glfwSwapBuffers(window);
}
// clean up and exit
glfwTerminate();
std::cerr << std::endl;
return 0;
}
And this works perfectly under Linux with the following makefile:
main: main.cpp makefile
g++ main.cpp -g -o main -lglfw -lGL -lX11 -lGLEW -std=c++2a
Then, I wanted to make it to compile under windows too, so I tried with this makefile:
main.exe: main.cpp makefile
g++ main.cpp -g -o main.exe -I"D:\cpp_libraries\glfw-3.3.5.bin.WIN64\include" -I"D:\cpp_libraries\glew-2.1.0\include" -L"D:\cpp_libraries\glfw-3.3.5.bin.WIN64\lib-mingw-w64" -L"D:\cpp_libraries\glew-2.1.0\lib\Release\x64" -lglfw3dll -lglew32 -lopengl32
Here, "glew-2.1.0" and "glfw-3.3.5.bin.WIN64" are downloaded from the respective download pages for precompiled windows binaries. I have added "D:\cpp_libraries\glew-2.1.0\bin\Release\x64" and "D:\cpp_libraries\glfw-3.3.5.bin.WIN64\lib-mingw-w64" to the Path environment variable as they contain glew32.dll and glfw3.dll.
When I try to compile, g++ gives "undefined reference to " every single glew/gl/glfw function call. I also tried running the same g++ command through VS Code's tasks.json with the same result.
Then I noticed that everyone on the internet said I must link against "glfw3.lib" and I noticed that the first -L argument to g++ is a folder that does not actually contain any .lib files as it should. But I cannot find the "glfw3.lib" anywhere in the zip I downloaded earlier. I even tried building glfw from source with CMake, Make and Code::Blocks, but none of them generated a .lib file I can link against.
Edit: I forgot to mention that I have not added the dlls to my project directory, but it really should not be necessary. I tried it anyway and it didn't fix the problem.
Edit 2: it would be better to link with -lglfw, not -lglfwdll, so I changed that.
Edit 3: actually, gl functions are getting linked fine. The problem is only with glfw and glew.
I finally got it working. It turns out the precompiled binaries for glew and glfw do not work on my machine. I had to download both sources and compile the libraries myself. This is the makefile that finally works:
main.exe: main.cpp makefile
g++ main.cpp -g -o main.exe -I"D:/cpp_libraries/glfw-3.3.5-source/include" -I"D:/cpp_libraries/glew-2.1.0-source/include" -L"D:/cpp_libraries/glfw-3.3.5-compiled/src" -L"D:/cpp_libraries/glew-2.1.0-compiled/lib" -lglfw3 -lglew32 -lopengl32

How add OpenGL & GLUT for CLion on Windows?

I have installed OpenGL and Glut but when I'm trying to run it - get this error. Have I installed something wrong? Or should I edit CMake file?
CMakeLists.txt:
project(LW1)
set(CMAKE_CXX_STANDARD 17)
include_directories(OpenGL)
include_directories(OpenGL/include) # OpenGL/include has to contain the required OpenGL's .h files
include_directories(OpenGL/lib) # OpenGL/lib has to contain the required OpenGL's .lib files
add_custom_target(glutdlllib
COMMAND ${CMAKE_COMMAND} -E copy LW1/OpenGL/dll/glut32.dll ${CMAKE_BINARY_DIR}
)
set(OpenGlLibs glaux glu32 glui32 glut32 opengl32 )
#These 3 lines are just linking and making executables
add_executable(LW1 main.cpp)
target_link_libraries(LW1 ${OpenGlLibs})
add_dependencies(LW1 glutdlllib)
target_link_libraries(LW1 -lOpenGL32 -lfreeGLUT)
main.cpp :
#include <windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glaux.h>
#include <glut.h>
void resize(int width, int height)
{
}
void display(void) {
glColor3d(1,1,0);
glutSolidSphere(1.0, 25, 25);
glFlush();
}
void init(void) {
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);;
glLoadIdentity();
glOrtho(-5.0, 5.0, -5.0, 5.0, 2.0, 12.0);
gluLookAt(0,0,5, 0,1,0, 0,1,0);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char ** argv) {
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowPosition(50, 10);
glutInitWindowSize(400, 400);
glutCreateWindow("Hello");
glutReshapeFunc(resize);
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
Error:
"C:\Program Files\JetBrains\CLion 2020.2.1\bin\cmake\win\bin\cmake.exe" --build C:\Users\Anichaaaaaa\CLionProjects\LW1\cmake-build-debug --target LW1 -- -j 6
Error copying file "LW1/OpenGL/dll/glut32.dll" to "C:/Users/Anichaaaaaa/CLionProjects/LW1/cmake-build-debug".
mingw32-make.exe[3]: *** [CMakeFiles\glutdlllib.dir\build.make:76: CMakeFiles/glutdlllib] Error 1
mingw32-make.exe[2]: *** [CMakeFiles\Makefile2:96: CMakeFiles/glutdlllib.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:130: CMakeFiles/LW1.dir/rule] Error 2
mingw32-make.exe: *** [Makefile:150: LW1] Error 2
The command in add_custom_target is executed with the working directory set to your build directory. The relative path you give is thus interpreted as relative to the build directory.
Either set the working directory to CMAKE_CURRENT_SOURCE_DIR or use it convert the relative path to an absolute one in your copy command.

GLFW Undefined Reference to many things

I have tried several times in the past to write programs using OpenGL in C and C++ on several platforms, every time only to be stopped by the ridiculous dependency and linking issues that inevitably come up.
I am attempting to use GLFW with C++ on a linux system, building using CMake, with absolutely no success.
CMakeLists.txt:
cmake_minimum_required(VERSION 2.8)
project( GL_TEST_PROJECT )
find_package(OpenGL REQUIRED)
if(NOT OPENGL_FOUND)
message("ERROR: OpenGL not found")
endif(NOT OPENGL_FOUND)
set(GL_LIBRARY GL GLU X11)
add_executable(testgl testgl.cpp)
target_link_libraries(testgl glfw3 Xxf86vm pthread dl Xi Xcursor Xrandr X11 ${GL_LIBRARY} m)
testgl.cpp:
#include <GLFW/glfw3.h>
#include <stdlib.h>
#include <stdio.h>
static void error_callback(int error, const char* description)
{
fputs(description, stderr);
}
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
}
int main(void)
{
GLFWwindow* window;
glfwSetErrorCallback(error_callback);
if (!glfwInit())
exit(EXIT_FAILURE);
window = glfwCreateWindow(640, 480, "Simple example", NULL, NULL);
if (!window)
{
glfwTerminate();
exit(EXIT_FAILURE);
}
glfwMakeContextCurrent(window);
glfwSetKeyCallback(window, key_callback);
while (!glfwWindowShouldClose(window))
{
float ratio;
int width, height;
glfwGetFramebufferSize(window, &width, &height);
ratio = width / (float) height;
glViewport(0, 0, width, height);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-ratio, ratio, -1.f, 1.f, 1.f, -1.f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef((float) glfwGetTime() * 50.f, 0.f, 0.f, 1.f);
glBegin(GL_TRIANGLES);
glColor3f(1.f, 0.f, 0.f);
glVertex3f(-0.6f, -0.4f, 0.f);
glColor3f(0.f, 1.f, 0.f);
glVertex3f(0.6f, -0.4f, 0.f);
glColor3f(0.f, 0.f, 1.f);
glVertex3f(0.f, 0.6f, 0.f);
glEnd();
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwDestroyWindow(window);
glfwTerminate();
exit(EXIT_SUCCESS);
}
make output:
Linking CXX executable testgl
//usr/local/lib/libglfw3.a(x11_init.c.o): In function `initExtensions':
x11_init.c:(.text+0x1b8b): undefined reference to `XineramaQueryExtension'
x11_init.c:(.text+0x1ba3): undefined reference to `XineramaIsActive'
//usr/local/lib/libglfw3.a(x11_monitor.c.o): In function `_glfwPlatformGetMonitors':
x11_monitor.c:(.text+0x672): undefined reference to `XineramaQueryScreens'
collect2: error: ld returned 1 exit status
make[2]: *** [testgl] Error 1
make[1]: *** [CMakeFiles/testgl.dir/all] Error 2
make: *** [all] Error 2
I have tried several different changes to the CMakeLists.txt file, including removing and adding different dependencies, but it seems that there is nowhere for me to go from here. What do I need to do to get this program to compile?
Using the
set(CMAKE_CXX_FLAGS ...)
Does not work for me, but adding them all into
set(GLFW_DEPS X11 Xrandr Xinerama Xxf86vm Xcursor GL dl pthread)
Does get things working.
So, my target_link_libraries looks like this
target_link_libraries( ${PROJECT_NAME} glfw3 ${GLFW_DEPS} ${GLEW_STATIC_LIBRARY} )
It is possible that I'm not using CMAKE_CXX_FLAGS correctly, but you should try just adding the libraries your self. Do be careful about it, their order is important and depending on which version of GLFW you're using they could be different.
That error your getting is strictly because your missing the Xinerama library.
There are two ways to not have to add those libraries manually:
Option 1 is to use GLFW_LIBRARIES which you would have to include glfw source into your project as stated here build with Cmake and GLFW source
Option 2 would be to use the help of find_package as described here: Build with installed GLFW binaries
It appears as though you have glfw installed on your machine so I would recommend option 2, in part because I have yet to get option 1 to work and I'm currently having to manually enter the libraries. This is because I don't want to have glfw installed on machine vs having it all contained in the project.
Add this to "target_link_libraries()":
glfw ${GLFW_LIBRARIES} ${X11_LIBRARIES}
And then add this somewhere above "include_directories" in your CMakeLists.txt file:
#########################################################
# FIND X11
#########################################################
find_package(X11 REQUIRED)
include_directories(${X11_INCLUDE_DIRS})
link_directories(${X11_LIBRARY_DIRS})
add_definitions(${X11_DEFINITIONS})
if(NOT X11_FOUND)
message(ERROR " X11 not found!")
endif(NOT X11_FOUND)
Edit #2: And try adding the following to your CMakeLists.txt
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -lglfw3 -lX11 -lXrandr -lXinerama -lXi -lXxf86vm -lXcursor -lGL -lpthread")

OPENgl - GL/glut.h no such file or directory

can any one help me with the following error
GL/glut.h no such file or directory
The things I did are
After installation of MinGW i have added the Path to the path
enviornment variable
Added 'glut.h' to C:\MinGW\Include\GL
Added glut32.dll to C:\Windows\SysWOW64
Added glut32.lib to the project folder
COmpiled with 'g++ -o hello.exe -Wall hello.c glee.lib glut32.lib -lopengl32 -lglu32'
and still the error above persist please help
#include<GL/glut.h>
#include<iostream>
//#include<conio.h>
void render(void);
void keyboard(unsigned char c, int x, int y);
void mouse(int button, int state, int x, int y);
int main(int argc, char** atgv)
{
glutInit(&argc, argv);
glutInitDisplayMode( GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100, 100);
glutInitWindowSize(640, 480);
glutCreateWindow("Sample GLUT Application");
glutDisplayFunc(render);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMainLoop();
}
void keyboard(unsigned char c, int x, int y)
{
if(c == 27){
exit(0);
}
}
void mouse(int button, int state, int x, int y)
{
if(button == GLUT_RIGHT_BUTTON )
{
exit(0);
}
}
void render(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glBegin(GL_TRIANGLES);
glColor3f(1, 0, 0);
glVertex2f(-0.5, -0.5);
glColor3f(0, 1, 0);
glVertex2f(0.5, -0.5);
glColor3f(0, 0, 1);
glVertex2f(0.0, 0.5);
glEnd();
glutSwapBuffers();
}
some simple things that can cause problems in my experience (so make sure they are set!:) :
check to see if C:\MinGW\bin is in your path variable if not, add C:\MinGW\bin to your PATH (type %path% in a console window to ensure the path property is applied to the window console session you are using)
put glut32.dll into C:\windows\system32 so it can be located at runtime, or place it in the same directory as the executable you wish to run
Just checked and my minGW has glut32.dll in c:\mingw\bin
and libglut32.a in c:\mingw\lib
and glut.h should be in c:\mingw\include\GL
Apologies for the previous omission.
That should see you alright provided there are no other issues at play.
Let me know if you need more info/help :)
Addendum:
I located this link which whilst old may be of use to you in making the files available to mingw's environment.
On a Debian based systems such as Ubuntu, do
sudo apt-get install libglfw3-dev libgl1-mesa-dev libglu1-mesa-dev

Opengl superbible 5th edition code problem

I've started reading superbible 5th edition and I'm stuck at the first program. As I'm using xp & vs2008, I've followed all the instruction provided by the book to setup the vs2008 to run the triangle program, but after compilation process it shows an error and I've no idea why is this happenning. The program is following:
// Triangle.cpp
// Our first OpenGL program that will just draw a triangle on the screen.
#include <GLTools.h> // OpenGL toolkit
#include <GLShaderManager.h> // Shader Manager Class
#ifdef __APPLE__
#include <glut/glut.h> // OS X version of GLUT
#else
#define FREEGLUT_STATIC
#include <GL/glut.h> // Windows FreeGlut equivalent
#endif
GLBatch triangleBatch;
GLShaderManager shaderManager;
///////////////////////////////////////////////////////////////////////////////
// Window has changed size, or has just been created. In either case, we need
// to use the window dimensions to set the viewport and the projection matrix.
void ChangeSize(int w, int h)
{
glViewport(0, 0, w, h);
}
///////////////////////////////////////////////////////////////////////////////
// This function does any needed initialization on the rendering context.
// This is the first opportunity to do any OpenGL related tasks.
void SetupRC()
{
// Blue background
glClearColor(0.0f, 0.0f, 1.0f, 1.0f );
shaderManager.InitializeStockShaders();
// Load up a triangle
GLfloat vVerts[] = { -0.5f, 0.0f, 0.0f,
0.5f, 0.0f, 0.0f,
0.0f, 0.5f, 0.0f };
triangleBatch.Begin(GL_TRIANGLES, 3);
triangleBatch.CopyVertexData3f(vVerts);
triangleBatch.End();
}
///////////////////////////////////////////////////////////////////////////////
// Called to draw scene
void RenderScene(void)
{
// Clear the window with current clearing color
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
GLfloat vRed[] = { 1.0f, 0.0f, 0.0f, 1.0f };
shaderManager.UseStockShader(GLT_SHADER_IDENTITY, vRed);
triangleBatch.Draw();
// Perform the buffer swap to display the back buffer
glutSwapBuffers();
}
///////////////////////////////////////////////////////////////////////////////
// Main entry point for GLUT based programs
int main(int argc, char* argv[])
{
gltSetWorkingDirectory(argv[0]);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_STENCIL);
glutInitWindowSize(800, 600);
glutCreateWindow("Triangle");
glutReshapeFunc(ChangeSize);
glutDisplayFunc(RenderScene);
GLenum err = glewInit();
if (GLEW_OK != err) {
fprintf(stderr, "GLEW Error: %s\n", glewGetErrorString(err));
return 1;
}
SetupRC();
glutMainLoop();
return 0;
}
And the error displaying is this
1>d:\opengl\SB5\freeglut-2.6.0\VisualStudio2008Static\Release\freeglut_static.lib : fatal error LNK1107: invalid or corrupt file: cannot read at 0x3
1>Build log was saved at "file://c:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\dfdf\Debug\BuildLog.htm"
Could somebody please help me with this problem? Thank you.
I think your problem lays somewhere else than code. I've traced back what is
#define FREEGLUT_STATIC
doing and this popped out:
# ifdef FREEGLUT_STATIC
# define FGAPI
# define FGAPIENTRY
/* Link with Win32 static freeglut lib */
# if FREEGLUT_LIB_PRAGMAS
# pragma comment (lib, "freeglut_static.lib")
# endif
However if you downloaded prepared package from freeglut Windows Development Libraries, you get only freeglut.lib. So either you have to build "freeglut_static.lib" for yourself from freeglut, or don't use static macro and you should be fine. Linker is just saying what it's experiencing - can't find your file so not able to read...
and BTW: What book instructs is one thing but maybe you skipped something or they just didn't included it, but when you download clean freeglut, VisualStudio2008Static folder doesn't contain any library just another folders and visual studio project. You need to open that project, select release version in MSVC (build-> configuration manager) and build project. You get you freeglut_static.lib in release folder.
The error message you're getting indicates that the freeglut_static.lib file is corrupt. You would be best off to reinstall the library to ensure the file was copied correctly, or you could try rebuilding the library from source.
It might be worth trying a prebuilt version from another website, maybe you'll have more luck with the Visual Studio version available here.