How add OpenGL & GLUT for CLion on Windows? - c++

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.

Related

Can't install OpenGL on MacOS10.14

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?

Calling gluPerspective and gluLookAt gives me an undefined reference?

I'm trying the following source, from Instant Glew:
#include <GL/glew.h>
#include <GL/freeglut.h>
#include <GL/glu.h>
#include <GL/gl.h>
void initGraphics()
{
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
const float lightPos[4] = {1, .5, 1, 0};
glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
glEnable(GL_DEPTH_TEST);
glClearColor(1.0, 1.0, 1.0, 1.0);
}
void onResize(int w, int h)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, w, h);
gluPerspective(40, (float) w / h, 1, 100);
glMatrixMode(GL_MODELVIEW);
}
void onDisplay()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(0.0, 0.0, 5.0,
0.0, 0.0, 1.0,
0.0, 1.0, 0.0);
glutSolidTeapot(1);
glutSwapBuffers();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowSize(500, 500);
glutCreateWindow("Teapot");
initGraphics();
glutDisplayFunc(onDisplay);
glutReshapeFunc(onResize);
glutMainLoop();
return 0;
}
My build setup is the Windows 10 VSCode, with MSYS2, and a Makefile like this:
OBJS = 01-teapot.cpp
OBJ_NAME = C:/<"MyProjectPath">/build/01-teapot
INC_PATH = -IC:/msys64/mingw64/include/GL -LC:/msys64/mingw64/include/GL
INC_LINK_LIBS = -lglew32 -lopengl32 -lfreeglut
compiling :
g++ -Wall $(OBJS) $(INC_PATH) $(INC_LINK_LIBS) -o $(OBJ_NAME) -g
But the output is like:
C:\Users\<"myUser">\AppData\Local\Temp\cc5d3AtP.o: In function `onResize(int, int)':
c:\<"MyProjectPath">\PaPu_Instant_GLEW/01-teapot.cpp:22: undefined reference to `gluPerspective'
C:\Users\<"myUser">\AppData\Local\Temp\cc5d3AtP.o: In function `onDisplay()':
c:\<"MyProjectPath">\PaPu_Instant_GLEW/01-teapot.cpp:31: undefined reference to `gluLookAt'
collect2.exe: error: ld returned 1 exit status
make: *** [Makefile:32: compilando] Error 1
I really don't know where I'm failing. Did I forget to link some lib? I tried to add -lglut -lGLU already, on the linked libs, but the compiler can't find it...
gluLookAt is a function from the GL Utility library. You need to include -lglu32 in the linker options. It's explained in Using GLUT with MinGW.
Also the order in which you give the libraries matter; see my related answer for reference. In your case, replace glfw3 with freeglut.
I believe you are using GLUT and GLU for learning OpenGL which is okay but be informed that they were part of OpenGL 1 back in the day but is no longer an integral part of OpenGL and are deprecated. If you're doing production-level work, I'd recommend using a more mature library like GLFW (instead of GLUT/FreeGLUT); GLM has all the convenience functions that GLU provides. See towards the end of datenwolf's answer.

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")

Cannot install glut for Dev-c++ on windows 64 bit

I want to install glut library for Dev-c++, I do the following:
Download and install Dev-c++ from here, (Dev-Cpp 5.7.1 TDM-GCC x64 4.8.1 Setup.exe)
Add glut from: Tool -> Check for
Packages... -> install glut 3.7.6 from Devpak site
(Base working directory: C:\Program Files (x86)\Dev-Cpp)
Copy glut.h, glutf90.h from include\GL to MinGW64\include\GL and
MinGW64\x86_64-w64-mingw32\include\GL (I don't know where is the correct folder so I add to somewhere else...)
Copy libglut32.a from lib\ to MinGW64\lib (reference from youtube here)
Open Dev-c++ and
Create an empty C++ project, add these options to project Linker Parameters:-lglut32 -lglu32 -lopengl32 -lwinmm -lgdi32
Add some file, like this:
//Lab01_Perimitives.cpp
/* -- INCLUDE FILES ------------------------------------------------------ */
#include <gl/gl.h>
#include <gl/glu.h>
#include <gl/glut.h>
/* ----------------------------------------------------------------------- */
void myInit( void ) {
glClearColor( 0.0, 0.0, 0.0, 0.0 );
glColor3f( 1.0, 0.0, 1.0 );
glPointSize( 3.0 );
glMatrixMode( GL_PROJECTION );
glLoadIdentity( );
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}
/* ----------------------------------------------------------------------- */
/* ----------------------------------------------------------------------- */
void myDisplay( void ) {
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glBegin(GL_POLYGON);
glColor3f(1.0f,0.0f,0.0f);
glVertex3f(0.25, 0.25, 0.0);
glColor3f(0.0f,1.0f,0.0f);
glVertex3f(0.75, 0.25, 0.0);
glColor3f(0.5f,0.5f,1.0f);
glVertex3f(0.75, 0.75, 0.0);
glColor3f(0.0f,0.0f,1.0f);
glVertex3f(0.25, 0.75, 0.0);
glEnd();
glutSwapBuffers();
}
/* ----------------------------------------------------------------------- */
int main( int argc, char *argv[] ) {
// Initialize GLUT.
glutInit( &argc, argv );
// Set the mode to draw in.
glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB );
// Set the window size in screen pixels.
glutInitWindowSize( 640, 480 );
// Set the window position in screen pixels.
glutInitWindowPosition( 100, 150 );
// Create the window.
glutCreateWindow( "Lab01" );
// Set the callback funcion to call when we need to draw something.
myInit( );
glutDisplayFunc( myDisplay );
// Initialize some things.
// Now that we have set everything up, loop responding to events.
glutMainLoop( );
}
/* ----------------------------------------------------------------------- */
But when I compile I get these error:
c:\program files (x86)\dev-cpp\mingw64\x86_64-w64-mingw32\bin\ld.exe skipping incompatible C:/Program Files (x86)/Dev-Cpp/MinGW64/lib/libglut32.a when searching for -lglut32
c:\program files (x86)\dev-cpp\mingw64\x86_64-w64-mingw32\bin\ld.exe skipping incompatible c:/program files (x86)/dev-cpp/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/4.8.1/../../../../lib/libglut32.a when searching for -lglut32
...etc...
c:\program files (x86)\dev-cpp\mingw64\x86_64-w64-mingw32\bin\ld.exe cannot find -lglut32
E:\Mon hoc\Do hoa may tinh\Lab01\collect2.exe [Error] ld returned 1 exit status
E:\Mon hoc\Do hoa may tinh\Lab01\Makefile.win recipe for target 'Lab01.exe' failed
What is problem here, how to fix it?
My computer is running Windows 8.1 64 bit

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